Subversion Repositories SmartDukaan

Rev

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

/**
 * 
 */
package in.shop2020.serving.controllers;

import in.shop2020.model.v1.order.HotspotStore;
import in.shop2020.thrift.clients.TransactionClient;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;

@Results({
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
})

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

    private String storeIdString;
    private Long storeId;
    private String redirectUrl;
    private String password; 
    private String message = "";
    private String hash;

    private long circleId;
    
    @Action("/")
    public String index()
    {
        String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
        if(loginStatus != null && loginStatus.equals("TRUE")){
            redirectUrl = "/home";
            return "redirect";
        }
        //Try to get the stored store id from session
        storeIdString = request.getParameter("storeid");
        hash = request.getParameter("hash");
        //if store and hash(token) is available then validate. 
        if(!StringUtils.isEmpty(storeIdString) && !StringUtils.isEmpty(hash)){
                try {
                        //Validation logic here
                        TransactionClient tcl = new TransactionClient(); 
                HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(0, storeIdString);
                if(!request.getParameter("hash").equals(hotSpotStore.getSalt())) {
                    return "authfail";
                }
                storeId = hotSpotStore.getId();
                //Save to session 
                request.getSession().setAttribute("STORE_ID", "" + storeId);
                request.getSession().setAttribute("OCR_SESSION", "TRUE");
                request.getSession().setAttribute("LOGGED_IN", "TRUE");
                redirectUrl = "/home";
                return "redirect";

            } catch (TTransportException e) {
                e.printStackTrace();
                return "authfail";
            } catch (TException e) {
                e.printStackTrace();
                return "authfail";
            }
        }
        return INDEX;
    }

    public String doLogin()
    {
        log.info("Logging in!");
        password = request.getParameter("password");
        storeIdString = request.getParameter("storeid");
        if(StringUtils.isEmpty(storeIdString) || StringUtils.isEmpty(password)){
                setMessage("Store id or password is blank");
            return "INDEX";
        }
        try {
            TransactionClient tcl = new TransactionClient(); 
            HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(0, storeIdString);
            if(!hotSpotStore.getPassword().equals(password)){
                setMessage("Wrong Password. Try Again.");
                return INDEX;    
            }
            request.getSession().setAttribute("STORE_ID", "" + hotSpotStore.getId());
            request.getSession().setAttribute("LOGGED_IN", "TRUE");
        } catch (TTransportException e) {
            e.printStackTrace();
            return "authfail";
        } catch (TException e) {
            e.printStackTrace();
            return "authfail";
        }
        redirectUrl = "/home";
        return "redirect";
    }
    
    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 "logout";
    }

    public String getPassword() {
        return password;
    }

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

    public void setRedirectUrl(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }

    public String getRedirectUrl() {
        return redirectUrl;
    }

    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;
    }
}