| 1050 |
rajveer |
1 |
package in.shop2020.content.security;
|
|
|
2 |
|
|
|
3 |
import java.util.ArrayList;
|
|
|
4 |
import java.util.HashMap;
|
|
|
5 |
import java.util.List;
|
|
|
6 |
import java.util.Map;
|
|
|
7 |
|
|
|
8 |
public class RoleManager {
|
|
|
9 |
|
|
|
10 |
private static RoleManager roleManager;
|
|
|
11 |
private Map<Role, List<Action>> permissions;
|
|
|
12 |
|
|
|
13 |
static{
|
|
|
14 |
synchronized(RoleManager.class){
|
|
|
15 |
roleManager = new RoleManager();
|
|
|
16 |
}
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
private RoleManager(){
|
|
|
20 |
//TODO get the object from bdb
|
|
|
21 |
permissions = new HashMap<Role, List<Action>>();
|
|
|
22 |
List<Action> developerActions = new ArrayList<Action>();
|
|
|
23 |
developerActions.add(Action.EDIT);
|
|
|
24 |
developerActions.add(Action.VIEW);
|
|
|
25 |
developerActions.add(Action.COMPLETE);
|
|
|
26 |
permissions.put(Role.DEVELOPER, developerActions);
|
|
|
27 |
|
|
|
28 |
List<Action> editorActions = new ArrayList<Action>();
|
|
|
29 |
editorActions.add(Action.EDIT);
|
|
|
30 |
editorActions.add(Action.VIEW);
|
|
|
31 |
editorActions.add(Action.COMPLETE);
|
|
|
32 |
editorActions.add(Action.ASSIGN);
|
|
|
33 |
editorActions.add(Action.READY);
|
|
|
34 |
permissions.put(Role.EDITOR, editorActions);
|
|
|
35 |
|
| 1121 |
rajveer |
36 |
List<Action> adminActions = new ArrayList<Action>();
|
|
|
37 |
adminActions.add(Action.EDIT);
|
|
|
38 |
adminActions.add(Action.VIEW);
|
|
|
39 |
adminActions.add(Action.CREATE);
|
|
|
40 |
adminActions.add(Action.COMPLETE);
|
|
|
41 |
adminActions.add(Action.ASSIGN);
|
|
|
42 |
adminActions.add(Action.READY);
|
|
|
43 |
adminActions.add(Action.DELETE);
|
|
|
44 |
permissions.put(Role.ADMIN, adminActions);
|
| 1050 |
rajveer |
45 |
}
|
|
|
46 |
|
|
|
47 |
public static RoleManager getRoleManager(){
|
|
|
48 |
return roleManager;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public void createRole(Role role){
|
|
|
52 |
if(!permissions.containsKey(role)){
|
|
|
53 |
permissions.put(role, null);
|
|
|
54 |
}
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public void dropRole(Role role){
|
|
|
58 |
if(permissions.containsKey(role)){
|
|
|
59 |
permissions.remove(role);
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
public void grantPermission(Role type, Action action){
|
|
|
64 |
List<Action> actions = permissions.get(type);
|
|
|
65 |
if(actions == null){
|
|
|
66 |
actions = new ArrayList<Action>();
|
|
|
67 |
}
|
|
|
68 |
if(!actions.contains(action)){
|
|
|
69 |
actions.add(action);
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
public void revokePermission(Role type, Action action){
|
|
|
74 |
List<Action> actions = permissions.get(type);
|
|
|
75 |
if(actions != null){
|
|
|
76 |
actions.remove(action);
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
public boolean hasPermission(Role role, Action action){
|
|
|
81 |
return permissions.get(role).contains(action);
|
|
|
82 |
}
|
|
|
83 |
}
|