Subversion Repositories SmartDukaan

Rev

Rev 7286 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
1153 rajveer 8
/**
9
 * Singleton class which stores permissions for a particular role. Utility functions for managing roles can be found here.
10
 * 
11
 * @author rajveer
12
 *
13
 */
1050 rajveer 14
public class RoleManager {
15
 
16
	private static RoleManager roleManager;
17
	private Map<Role, List<Action>> permissions;
18
 
1153 rajveer 19
 
1050 rajveer 20
	static{
21
		synchronized(RoleManager.class){
22
			roleManager = new RoleManager();
23
		}
24
	}
25
 
26
	private RoleManager(){
27
		//TODO get the object from bdb
28
		permissions = new HashMap<Role, List<Action>>();
29
		List<Action> developerActions = new ArrayList<Action>();
30
		developerActions.add(Action.EDIT);
31
		developerActions.add(Action.VIEW);
32
		developerActions.add(Action.COMPLETE);
33
		permissions.put(Role.DEVELOPER, developerActions);
34
 
35
		List<Action> editorActions = new ArrayList<Action>();
36
		editorActions.add(Action.EDIT);
37
		editorActions.add(Action.VIEW);
38
		editorActions.add(Action.COMPLETE);
39
		editorActions.add(Action.ASSIGN);
40
		editorActions.add(Action.READY);
19685 kshitij.so 41
		editorActions.add(Action.UPLOAD);
1050 rajveer 42
		permissions.put(Role.EDITOR, editorActions);
43
 
1121 rajveer 44
		List<Action> adminActions = new ArrayList<Action>();
45
		adminActions.add(Action.EDIT);
46
		adminActions.add(Action.VIEW);
47
		adminActions.add(Action.CREATE);
48
		adminActions.add(Action.COMPLETE);
49
		adminActions.add(Action.ASSIGN);
50
		adminActions.add(Action.READY);
51
		adminActions.add(Action.DELETE);
7286 amit.gupta 52
		adminActions.add(Action.REVIEWADD);
53
		adminActions.add(Action.REVIEWADDREMOVE);
54
		adminActions.add(Action.REVIEWREMOVE);
55
		adminActions.add(Action.REVIEWPUBLISH);
56
		adminActions.add(Action.REVIEWARRAGE);
57
		adminActions.add(Action.REVIEWAPPROVE);
58
		adminActions.add(Action.REVIEWSOURCELISTADDDELETE);
19685 kshitij.so 59
		adminActions.add(Action.UPLOAD);
1121 rajveer 60
		permissions.put(Role.ADMIN, adminActions);
7286 amit.gupta 61
 
62
		List<Action> reviewWriterActions = new ArrayList<Action>();
63
		reviewWriterActions.add(Action.REVIEWADD);
64
		reviewWriterActions.add(Action.REVIEWADDREMOVE);
65
		permissions.put(Role.REVIEWWRITER, reviewWriterActions);
66
 
67
		List<Action> reviewAdminActions = new ArrayList<Action>();
68
		reviewAdminActions.add(Action.REVIEWADD);
69
		reviewAdminActions.add(Action.REVIEWADDREMOVE);
70
		reviewAdminActions.add(Action.REVIEWREMOVE);
71
		reviewAdminActions.add(Action.REVIEWPUBLISH);
72
		reviewAdminActions.add(Action.REVIEWARRAGE);
73
		reviewAdminActions.add(Action.REVIEWAPPROVE);
74
		reviewAdminActions.add(Action.REVIEWSOURCELISTADDDELETE);
75
		permissions.put(Role.REVIEWADMIN, reviewAdminActions);
1050 rajveer 76
	}
77
 
1153 rajveer 78
	/**
79
	 * Get the singleton instance of role manager
80
	 * @return
81
	 */
1050 rajveer 82
	public static RoleManager getRoleManager(){
83
		return roleManager;
84
	}
85
 
1153 rajveer 86
	/**
87
	 * Create new role. If already exists, does nothing. 
88
	 * @param role
89
	 */
1050 rajveer 90
	public void createRole(Role role){
91
		if(!permissions.containsKey(role)){
92
			permissions.put(role, null);
93
		}
94
	}
95
 
1153 rajveer 96
	/**
97
	 * utility function to remove a role.
98
	 * 
99
	 * @param role
100
	 */
1050 rajveer 101
	public void dropRole(Role role){
102
		if(permissions.containsKey(role)){
103
			permissions.remove(role);
104
		}	
105
	}
106
 
1153 rajveer 107
	/**
108
	 * Method to grant permission for an action to an role
109
	 * 
110
	 * @param type role to which permission to be granted
111
	 * @param action action which to be added for role
112
	 */
1050 rajveer 113
	public void grantPermission(Role type, Action action){
114
		List<Action> actions = permissions.get(type);
115
		if(actions == null){
116
			actions = new ArrayList<Action>();
117
		}
118
		if(!actions.contains(action)){
119
			actions.add(action);
120
		}
121
	}
122
 
1153 rajveer 123
	/**
124
	 * utility function to remove permissions from a role.
125
	 * 
126
	 * @param type
127
	 * @param action
128
	 */
1050 rajveer 129
	public void revokePermission(Role type, Action action){
130
		List<Action> actions = permissions.get(type);
131
		if(actions != null){
132
			actions.remove(action);
133
		}
134
	}
135
 
1153 rajveer 136
 
1050 rajveer 137
	public boolean hasPermission(Role role, Action action){
138
		return permissions.get(role).contains(action);
139
	}
140
}