| 1050 |
rajveer |
1 |
package in.shop2020.content.security;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.metamodel.core.EntityState;
|
|
|
4 |
import in.shop2020.metamodel.core.EntityStatus;
|
| 8749 |
amit.gupta |
5 |
import in.shop2020.metamodel.util.CreationUtils;
|
| 1050 |
rajveer |
6 |
import in.shop2020.util.Utils;
|
|
|
7 |
|
|
|
8 |
import java.io.File;
|
|
|
9 |
import java.util.ArrayList;
|
| 8493 |
amit.gupta |
10 |
import java.util.Calendar;
|
| 1050 |
rajveer |
11 |
import java.util.Collection;
|
|
|
12 |
import java.util.HashMap;
|
|
|
13 |
import java.util.List;
|
|
|
14 |
import java.util.Map;
|
|
|
15 |
import java.util.Set;
|
|
|
16 |
|
|
|
17 |
import javax.xml.parsers.DocumentBuilder;
|
|
|
18 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
19 |
import javax.xml.xpath.XPath;
|
|
|
20 |
import javax.xml.xpath.XPathConstants;
|
|
|
21 |
import javax.xml.xpath.XPathExpressionException;
|
|
|
22 |
import javax.xml.xpath.XPathFactory;
|
|
|
23 |
|
|
|
24 |
import org.w3c.dom.Document;
|
|
|
25 |
import org.w3c.dom.Element;
|
|
|
26 |
import org.w3c.dom.NodeList;
|
|
|
27 |
import org.xml.sax.InputSource;
|
|
|
28 |
|
| 1153 |
rajveer |
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Singleton class for managing users.
|
|
|
32 |
*
|
|
|
33 |
* @author rajveer
|
|
|
34 |
*
|
|
|
35 |
*/
|
| 1050 |
rajveer |
36 |
public class UserManager {
|
|
|
37 |
|
|
|
38 |
private static UserManager userManager;
|
|
|
39 |
private Map<String, User> users;
|
|
|
40 |
private String admin = "admin";
|
|
|
41 |
static{
|
|
|
42 |
synchronized(UserManager.class){
|
|
|
43 |
userManager = new UserManager();
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
private UserManager(){
|
|
|
48 |
//TODO Load initial list of users
|
|
|
49 |
users = new HashMap<String, User>();
|
|
|
50 |
this.loadUsers();
|
|
|
51 |
// System.out.println(users);
|
|
|
52 |
// users.put("vrinda", new User("vrinda", "vrinda", Role.EDITOR));
|
|
|
53 |
// users.put("smriti", new User("smriti", "smriti", Role.DEVELOPER));
|
|
|
54 |
// users.put("priyanka", new User("priyanka", "priyanka", Role.DEVELOPER));
|
|
|
55 |
// users.put("rajveer", new User("rajveer", "rajveer", Role.ADMIN));
|
|
|
56 |
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public static void main(String[] args){
|
|
|
60 |
System.out.println(UserManager.getUserManager().getAllUsers());
|
|
|
61 |
}
|
|
|
62 |
|
| 1153 |
rajveer |
63 |
/**
|
|
|
64 |
* Read the xml file and populate current users with their roles.
|
|
|
65 |
*/
|
| 1050 |
rajveer |
66 |
private void loadUsers() {
|
|
|
67 |
String xmlFile = Utils.CONTENT_DB_PATH + "definitions" + File.separator + "users.xml";
|
|
|
68 |
File file = new File(xmlFile);
|
|
|
69 |
if(file.exists()){
|
|
|
70 |
try{
|
|
|
71 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
72 |
DocumentBuilder builder = factory.newDocumentBuilder();
|
|
|
73 |
Document doc = builder.parse(xmlFile);
|
|
|
74 |
NodeList list = doc.getElementsByTagName("User");
|
|
|
75 |
for(int i=0; i<list.getLength(); i++) {
|
|
|
76 |
Element element = (Element)list.item(i);
|
|
|
77 |
String name = element.getElementsByTagName("Username").item(0).getTextContent();
|
|
|
78 |
String password = element.getElementsByTagName("Password").item(0).getTextContent();
|
|
|
79 |
int roleId = Integer.parseInt(element.getElementsByTagName("Role").item(0).getTextContent());
|
|
|
80 |
|
|
|
81 |
Role role = Role.DEVELOPER;
|
|
|
82 |
switch (roleId) {
|
|
|
83 |
case 0:
|
|
|
84 |
role = Role.DEVELOPER;
|
|
|
85 |
break;
|
|
|
86 |
|
|
|
87 |
case 1:
|
|
|
88 |
role = Role.EDITOR;
|
|
|
89 |
break;
|
|
|
90 |
|
|
|
91 |
case 2:
|
|
|
92 |
role = Role.ADMIN;
|
|
|
93 |
admin = name;
|
|
|
94 |
break;
|
| 7286 |
amit.gupta |
95 |
|
|
|
96 |
case 3:
|
|
|
97 |
role = Role.REVIEWWRITER;
|
|
|
98 |
break;
|
|
|
99 |
|
|
|
100 |
case 4:
|
|
|
101 |
role = Role.REVIEWADMIN;
|
|
|
102 |
break;
|
| 1050 |
rajveer |
103 |
default:
|
|
|
104 |
break;
|
|
|
105 |
}
|
|
|
106 |
User user = new User(name, password, role);
|
|
|
107 |
users.put(name, user);
|
|
|
108 |
System.out.println(name +password + role);
|
|
|
109 |
|
|
|
110 |
}
|
|
|
111 |
}catch(Exception ex){
|
|
|
112 |
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
}
|
| 1153 |
rajveer |
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Method no more used.
|
|
|
119 |
*/
|
| 1050 |
rajveer |
120 |
private void loadUsers1(){
|
|
|
121 |
InputSource inputSource = new InputSource(Utils.CONTENT_DB_PATH + "definitions" + File.separator + "users.xml");
|
|
|
122 |
XPath xpath = XPathFactory.newInstance().newXPath();
|
|
|
123 |
|
|
|
124 |
String expression = "/Definition";
|
|
|
125 |
NodeList nodes = null;
|
|
|
126 |
try {
|
|
|
127 |
nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
|
|
|
128 |
} catch(XPathExpressionException xpee) {
|
|
|
129 |
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
System.out.println(nodes);
|
|
|
133 |
|
|
|
134 |
for(int i=nodes.getLength()-1; i>=0; i--) {
|
|
|
135 |
System.out.println(nodes.item(i));
|
|
|
136 |
|
|
|
137 |
System.out.println(nodes.getLength());
|
|
|
138 |
|
|
|
139 |
Element userInfo = (Element) nodes.item(i);
|
|
|
140 |
|
|
|
141 |
String name = userInfo.getElementsByTagName("Username").item(0).getTextContent();
|
|
|
142 |
String password = userInfo.getElementsByTagName("Password").item(0).getTextContent();
|
|
|
143 |
int roleId = Integer.parseInt(userInfo.getElementsByTagName("role").item(0).getTextContent());
|
|
|
144 |
Role role = Role.DEVELOPER;
|
|
|
145 |
|
|
|
146 |
switch (roleId) {
|
|
|
147 |
case 0:
|
|
|
148 |
role = Role.DEVELOPER;
|
|
|
149 |
break;
|
|
|
150 |
|
|
|
151 |
case 1:
|
|
|
152 |
role = Role.EDITOR;
|
|
|
153 |
break;
|
|
|
154 |
|
|
|
155 |
case 2:
|
|
|
156 |
role = Role.ADMIN;
|
|
|
157 |
break;
|
|
|
158 |
|
|
|
159 |
default:
|
|
|
160 |
break;
|
|
|
161 |
}
|
|
|
162 |
User user = new User(name, password, role);
|
|
|
163 |
users.put(name, user);
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
}
|
|
|
167 |
|
| 1153 |
rajveer |
168 |
/**
|
|
|
169 |
*
|
|
|
170 |
* @return Singleton instance of user manager
|
|
|
171 |
*/
|
| 1050 |
rajveer |
172 |
public static UserManager getUserManager(){
|
|
|
173 |
return userManager;
|
|
|
174 |
}
|
|
|
175 |
|
| 1153 |
rajveer |
176 |
/**
|
|
|
177 |
*
|
|
|
178 |
* @return All users with roles.
|
|
|
179 |
*/
|
| 1050 |
rajveer |
180 |
public Collection<User> getAllUsers(){
|
|
|
181 |
Collection<User> allUsers = users.values();
|
|
|
182 |
return allUsers;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
|
| 1153 |
rajveer |
186 |
/**
|
|
|
187 |
*
|
|
|
188 |
* @return Names of all users.
|
|
|
189 |
*/
|
| 1050 |
rajveer |
190 |
public Set<String> getAllUserNames(){
|
|
|
191 |
Set<String> allUsers = users.keySet();
|
|
|
192 |
return allUsers;
|
|
|
193 |
}
|
|
|
194 |
|
| 1153 |
rajveer |
195 |
/**
|
|
|
196 |
*
|
|
|
197 |
* @return Name of the admin.
|
|
|
198 |
*/
|
| 1050 |
rajveer |
199 |
public String getAdminName(){
|
|
|
200 |
return this.admin;
|
|
|
201 |
}
|
|
|
202 |
|
| 1153 |
rajveer |
203 |
/**
|
|
|
204 |
*
|
|
|
205 |
* @param role
|
|
|
206 |
* @return all users for a particular role
|
|
|
207 |
*/
|
| 1050 |
rajveer |
208 |
public List<User> getAllUsersForRole(Role role){
|
|
|
209 |
Collection<User> allUsers = users.values();
|
|
|
210 |
List<User> usersForRole = new ArrayList<User>();
|
|
|
211 |
for(User user: allUsers){
|
|
|
212 |
if(user.getRole() == role){
|
|
|
213 |
usersForRole.add(user);
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
return usersForRole;
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
|
| 1153 |
rajveer |
220 |
/**
|
|
|
221 |
* Create a new user.
|
|
|
222 |
* @param username
|
|
|
223 |
* @param password
|
|
|
224 |
* @param role
|
|
|
225 |
* @return false if user already exists else true
|
|
|
226 |
*/
|
| 1050 |
rajveer |
227 |
public boolean createUser(String username, String password, Role role){
|
|
|
228 |
User user = new User(username, password, role);
|
|
|
229 |
if(users.containsKey(username)){
|
|
|
230 |
return false;
|
|
|
231 |
}
|
|
|
232 |
users.put(username, user);
|
|
|
233 |
return true;
|
|
|
234 |
}
|
|
|
235 |
|
| 1153 |
rajveer |
236 |
/**
|
|
|
237 |
* Authenticate user
|
|
|
238 |
* @param username
|
|
|
239 |
* @param password
|
|
|
240 |
* @return true if user is authenticated else false
|
|
|
241 |
*/
|
| 1050 |
rajveer |
242 |
public boolean authenticateUser(String username, String password){
|
|
|
243 |
User user = users.get(username);
|
|
|
244 |
if(user != null && user.getPassword().equals(password)){
|
|
|
245 |
return true;
|
|
|
246 |
}
|
|
|
247 |
return false;
|
|
|
248 |
}
|
|
|
249 |
|
| 1153 |
rajveer |
250 |
/**
|
|
|
251 |
* Get user for given username
|
|
|
252 |
* @param username
|
|
|
253 |
* @return User
|
|
|
254 |
*/
|
| 1050 |
rajveer |
255 |
public User getUser(String username){
|
|
|
256 |
return users.get(username);
|
|
|
257 |
}
|
|
|
258 |
|
| 1153 |
rajveer |
259 |
/**
|
|
|
260 |
*
|
|
|
261 |
* @param username
|
|
|
262 |
* @return Role
|
|
|
263 |
*/
|
| 1050 |
rajveer |
264 |
public Role getUserRole(String username){
|
| 8493 |
amit.gupta |
265 |
Role role = users.get(username).getRole();
|
|
|
266 |
if(Calendar.getInstance().get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
|
|
|
267 |
if(Role.DEVELOPER.equals(role)){
|
|
|
268 |
return Role.EDITOR;
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
return role;
|
| 1050 |
rajveer |
272 |
}
|
|
|
273 |
|
| 1153 |
rajveer |
274 |
/**
|
|
|
275 |
* Checks weather user have permission to view the entity?
|
|
|
276 |
*
|
|
|
277 |
* @param username
|
|
|
278 |
* @param entityId
|
|
|
279 |
* @return
|
|
|
280 |
*/
|
| 1050 |
rajveer |
281 |
public boolean canView(String username, long entityId){
|
|
|
282 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.VIEW);
|
|
|
283 |
}
|
|
|
284 |
|
| 1153 |
rajveer |
285 |
/**
|
|
|
286 |
*
|
|
|
287 |
* @param username
|
|
|
288 |
* @param entityId
|
|
|
289 |
* @return
|
|
|
290 |
*/
|
| 1050 |
rajveer |
291 |
public boolean canDelete(String username, long entityId){
|
|
|
292 |
if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.DELETE)){
|
| 7362 |
amit.gupta |
293 |
//EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
|
|
|
294 |
//if(state.getStatus() != EntityStatus.READY){
|
| 1050 |
rajveer |
295 |
return true;
|
| 7362 |
amit.gupta |
296 |
//}
|
| 1050 |
rajveer |
297 |
}
|
|
|
298 |
return false;
|
|
|
299 |
}
|
|
|
300 |
|
| 1153 |
rajveer |
301 |
/**
|
|
|
302 |
*
|
|
|
303 |
* @param username
|
|
|
304 |
* @param entityId
|
|
|
305 |
* @return
|
| 8749 |
amit.gupta |
306 |
* @throws Exception
|
| 1153 |
rajveer |
307 |
*/
|
| 8749 |
amit.gupta |
308 |
public boolean canEdit(String username, long entityId) throws Exception{
|
| 1050 |
rajveer |
309 |
if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.EDIT)){
|
| 8749 |
amit.gupta |
310 |
EntityState state = CreationUtils.getEntityState(entityId);
|
| 1050 |
rajveer |
311 |
if(state.getStatus() == EntityStatus.ASSIGNED && state.getAssignedTo().equalsIgnoreCase(username)){
|
|
|
312 |
return true;
|
|
|
313 |
}
|
|
|
314 |
}
|
|
|
315 |
return false;
|
|
|
316 |
}
|
|
|
317 |
|
| 1153 |
rajveer |
318 |
/**
|
|
|
319 |
*
|
|
|
320 |
* @param username
|
|
|
321 |
* @param entityId
|
|
|
322 |
* @return
|
|
|
323 |
*/
|
| 1050 |
rajveer |
324 |
public boolean canAssign(String username, long entityId){
|
|
|
325 |
if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.ASSIGN)){
|
| 2022 |
rajveer |
326 |
return true;
|
|
|
327 |
/* Now content editor will be able to change status from ready to non ready
|
| 1050 |
rajveer |
328 |
EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
|
|
|
329 |
if(state.getStatus() != EntityStatus.READY){
|
|
|
330 |
return true;
|
| 2022 |
rajveer |
331 |
}
|
|
|
332 |
else{
|
| 1050 |
rajveer |
333 |
if(userManager.getUserRole(username) == Role.ADMIN){
|
|
|
334 |
return true;
|
|
|
335 |
}
|
|
|
336 |
}
|
| 2022 |
rajveer |
337 |
*/
|
| 1050 |
rajveer |
338 |
}
|
|
|
339 |
return false;
|
|
|
340 |
}
|
|
|
341 |
|
| 1153 |
rajveer |
342 |
/**
|
|
|
343 |
*
|
|
|
344 |
* @param username
|
|
|
345 |
* @param entityId
|
|
|
346 |
* @return
|
|
|
347 |
*/
|
| 1050 |
rajveer |
348 |
public boolean canComplete(String username, long entityId){
|
|
|
349 |
if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.COMPLETE)){
|
| 8749 |
amit.gupta |
350 |
//EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
|
|
|
351 |
EntityState state = CreationUtils.getEntityState(entityId);
|
| 1050 |
rajveer |
352 |
if(state.getStatus() == EntityStatus.ASSIGNED && state.getAssignedTo().equalsIgnoreCase(username)){
|
|
|
353 |
return true;
|
|
|
354 |
}
|
|
|
355 |
}
|
|
|
356 |
return false;
|
|
|
357 |
}
|
|
|
358 |
|
| 1153 |
rajveer |
359 |
/**
|
|
|
360 |
*
|
|
|
361 |
* @param username
|
|
|
362 |
* @return
|
|
|
363 |
*/
|
| 1050 |
rajveer |
364 |
public boolean canCreate(String username){
|
|
|
365 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.CREATE);
|
|
|
366 |
}
|
|
|
367 |
|
| 1153 |
rajveer |
368 |
/**
|
|
|
369 |
*
|
|
|
370 |
* @param username
|
|
|
371 |
* @param entityId
|
|
|
372 |
* @return
|
|
|
373 |
*/
|
| 1050 |
rajveer |
374 |
public boolean canMarkReady(String username, long entityId){
|
|
|
375 |
if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.READY)){
|
| 8749 |
amit.gupta |
376 |
//EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
|
|
|
377 |
EntityState state = CreationUtils.getEntityState(entityId);
|
| 1050 |
rajveer |
378 |
if(state.getStatus() == EntityStatus.COMPLETE){
|
|
|
379 |
return true;
|
|
|
380 |
}
|
|
|
381 |
}
|
|
|
382 |
return false;
|
|
|
383 |
}
|
|
|
384 |
|
| 7286 |
amit.gupta |
385 |
public boolean canApproveReview(String username){
|
|
|
386 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.REVIEWAPPROVE);
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
public boolean canPublishReview(String username){
|
|
|
390 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.REVIEWPUBLISH);
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
public boolean canPhaseOutReview(String username){
|
|
|
394 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.REVIEWPHASEDOUT);
|
|
|
395 |
}
|
|
|
396 |
|
|
|
397 |
public boolean canCreateReview(String username){
|
|
|
398 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.REVIEWADD);
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
public boolean canAddRemoveReview(String username){
|
|
|
402 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.REVIEWADDREMOVE);
|
|
|
403 |
}
|
|
|
404 |
|
|
|
405 |
public boolean canRemoveReview(String username){
|
|
|
406 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.REVIEWREMOVE);
|
|
|
407 |
}
|
|
|
408 |
|
|
|
409 |
public boolean canOrderReview(String username){
|
|
|
410 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.REVIEWREMOVE);
|
|
|
411 |
}
|
|
|
412 |
|
|
|
413 |
public boolean canAddDeleteReviewSourceList(String username){
|
|
|
414 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.REVIEWSOURCELISTADDDELETE);
|
|
|
415 |
}
|
|
|
416 |
|
|
|
417 |
|
|
|
418 |
|
| 1050 |
rajveer |
419 |
/*
|
|
|
420 |
public boolean canView(String username, long entityId){
|
|
|
421 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.VIEW);
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
public boolean canEdit(String username, long entityId){
|
|
|
425 |
if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.EDIT)){
|
|
|
426 |
EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
|
|
|
427 |
if(state.getStatus() == EntityStatus.)
|
|
|
428 |
}
|
|
|
429 |
return false;
|
|
|
430 |
}
|
|
|
431 |
|
|
|
432 |
public boolean canDelete(String username, long entityId){
|
|
|
433 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.DELETE);
|
|
|
434 |
}
|
|
|
435 |
|
|
|
436 |
public boolean canAssign(String username, long entityId){
|
|
|
437 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.ASSIGN);
|
|
|
438 |
}
|
|
|
439 |
public boolean canComplete(String username, long entityId){
|
|
|
440 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.COMPLETE);
|
|
|
441 |
}
|
|
|
442 |
public boolean canMarkReady(String username, long entityId){
|
|
|
443 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.READY);
|
|
|
444 |
}
|
|
|
445 |
public boolean canCreate(String username, long entityId){
|
|
|
446 |
return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.CREATE);
|
|
|
447 |
}
|
|
|
448 |
*/
|
|
|
449 |
}
|