Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.recharge.controllers;

import in.shop2020.thrift.clients.HelperClient;
import in.shop2020.utils.HelperServiceException;
import in.shop2020.utils.ReportUser;

public class AdminController extends BaseController {
    private static final long serialVersionUID = 1L;

    private String password; 
    private String username;
    private String message = "";
    private String hash;
    
    public String index()
    {
        String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
        String role = (String) request.getSession().getAttribute("ROLE");
        if(loginStatus != null && role != null && loginStatus.equals("TRUE") && role.equals("ADMIN")){
            return "show";
        }
        return INDEX;
    }

    public String create()
    {
        log.info("Logging in!");
        password = request.getParameter("password");
        username = request.getParameter("username");
        if(username == null || username.isEmpty()){
            setMessage("Username/Password cannot be empty.");
            return "index";
        }
        
        if(password == null || password.isEmpty()){
            setMessage("Username/Password cannot be empty.");
            return "index";
        }
        
        try {
            HelperClient hcl = new HelperClient(); 
            ReportUser user = hcl.getClient().authenticateReportUser(username, password);
            if (user.getRole() == -1) {
                request.getSession().setAttribute("LOGGED_IN", "TRUE");
                request.getSession().setAttribute("ROLE", "ADMIN");
            } else {
                message = "Invalid Username/Password. Please try again";
                return "index";
            }
        } catch (HelperServiceException e) {
            log.error("Invalid user", e);
            message = "Invalid Username/Password. Please try again";
            return "index";
        } catch (Exception e) {
            log.error("Problem in helper service", e);
            message = "Internal error. Please try again or contact Engineering team";
            return "index";
        }
        return "show";
    }
    
    public String doLogout()
    {
        log.info("Logging out!");
        request.getSession().setAttribute("LOGGED_IN", null);
        try {
            request.getSession().invalidate();
        } catch(IllegalStateException e) {
            log.error("Session is already invalidated", e);
        }
        return "index";
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setHash(String hash) {
        this.hash = hash;
    }

    public String getHash() {
        return hash;
    }
}