Subversion Repositories SmartDukaan

Rev

Rev 1121 | Rev 7286 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1121 Rev 1153
Line 3... Line 3...
3
import java.util.ArrayList;
3
import java.util.ArrayList;
4
import java.util.HashMap;
4
import java.util.HashMap;
5
import java.util.List;
5
import java.util.List;
6
import java.util.Map;
6
import java.util.Map;
7
 
7
 
-
 
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
 */
8
public class RoleManager {
14
public class RoleManager {
9
	
15
	
10
	private static RoleManager roleManager;
16
	private static RoleManager roleManager;
11
	private Map<Role, List<Action>> permissions;
17
	private Map<Role, List<Action>> permissions;
12
	
18
	
-
 
19
	
13
	static{
20
	static{
14
		synchronized(RoleManager.class){
21
		synchronized(RoleManager.class){
15
			roleManager = new RoleManager();
22
			roleManager = new RoleManager();
16
		}
23
		}
17
	}
24
	}
Line 42... Line 49...
42
		adminActions.add(Action.READY);
49
		adminActions.add(Action.READY);
43
		adminActions.add(Action.DELETE);
50
		adminActions.add(Action.DELETE);
44
		permissions.put(Role.ADMIN, adminActions);
51
		permissions.put(Role.ADMIN, adminActions);
45
	}
52
	}
46
	
53
	
-
 
54
	/**
-
 
55
	 * Get the singleton instance of role manager
-
 
56
	 * @return
-
 
57
	 */
47
	public static RoleManager getRoleManager(){
58
	public static RoleManager getRoleManager(){
48
		return roleManager;
59
		return roleManager;
49
	}
60
	}
50
	
61
	
-
 
62
	/**
-
 
63
	 * Create new role. If already exists, does nothing. 
-
 
64
	 * @param role
-
 
65
	 */
51
	public void createRole(Role role){
66
	public void createRole(Role role){
52
		if(!permissions.containsKey(role)){
67
		if(!permissions.containsKey(role)){
53
			permissions.put(role, null);
68
			permissions.put(role, null);
54
		}
69
		}
55
	}
70
	}
56
	
71
	
-
 
72
	/**
-
 
73
	 * utility function to remove a role.
-
 
74
	 * 
-
 
75
	 * @param role
-
 
76
	 */
57
	public void dropRole(Role role){
77
	public void dropRole(Role role){
58
		if(permissions.containsKey(role)){
78
		if(permissions.containsKey(role)){
59
			permissions.remove(role);
79
			permissions.remove(role);
60
		}	
80
		}	
61
	}
81
	}
62
	
82
	
-
 
83
	/**
-
 
84
	 * Method to grant permission for an action to an role
-
 
85
	 * 
-
 
86
	 * @param type role to which permission to be granted
-
 
87
	 * @param action action which to be added for role
-
 
88
	 */
63
	public void grantPermission(Role type, Action action){
89
	public void grantPermission(Role type, Action action){
64
		List<Action> actions = permissions.get(type);
90
		List<Action> actions = permissions.get(type);
65
		if(actions == null){
91
		if(actions == null){
66
			actions = new ArrayList<Action>();
92
			actions = new ArrayList<Action>();
67
		}
93
		}
68
		if(!actions.contains(action)){
94
		if(!actions.contains(action)){
69
			actions.add(action);
95
			actions.add(action);
70
		}
96
		}
71
	}
97
	}
72
 
98
 
-
 
99
	/**
-
 
100
	 * utility function to remove permissions from a role.
-
 
101
	 * 
-
 
102
	 * @param type
-
 
103
	 * @param action
-
 
104
	 */
73
	public void revokePermission(Role type, Action action){
105
	public void revokePermission(Role type, Action action){
74
		List<Action> actions = permissions.get(type);
106
		List<Action> actions = permissions.get(type);
75
		if(actions != null){
107
		if(actions != null){
76
			actions.remove(action);
108
			actions.remove(action);
77
		}
109
		}
78
	}
110
	}
79
 
111
 
-
 
112
	
80
	public boolean hasPermission(Role role, Action action){
113
	public boolean hasPermission(Role role, Action action){
81
		return permissions.get(role).contains(action);
114
		return permissions.get(role).contains(action);
82
	}
115
	}
83
}
116
}