Subversion Repositories SmartDukaan

Rev

Rev 1153 | Go to most recent revision | 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);
41
		permissions.put(Role.EDITOR, editorActions);
42
 
1121 rajveer 43
		List<Action> adminActions = new ArrayList<Action>();
44
		adminActions.add(Action.EDIT);
45
		adminActions.add(Action.VIEW);
46
		adminActions.add(Action.CREATE);
47
		adminActions.add(Action.COMPLETE);
48
		adminActions.add(Action.ASSIGN);
49
		adminActions.add(Action.READY);
50
		adminActions.add(Action.DELETE);
7286 amit.gupta 51
		adminActions.add(Action.REVIEWADD);
52
		adminActions.add(Action.REVIEWADDREMOVE);
53
		adminActions.add(Action.REVIEWREMOVE);
54
		adminActions.add(Action.REVIEWPUBLISH);
55
		adminActions.add(Action.REVIEWARRAGE);
56
		adminActions.add(Action.REVIEWAPPROVE);
57
		adminActions.add(Action.REVIEWSOURCELISTADDDELETE);
1121 rajveer 58
		permissions.put(Role.ADMIN, adminActions);
7286 amit.gupta 59
 
60
		List<Action> reviewWriterActions = new ArrayList<Action>();
61
		reviewWriterActions.add(Action.REVIEWADD);
62
		reviewWriterActions.add(Action.REVIEWADDREMOVE);
63
		permissions.put(Role.REVIEWWRITER, reviewWriterActions);
64
 
65
		List<Action> reviewAdminActions = new ArrayList<Action>();
66
		reviewAdminActions.add(Action.REVIEWADD);
67
		reviewAdminActions.add(Action.REVIEWADDREMOVE);
68
		reviewAdminActions.add(Action.REVIEWREMOVE);
69
		reviewAdminActions.add(Action.REVIEWPUBLISH);
70
		reviewAdminActions.add(Action.REVIEWARRAGE);
71
		reviewAdminActions.add(Action.REVIEWAPPROVE);
72
		reviewAdminActions.add(Action.REVIEWSOURCELISTADDDELETE);
73
		permissions.put(Role.REVIEWADMIN, reviewAdminActions);
1050 rajveer 74
	}
75
 
1153 rajveer 76
	/**
77
	 * Get the singleton instance of role manager
78
	 * @return
79
	 */
1050 rajveer 80
	public static RoleManager getRoleManager(){
81
		return roleManager;
82
	}
83
 
1153 rajveer 84
	/**
85
	 * Create new role. If already exists, does nothing. 
86
	 * @param role
87
	 */
1050 rajveer 88
	public void createRole(Role role){
89
		if(!permissions.containsKey(role)){
90
			permissions.put(role, null);
91
		}
92
	}
93
 
1153 rajveer 94
	/**
95
	 * utility function to remove a role.
96
	 * 
97
	 * @param role
98
	 */
1050 rajveer 99
	public void dropRole(Role role){
100
		if(permissions.containsKey(role)){
101
			permissions.remove(role);
102
		}	
103
	}
104
 
1153 rajveer 105
	/**
106
	 * Method to grant permission for an action to an role
107
	 * 
108
	 * @param type role to which permission to be granted
109
	 * @param action action which to be added for role
110
	 */
1050 rajveer 111
	public void grantPermission(Role type, Action action){
112
		List<Action> actions = permissions.get(type);
113
		if(actions == null){
114
			actions = new ArrayList<Action>();
115
		}
116
		if(!actions.contains(action)){
117
			actions.add(action);
118
		}
119
	}
120
 
1153 rajveer 121
	/**
122
	 * utility function to remove permissions from a role.
123
	 * 
124
	 * @param type
125
	 * @param action
126
	 */
1050 rajveer 127
	public void revokePermission(Role type, Action action){
128
		List<Action> actions = permissions.get(type);
129
		if(actions != null){
130
			actions.remove(action);
131
		}
132
	}
133
 
1153 rajveer 134
 
1050 rajveer 135
	public boolean hasPermission(Role role, Action action){
136
		return permissions.get(role).contains(action);
137
	}
138
}