Subversion Repositories SmartDukaan

Rev

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

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