Subversion Repositories SmartDukaan

Rev

Rev 7068 | Rev 7113 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7068 Rev 7096
Line 1... Line 1...
1
/**
1
/**
2
 * 
2
 * 
3
 */
3
 */
4
package in.shop2020.recharge.controllers;
4
package in.shop2020.recharge.controllers;
5
 
5
 
6
import in.shop2020.serving.auth.InventoryAuthorizingRealm;
6
import in.shop2020.model.v1.order.HotspotStore;
7
import in.shop2020.thrift.clients.HelperClient;
7
import in.shop2020.thrift.clients.TransactionClient;
8
import in.shop2020.utils.HelperService.Client;
-
 
9
 
8
 
10
import org.apache.shiro.SecurityUtils;
-
 
11
import org.apache.shiro.crypto.hash.Sha256Hash;
9
import org.apache.struts2.convention.annotation.Action;
12
import org.apache.struts2.convention.annotation.Result;
10
import org.apache.struts2.convention.annotation.Result;
-
 
11
import org.apache.struts2.convention.annotation.Results;
13
import org.apache.thrift.TException;
12
import org.apache.thrift.TException;
-
 
13
import org.apache.thrift.transport.TTransportException;
14
 
14
 
15
/**
-
 
16
 * @author mandeep
15
@Results({
17
 * This class manages all the login related updates for a user.
16
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
18
 */
17
})
19
@Result(name="loginPage", location="/login.html")
-
 
20
public class LoginController extends BaseController {
-
 
21
 
18
 
22
    /**
-
 
23
     * 
19
public class LoginController extends BaseController {
24
     */
-
 
25
    private static final long serialVersionUID = 1L;
20
    private static final long serialVersionUID = 1L;
26
 
21
 
-
 
22
    private String storeIdString;
-
 
23
    private Long storeId;
-
 
24
    private String redirectUrl;
27
    private String password; 
25
    private String password; 
-
 
26
    private String message = "";
-
 
27
    private String hash;
28
 
28
 
-
 
29
    private long circleId;
-
 
30
    
-
 
31
    @Action("/")
29
    public String updatePassword() throws TException
32
    public String index()
30
    {
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
        storeIdString = (String) request.getSession().getAttribute("STORE_ID");
-
 
40
        if(storeIdString == null || storeIdString.isEmpty()){
-
 
41
            storeIdString = request.getParameter("storeid");
-
 
42
            if(storeIdString == null){
-
 
43
                return "authfail";
-
 
44
            }else{
-
 
45
                try {
31
        Client helperClient = new HelperClient().getClient();
46
                    TransactionClient tcl = new TransactionClient(); 
32
        helperClient.updatePasswordForAgent(SecurityUtils.getSubject().getPrincipal().toString(), new Sha256Hash(password).toHex());
47
                    HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(0, storeIdString);
-
 
48
                    if(!request.getParameter("hash").equals(hotSpotStore.getSalt())) {
-
 
49
                        return "authfail";
-
 
50
                    }
-
 
51
                    storeId = hotSpotStore.getId();
-
 
52
                    setHash(hotSpotStore.getSalt());
-
 
53
                    circleId = hotSpotStore.getCircleId();
-
 
54
                } catch (TTransportException e) {
-
 
55
                    e.printStackTrace();
-
 
56
                    return "authfail";
-
 
57
                } catch (TException e) {
-
 
58
                    e.printStackTrace();
-
 
59
                    return "authfail";
-
 
60
                }
-
 
61
                request.getSession().setAttribute("STORE_ID", "" + storeId);
-
 
62
                request.getSession().setAttribute("HASH", hash);
-
 
63
                request.getSession().setAttribute("CIRCLE_ID", "" + circleId);
-
 
64
            }
-
 
65
        } else {
33
        addActionMessage("Password changed successfully!");
66
            storeId = Long.parseLong(storeIdString);
-
 
67
        }
34
 
68
 
35
        // Dummy call to reload agents
-
 
36
        InventoryAuthorizingRealm.getAgent(-1);
-
 
37
        return INDEX;
69
        return INDEX;
38
    }
70
    }
39
 
71
 
-
 
72
    public String doLogin()
-
 
73
    {
-
 
74
        log.info("Logging in!");
-
 
75
        password = request.getParameter("password");
-
 
76
        storeIdString = (String) request.getSession().getAttribute("STORE_ID");
-
 
77
        if(storeIdString == null || storeIdString.isEmpty()){
-
 
78
            return "authfail";
-
 
79
        }
-
 
80
        try {
-
 
81
            TransactionClient tcl = new TransactionClient(); 
-
 
82
            HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(Long.parseLong(storeIdString), "");
-
 
83
            hash = (String) request.getSession().getAttribute("HASH");
-
 
84
            if(hash == null || !hash.equals(hotSpotStore.getSalt())) {
-
 
85
                return "authfail";
-
 
86
            }
-
 
87
            if(!hotSpotStore.getPassword().equals(password)){
-
 
88
                setMessage("Wrong Password. Try Again.");
-
 
89
                return INDEX;    
-
 
90
            }
-
 
91
        } catch (TTransportException e) {
-
 
92
            e.printStackTrace();
-
 
93
            return "authfail";
-
 
94
        } catch (TException e) {
-
 
95
            e.printStackTrace();
-
 
96
            return "authfail";
-
 
97
        }
-
 
98
        request.getSession().setAttribute("LOGGED_IN", "TRUE");
-
 
99
        redirectUrl = "/home";
-
 
100
        return "redirect";
-
 
101
    }
-
 
102
    
40
    public String logout()
103
    public String doLogout()
41
    {
104
    {
42
        log.info("Logging out!");
105
        log.info("Logging out!");
43
        SecurityUtils.getSubject().logout();
106
        request.getSession().setAttribute("LOGGED_IN", null);
44
        return "loginPage";
107
        return INDEX;
45
    }
108
    }
46
 
109
 
47
    public String getPassword() {
110
    public String getPassword() {
48
        return password;
111
        return password;
49
    }
112
    }
50
 
113
 
51
    public void setPassword(String password) {
114
    public void setPassword(String password) {
52
        this.password = password;
115
        this.password = password;
53
    }
116
    }
54
 
117
 
-
 
118
    public void setRedirectUrl(String redirectUrl) {
-
 
119
        this.redirectUrl = redirectUrl;
-
 
120
    }
-
 
121
 
-
 
122
    public String getRedirectUrl() {
-
 
123
        return redirectUrl;
-
 
124
    }
-
 
125
 
55
    public static void main(String[] args) {
126
    public void setMessage(String message) {
-
 
127
        this.message = message;
-
 
128
    }
-
 
129
 
56
        if (args != null && args.length != 0) {
130
    public String getMessage() {
-
 
131
        return message;
-
 
132
    }
-
 
133
 
57
            System.out.println(new Sha256Hash(args[0]).toHex());
134
    public void setHash(String hash) {
-
 
135
        this.hash = hash;
58
        }
136
    }
-
 
137
 
-
 
138
    public String getHash() {
-
 
139
        return hash;
59
    }
140
    }
60
}
141
}