Subversion Repositories SmartDukaan

Rev

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

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

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

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("/login")
    
    public String storeSession()
    {   
        log.info("Store session!");
        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 = (String) request.getSession().getAttribute("STORE_ID");
        if(storeIdString == null || storeIdString.isEmpty()){
            //If not found try to get the hotspotId from request
            //It is a bit confusing but "storeIdString" from the next line on means hotspot id
            storeIdString = request.getParameter("storeid");
            if(storeIdString == null){
                log.info("Empty store id string!");
                return "authfail";
            }else{
                //If we get the hotspot id then we fetch the store and store the store id in session to use on subsequent requests
                try {
                    TransactionClient tcl = new TransactionClient(); 
                    HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(0, storeIdString);
                    if(!request.getParameter("hash").equals(hotSpotStore.getSalt())) {
                        return "authfail";
                    }
                    storeId = hotSpotStore.getId();
                    setHash(hotSpotStore.getSalt());
                    circleId = hotSpotStore.getCircleId();
                } catch (TTransportException e) {
                    e.printStackTrace();
                    return "authfail";
                } catch (TException e) {
                    e.printStackTrace();
                    return "authfail";
                }
                request.getSession().setAttribute("STORE_ID", "" + storeId);
                request.getSession().setAttribute("HASH", hash);
                request.getSession().setAttribute("CIRCLE_ID", "" + circleId);
            }
        } else {
            //FIXME : This seems to be useless code
            storeId = Long.parseLong(storeIdString);
        }

        return INDEX;
    }
    
    @Action("/")
    
    public String doLogin()
    {
        log.info("Logging in!");
        password = request.getParameter("password");
        storeIdString = (String) request.getSession().getAttribute("STORE_ID");
        if(storeIdString == null || storeIdString.isEmpty()){
            return "authfail";
        }
        try {
            TransactionClient tcl = new TransactionClient(); 
            HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(Long.parseLong(storeIdString), "");
            hash = (String) request.getSession().getAttribute("HASH");
            if(hash == null || !hash.equals(hotSpotStore.getSalt())) {
                return "authfail";
            }
            if(!hotSpotStore.getPassword().equals(password)){
                setMessage("Wrong Password. Try Again.");
                return INDEX;    
            }
            request.getSession().setAttribute("STORE_CIRCLE_CODE", tcl.getClient().getTelecomCircle(hotSpotStore.getCircleId(), "").getCode());
            request.getSession().setAttribute("LOGGED_IN", "TRUE");
        } catch (TTransportException e) {
            e.printStackTrace();
            return "authfail";
        } catch (TException e) {
            e.printStackTrace();
            return "authfail";
        }
        redirectUrl = "/home";
        return "redirect";
    }
    
    @Action("/")
    
    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;
    }
}