Subversion Repositories SmartDukaan

Rev

Rev 7125 | Rev 7235 | 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
 
31
    @Action("/")
32
    public String index()
7068 anupam.sin 33
    {
7096 anupam.sin 34
        String loginStatus = (String) request.getSession().getAttribute("LOGGED_IN");
35
        if(loginStatus != null && loginStatus.equals("TRUE")){
36
            redirectUrl = "/home";
37
            return "redirect";
38
        }
7169 anupam.sin 39
        //Try to get the stored store id from session
7096 anupam.sin 40
        storeIdString = (String) request.getSession().getAttribute("STORE_ID");
41
        if(storeIdString == null || storeIdString.isEmpty()){
7169 anupam.sin 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
7096 anupam.sin 44
            storeIdString = request.getParameter("storeid");
45
            if(storeIdString == null){
46
                return "authfail";
47
            }else{
7169 anupam.sin 48
                //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 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 {
7169 anupam.sin 70
            //FIXME : This seems to be useless code
7096 anupam.sin 71
            storeId = Long.parseLong(storeIdString);
72
        }
7068 anupam.sin 73
 
74
        return INDEX;
75
    }
76
 
7096 anupam.sin 77
    public String doLogin()
7068 anupam.sin 78
    {
7096 anupam.sin 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
            }
7125 amit.gupta 96
            request.getSession().setAttribute("STORE_CIRCLE_CODE", tcl.getClient().getTelecomCircle(hotSpotStore.getCircleId(), "").getCode());
7113 rajveer 97
            request.getSession().setAttribute("LOGGED_IN", "TRUE");
7096 anupam.sin 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
    {
7068 anupam.sin 111
        log.info("Logging out!");
7096 anupam.sin 112
        request.getSession().setAttribute("LOGGED_IN", null);
113
        return INDEX;
7068 anupam.sin 114
    }
115
 
116
    public String getPassword() {
117
        return password;
118
    }
119
 
120
    public void setPassword(String password) {
121
        this.password = password;
122
    }
123
 
7096 anupam.sin 124
    public void setRedirectUrl(String redirectUrl) {
125
        this.redirectUrl = redirectUrl;
7068 anupam.sin 126
    }
7096 anupam.sin 127
 
128
    public String getRedirectUrl() {
129
        return redirectUrl;
130
    }
131
 
132
    public void setMessage(String message) {
133
        this.message = message;
134
    }
135
 
136
    public String getMessage() {
137
        return message;
138
    }
139
 
140
    public void setHash(String hash) {
141
        this.hash = hash;
142
    }
143
 
144
    public String getHash() {
145
        return hash;
146
    }
7068 anupam.sin 147
}