Subversion Repositories SmartDukaan

Rev

Rev 1081 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.creation.controllers;
import in.shop2020.content.security.Role;
import in.shop2020.content.security.UserManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import org.apache.struts2.convention.annotation.Result;

/**
 * 
 * @author rajveer
 * 
 */

@Result(name="success", type="redirectAction", params = {"actionName" , "${url}"})


public class LoginController extends BaseController {

        /**
         * 
         */
        private static final long serialVersionUID = 5390035354379263121L;
        private String url = "";


        
        public String index() throws SecurityException, IOException {
                return "index";
        }

        public String create() throws SecurityException, Exception {
                if (loginUser()) {
                        Role r = UserManager.getUserManager().getUserRole(getUsername());
                        if(new ArrayList<Role>(Arrays.asList(Role.ADMIN,Role.DEVELOPER, Role.EDITOR)).contains(r)) {
                                this.url = "entity";
                        }else {
                                this.url = "expert-review";
                        }
                        return "success";
                } else {
                        addActionError("Wrong username or password.");
                        return "index";
                }
        }

        private boolean loginUser() {
                try{
                        String username = this.reqparams.get("username")[0];
                        String password = this.reqparams.get("password")[0];
                        if(UserManager.getUserManager().authenticateUser(username, password)){
                                setUserAsLogin(username);
                                return true;
                        }
                }catch(Exception exp){
                        
                }
                return false;
        }
        
        public String getUrl(){
                return this.url;
        }

}