Subversion Repositories SmartDukaan

Rev

Rev 7235 | Rev 13174 | Go to most recent revision | 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
    {   
7096 anupam.sin 35
        String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
36
        if(loginStatus != null && loginStatus.equals("TRUE")){
37
            redirectUrl = "/home";
38
            return "redirect";
39
        }
7169 anupam.sin 40
        //Try to get the stored store id from session
7096 anupam.sin 41
        storeIdString = (String) request.getSession().getAttribute("STORE_ID");
42
        if(storeIdString == null || storeIdString.isEmpty()){
7169 anupam.sin 43
            //If not found try to get the hotspotId from request
44
            //It is a bit confusing but "storeIdString" from the next line on means hotspot id
7096 anupam.sin 45
            storeIdString = request.getParameter("storeid");
46
            if(storeIdString == null){
47
                return "authfail";
48
            }else{
7169 anupam.sin 49
                //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 50
                try {
51
                    TransactionClient tcl = new TransactionClient(); 
52
                    HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(0, storeIdString);
53
                    if(!request.getParameter("hash").equals(hotSpotStore.getSalt())) {
54
                        return "authfail";
55
                    }
56
                    storeId = hotSpotStore.getId();
57
                    setHash(hotSpotStore.getSalt());
58
                    circleId = hotSpotStore.getCircleId();
59
                } catch (TTransportException e) {
60
                    e.printStackTrace();
61
                    return "authfail";
62
                } catch (TException e) {
63
                    e.printStackTrace();
64
                    return "authfail";
65
                }
66
                request.getSession().setAttribute("STORE_ID", "" + storeId);
67
                request.getSession().setAttribute("HASH", hash);
68
                request.getSession().setAttribute("CIRCLE_ID", "" + circleId);
69
            }
70
        } else {
7169 anupam.sin 71
            //FIXME : This seems to be useless code
7096 anupam.sin 72
            storeId = Long.parseLong(storeIdString);
73
        }
7068 anupam.sin 74
 
75
        return INDEX;
76
    }
13173 kshitij.so 77
 
78
    @Action("/")
79
 
7096 anupam.sin 80
    public String doLogin()
7068 anupam.sin 81
    {
7096 anupam.sin 82
        log.info("Logging in!");
83
        password = request.getParameter("password");
84
        storeIdString = (String) request.getSession().getAttribute("STORE_ID");
85
        if(storeIdString == null || storeIdString.isEmpty()){
86
            return "authfail";
87
        }
88
        try {
89
            TransactionClient tcl = new TransactionClient(); 
90
            HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(Long.parseLong(storeIdString), "");
91
            hash = (String) request.getSession().getAttribute("HASH");
92
            if(hash == null || !hash.equals(hotSpotStore.getSalt())) {
93
                return "authfail";
94
            }
95
            if(!hotSpotStore.getPassword().equals(password)){
96
                setMessage("Wrong Password. Try Again.");
97
                return INDEX;    
98
            }
7125 amit.gupta 99
            request.getSession().setAttribute("STORE_CIRCLE_CODE", tcl.getClient().getTelecomCircle(hotSpotStore.getCircleId(), "").getCode());
7113 rajveer 100
            request.getSession().setAttribute("LOGGED_IN", "TRUE");
7096 anupam.sin 101
        } catch (TTransportException e) {
102
            e.printStackTrace();
103
            return "authfail";
104
        } catch (TException e) {
105
            e.printStackTrace();
106
            return "authfail";
107
        }
108
        redirectUrl = "/home";
109
        return "redirect";
110
    }
111
 
13173 kshitij.so 112
    @Action("/")
113
 
7096 anupam.sin 114
    public String doLogout()
115
    {
7068 anupam.sin 116
        log.info("Logging out!");
7096 anupam.sin 117
        request.getSession().setAttribute("LOGGED_IN", null);
7235 anupam.sin 118
        try {
119
            request.getSession().invalidate();
120
        } catch(IllegalStateException e) {
121
            log.error("Session is already invalidated", e);
122
        }
123
        return "logout";
7068 anupam.sin 124
    }
125
 
126
    public String getPassword() {
127
        return password;
128
    }
129
 
130
    public void setPassword(String password) {
131
        this.password = password;
132
    }
133
 
7096 anupam.sin 134
    public void setRedirectUrl(String redirectUrl) {
135
        this.redirectUrl = redirectUrl;
7068 anupam.sin 136
    }
7096 anupam.sin 137
 
138
    public String getRedirectUrl() {
139
        return redirectUrl;
140
    }
141
 
142
    public void setMessage(String message) {
143
        this.message = message;
144
    }
145
 
146
    public String getMessage() {
147
        return message;
148
    }
149
 
150
    public void setHash(String hash) {
151
        this.hash = hash;
152
    }
153
 
154
    public String getHash() {
155
        return hash;
156
    }
7068 anupam.sin 157
}