Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
7386 anupam.sin 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
import in.shop2020.model.v1.order.HotspotStore;
7
import in.shop2020.thrift.clients.TransactionClient;
8
 
19706 amit.gupta 9
import org.apache.commons.lang.StringUtils;
7386 anupam.sin 10
import org.apache.struts2.convention.annotation.Action;
11
import org.apache.struts2.convention.annotation.Result;
12
import org.apache.struts2.convention.annotation.Results;
13
import org.apache.thrift.TException;
14
import org.apache.thrift.transport.TTransportException;
15
 
16
@Results({
17
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
18
})
19
 
20
public class LoginController extends BaseController {
21
    private static final long serialVersionUID = 1L;
22
 
23
    private String storeIdString;
24
    private Long storeId;
25
    private String redirectUrl;
26
    private String password; 
27
    private String message = "";
28
    private String hash;
29
 
30
    private long circleId;
31
 
32
    @Action("/")
33
    public String index()
34
    {
35
        String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
36
        if(loginStatus != null && loginStatus.equals("TRUE")){
37
            redirectUrl = "/home";
38
            return "redirect";
39
        }
40
        //Try to get the stored store id from session
19706 amit.gupta 41
        storeIdString = request.getParameter("storeid");
42
        hash = request.getParameter("hash");
43
        //if store and hash(token) is available then validate. 
44
        if(!StringUtils.isEmpty(storeIdString) && !StringUtils.isEmpty(hash)){
45
        	try {
46
        		//Validation logic here
47
        		TransactionClient tcl = new TransactionClient(); 
48
                HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(0, storeIdString);
49
                if(!request.getParameter("hash").equals(hotSpotStore.getSalt())) {
7386 anupam.sin 50
                    return "authfail";
51
                }
19706 amit.gupta 52
                storeId = hotSpotStore.getId();
53
                //Save to session 
7386 anupam.sin 54
                request.getSession().setAttribute("STORE_ID", "" + storeId);
19706 amit.gupta 55
                request.getSession().setAttribute("OCR_SESSION", "TRUE");
56
                request.getSession().setAttribute("LOGGED_IN", "TRUE");
57
                redirectUrl = "/home";
58
                return "redirect";
59
 
60
            } catch (TTransportException e) {
61
                e.printStackTrace();
62
                return "authfail";
63
            } catch (TException e) {
64
                e.printStackTrace();
65
                return "authfail";
7386 anupam.sin 66
            }
67
        }
68
        return INDEX;
69
    }
70
 
71
    public String doLogin()
72
    {
73
        log.info("Logging in!");
74
        password = request.getParameter("password");
19706 amit.gupta 75
        storeIdString = request.getParameter("storeid");
76
        if(StringUtils.isEmpty(storeIdString) || StringUtils.isEmpty(password)){
77
        	setMessage("Store id or password is blank");
78
            return "INDEX";
7386 anupam.sin 79
        }
80
        try {
81
            TransactionClient tcl = new TransactionClient(); 
19706 amit.gupta 82
            HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(0, storeIdString);
7386 anupam.sin 83
            if(!hotSpotStore.getPassword().equals(password)){
84
                setMessage("Wrong Password. Try Again.");
85
                return INDEX;    
86
            }
19706 amit.gupta 87
            request.getSession().setAttribute("STORE_ID", "" + hotSpotStore.getId());
7386 anupam.sin 88
            request.getSession().setAttribute("LOGGED_IN", "TRUE");
89
        } catch (TTransportException e) {
90
            e.printStackTrace();
91
            return "authfail";
92
        } catch (TException e) {
93
            e.printStackTrace();
94
            return "authfail";
95
        }
96
        redirectUrl = "/home";
97
        return "redirect";
98
    }
99
 
100
    public String doLogout()
101
    {
102
        log.info("Logging out!");
103
        request.getSession().setAttribute("LOGGED_IN", null);
104
        try {
105
            request.getSession().invalidate();
106
        } catch(IllegalStateException e) {
107
            log.error("Session is already invalidated", e);
108
        }
109
        return "logout";
110
    }
111
 
112
    public String getPassword() {
113
        return password;
114
    }
115
 
116
    public void setPassword(String password) {
117
        this.password = password;
118
    }
119
 
120
    public void setRedirectUrl(String redirectUrl) {
121
        this.redirectUrl = redirectUrl;
122
    }
123
 
124
    public String getRedirectUrl() {
125
        return redirectUrl;
126
    }
127
 
128
    public void setMessage(String message) {
129
        this.message = message;
130
    }
131
 
132
    public String getMessage() {
133
        return message;
134
    }
135
 
136
    public void setHash(String hash) {
137
        this.hash = hash;
138
    }
139
 
140
    public String getHash() {
141
        return hash;
142
    }
143
}