| 1891 |
ankur.sing |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.thrift.clients.HelperServiceClient;
|
|
|
4 |
import in.shop2020.utils.Report;
|
|
|
5 |
import in.shop2020.utils.ReportUser;
|
|
|
6 |
|
|
|
7 |
import java.util.List;
|
|
|
8 |
|
|
|
9 |
import javax.servlet.ServletContext;
|
|
|
10 |
import javax.servlet.http.HttpServletRequest;
|
|
|
11 |
import javax.servlet.http.HttpServletResponse;
|
|
|
12 |
import javax.servlet.http.HttpSession;
|
|
|
13 |
|
|
|
14 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
15 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
16 |
import org.apache.struts2.util.ServletContextAware;
|
|
|
17 |
import org.apache.thrift.TException;
|
|
|
18 |
|
|
|
19 |
public class ReportsController implements ServletResponseAware, ServletRequestAware, ServletContextAware {
|
|
|
20 |
|
|
|
21 |
private HttpServletRequest request;
|
|
|
22 |
private HttpServletResponse response;
|
|
|
23 |
private HttpSession session;
|
|
|
24 |
private ServletContext context;
|
|
|
25 |
|
|
|
26 |
private String errorMsg = "";
|
|
|
27 |
private final String authsuccess = "authsuccess";
|
|
|
28 |
private final String authfail = "authfail";
|
|
|
29 |
private List<Report> reports;
|
|
|
30 |
|
|
|
31 |
private HelperServiceClient hsc;
|
|
|
32 |
private in.shop2020.utils.HelperService.Client client;
|
|
|
33 |
|
|
|
34 |
private long role;
|
|
|
35 |
private static final String USER_NAME = "username", PASSWORD = "password", ROLE = "role";
|
|
|
36 |
|
|
|
37 |
public ReportsController() {
|
|
|
38 |
try {
|
|
|
39 |
hsc = new HelperServiceClient();
|
|
|
40 |
client = hsc.getClient();
|
|
|
41 |
} catch (Exception e) {
|
|
|
42 |
e.printStackTrace();
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
@Override
|
|
|
47 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
48 |
this.request = req;
|
|
|
49 |
this.session = req.getSession();
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
public HttpServletRequest getServletRequest() {
|
|
|
53 |
return request;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
@Override
|
|
|
57 |
public void setServletResponse(HttpServletResponse res) {
|
|
|
58 |
this.response = res;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public HttpServletResponse getServletResponse() {
|
|
|
62 |
return response;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
@Override
|
|
|
66 |
public void setServletContext(ServletContext context) {
|
|
|
67 |
this.context = context;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
public String getServletContextPath() {
|
|
|
71 |
return context.getContextPath();
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
public String index() {
|
|
|
75 |
if (getSessionUserName() == null) {
|
|
|
76 |
return authfail;
|
|
|
77 |
} else {
|
|
|
78 |
loadReports();
|
|
|
79 |
return authsuccess;
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public boolean canAccessReport() {
|
|
|
84 |
String reportPath = request.getServletPath();
|
|
|
85 |
reportPath = reportPath.substring(1, reportPath.length());
|
|
|
86 |
if(reports == null) {
|
|
|
87 |
loadReports();
|
|
|
88 |
}
|
|
|
89 |
for(Report r : reports) {
|
|
|
90 |
if(r.getController().equals(reportPath))
|
|
|
91 |
return true;
|
|
|
92 |
}
|
|
|
93 |
return false;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
public void loadReports() {
|
|
|
97 |
try {
|
|
|
98 |
reports = client.getReports((Long)session.getAttribute(ROLE));
|
|
|
99 |
} catch (TException e) {
|
|
|
100 |
e.printStackTrace();
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
// Handles the POST request (Form Submission)
|
|
|
105 |
public String create() {
|
|
|
106 |
String username = request.getParameter(USER_NAME);
|
|
|
107 |
String password = request.getParameter(PASSWORD);
|
|
|
108 |
if (username != null && password != null) {
|
|
|
109 |
try {
|
|
|
110 |
ReportUser user = client.authenticateReportUser(username, password);
|
|
|
111 |
role = user.getRole();
|
|
|
112 |
session.setAttribute(USER_NAME, user.getUsername());
|
|
|
113 |
session.setAttribute(ROLE, role);
|
|
|
114 |
loadReports();
|
|
|
115 |
return authsuccess;
|
|
|
116 |
} catch (Exception e) {
|
|
|
117 |
e.printStackTrace();
|
|
|
118 |
errorMsg = "Invalid Username or Password. Please try again...";
|
|
|
119 |
return authfail;
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
if (getSessionUserName() == null) {
|
|
|
123 |
return authfail;
|
|
|
124 |
}
|
|
|
125 |
return authsuccess;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
public String getErrorMsg() {
|
|
|
129 |
return errorMsg;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
public String getSessionUserName() {
|
|
|
133 |
return (String) session.getAttribute(USER_NAME);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
public List<Report> getReports() {
|
|
|
137 |
return reports;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
public String returnLoginFail() {
|
|
|
141 |
return authfail;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
public boolean isLoggedIn() {
|
|
|
145 |
if (session.getAttribute(USER_NAME) != null) {
|
|
|
146 |
return true;
|
|
|
147 |
}
|
|
|
148 |
return false;
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
public void setUserAsLogout() {
|
|
|
152 |
session.setAttribute(USER_NAME, null);
|
|
|
153 |
session.setAttribute(ROLE, null);
|
|
|
154 |
}
|
|
|
155 |
}
|