Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | 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
 
9
import org.apache.struts2.convention.annotation.Action;
10
import org.apache.struts2.convention.annotation.Result;
11
import org.apache.struts2.convention.annotation.Results;
12
import org.apache.thrift.TException;
13
import org.apache.thrift.transport.TTransportException;
14
 
15
@Results({
16
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
17
})
18
 
19
public class LoginController extends BaseController {
20
    private static final long serialVersionUID = 1L;
21
 
22
    private String storeIdString;
23
    private Long storeId;
24
    private String redirectUrl;
25
    private String password; 
26
    private String message = "";
27
    private String hash;
28
 
29
    private long circleId;
30
 
31
    @Action("/")
32
    public String index()
33
    {
34
        String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
35
        if(loginStatus != null && loginStatus.equals("TRUE")){
36
            redirectUrl = "/home";
37
            return "redirect";
38
        }
39
        //Try to get the stored store id from session
40
        storeIdString = (String) request.getSession().getAttribute("STORE_ID");
41
        if(storeIdString == null || storeIdString.isEmpty()){
42
            //If not found try to get the hotspotId from request
43
            //It is a bit confusing but "storeIdString" from the next line on means hotspot id
44
            storeIdString = request.getParameter("storeid");
45
            if(storeIdString == null){
46
                return "authfail";
47
            }else{
48
                //If we get the hotspot id then we fetch the store and store the store id in session to use on subsequent requests
49
                try {
50
                    TransactionClient tcl = new TransactionClient(); 
51
                    HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(0, storeIdString);
52
                    if(!request.getParameter("hash").equals(hotSpotStore.getSalt())) {
53
                        return "authfail";
54
                    }
55
                    storeId = hotSpotStore.getId();
56
                    setHash(hotSpotStore.getSalt());
57
                    circleId = hotSpotStore.getCircleId();
58
                } catch (TTransportException e) {
59
                    e.printStackTrace();
60
                    return "authfail";
61
                } catch (TException e) {
62
                    e.printStackTrace();
63
                    return "authfail";
64
                }
65
                request.getSession().setAttribute("STORE_ID", "" + storeId);
66
                request.getSession().setAttribute("HASH", hash);
67
                request.getSession().setAttribute("CIRCLE_ID", "" + circleId);
68
            }
69
        } else {
70
            //FIXME : This seems to be useless code
71
            storeId = Long.parseLong(storeIdString);
72
        }
73
 
74
        return INDEX;
75
    }
76
 
77
    public String doLogin()
78
    {
79
        log.info("Logging in!");
80
        password = request.getParameter("password");
81
        storeIdString = (String) request.getSession().getAttribute("STORE_ID");
82
        if(storeIdString == null || storeIdString.isEmpty()){
83
            return "authfail";
84
        }
85
        try {
86
            TransactionClient tcl = new TransactionClient(); 
87
            HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(Long.parseLong(storeIdString), "");
88
            hash = (String) request.getSession().getAttribute("HASH");
89
            if(hash == null || !hash.equals(hotSpotStore.getSalt())) {
90
                return "authfail";
91
            }
92
            if(!hotSpotStore.getPassword().equals(password)){
93
                setMessage("Wrong Password. Try Again.");
94
                return INDEX;    
95
            }
96
            request.getSession().setAttribute("STORE_CIRCLE_CODE", tcl.getClient().getTelecomCircle(hotSpotStore.getCircleId(), "").getCode());
97
            request.getSession().setAttribute("LOGGED_IN", "TRUE");
98
        } catch (TTransportException e) {
99
            e.printStackTrace();
100
            return "authfail";
101
        } catch (TException e) {
102
            e.printStackTrace();
103
            return "authfail";
104
        }
105
        redirectUrl = "/home";
106
        return "redirect";
107
    }
108
 
109
    public String doLogout()
110
    {
111
        log.info("Logging out!");
112
        request.getSession().setAttribute("LOGGED_IN", null);
113
        try {
114
            request.getSession().invalidate();
115
        } catch(IllegalStateException e) {
116
            log.error("Session is already invalidated", e);
117
        }
118
        return "logout";
119
    }
120
 
121
    public String getPassword() {
122
        return password;
123
    }
124
 
125
    public void setPassword(String password) {
126
        this.password = password;
127
    }
128
 
129
    public void setRedirectUrl(String redirectUrl) {
130
        this.redirectUrl = redirectUrl;
131
    }
132
 
133
    public String getRedirectUrl() {
134
        return redirectUrl;
135
    }
136
 
137
    public void setMessage(String message) {
138
        this.message = message;
139
    }
140
 
141
    public String getMessage() {
142
        return message;
143
    }
144
 
145
    public void setHash(String hash) {
146
        this.hash = hash;
147
    }
148
 
149
    public String getHash() {
150
        return hash;
151
    }
152
}