Subversion Repositories SmartDukaan

Rev

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

Rev 1061 Rev 1153
Line 4... Line 4...
4
import in.shop2020.metamodel.core.EntityStatus;
4
import in.shop2020.metamodel.core.EntityStatus;
5
import in.shop2020.storage.bdb.StorageManager;
5
import in.shop2020.storage.bdb.StorageManager;
6
import in.shop2020.util.Utils;
6
import in.shop2020.util.Utils;
7
 
7
 
8
import java.io.File;
8
import java.io.File;
9
import java.io.IOException;
-
 
10
import java.io.InputStreamReader;
-
 
11
import java.util.ArrayList;
9
import java.util.ArrayList;
12
import java.util.Collection;
10
import java.util.Collection;
13
import java.util.HashMap;
11
import java.util.HashMap;
14
import java.util.List;
12
import java.util.List;
15
import java.util.Map;
13
import java.util.Map;
16
import java.util.Set;
14
import java.util.Set;
17
 
15
 
18
import javax.xml.parsers.DocumentBuilder;
16
import javax.xml.parsers.DocumentBuilder;
19
import javax.xml.parsers.DocumentBuilderFactory;
17
import javax.xml.parsers.DocumentBuilderFactory;
20
import javax.xml.parsers.ParserConfigurationException;
-
 
21
import javax.xml.xpath.XPath;
18
import javax.xml.xpath.XPath;
22
import javax.xml.xpath.XPathConstants;
19
import javax.xml.xpath.XPathConstants;
23
import javax.xml.xpath.XPathExpressionException;
20
import javax.xml.xpath.XPathExpressionException;
24
import javax.xml.xpath.XPathFactory;
21
import javax.xml.xpath.XPathFactory;
25
 
22
 
26
import org.w3c.dom.Document;
23
import org.w3c.dom.Document;
27
import org.w3c.dom.Element;
24
import org.w3c.dom.Element;
28
import org.w3c.dom.NodeList;
25
import org.w3c.dom.NodeList;
29
import org.xml.sax.InputSource;
26
import org.xml.sax.InputSource;
30
import org.xml.sax.SAXException;
-
 
31
 
27
 
-
 
28
 
-
 
29
/**
-
 
30
 * Singleton class for managing users.
-
 
31
 * 
-
 
32
 * @author rajveer
-
 
33
 *
-
 
34
 */
32
public class UserManager {
35
public class UserManager {
33
	
36
	
34
	private static UserManager userManager;
37
	private static UserManager userManager;
35
	private Map<String, User> users;
38
	private Map<String, User> users;
36
	private String admin = "admin";
39
	private String admin = "admin";
Line 54... Line 57...
54
	
57
	
55
	public static void main(String[] args){
58
	public static void main(String[] args){
56
		System.out.println(UserManager.getUserManager().getAllUsers());
59
		System.out.println(UserManager.getUserManager().getAllUsers());
57
	}
60
	}
58
	
61
	
-
 
62
	/**
-
 
63
	 * Read the xml file and populate current users with their roles.
-
 
64
	 */
59
	private void loadUsers() {
65
	private void loadUsers() {
60
	       String xmlFile = Utils.CONTENT_DB_PATH + "definitions" + File.separator + "users.xml";
66
	       String xmlFile = Utils.CONTENT_DB_PATH + "definitions" + File.separator + "users.xml";
61
	       File file = new File(xmlFile);
67
	       File file = new File(xmlFile);
62
	       if(file.exists()){
68
	       if(file.exists()){
63
	    	   try{
69
	    	   try{
Line 97... Line 103...
97
	    	   }catch(Exception ex){
103
	    	   }catch(Exception ex){
98
	    		   
104
	    		   
99
	    	   }
105
	    	   }
100
	       }
106
	       }
101
	}
107
	}
-
 
108
	
-
 
109
	/**
-
 
110
	 * Method no more used.
-
 
111
	 */
102
	private void loadUsers1(){
112
	private void loadUsers1(){
103
		InputSource inputSource = new InputSource(Utils.CONTENT_DB_PATH + "definitions" + File.separator + "users.xml");
113
		InputSource inputSource = new InputSource(Utils.CONTENT_DB_PATH + "definitions" + File.separator + "users.xml");
104
		XPath xpath = XPathFactory.newInstance().newXPath();
114
		XPath xpath = XPathFactory.newInstance().newXPath();
105
		
115
		
106
		String expression = "/Definition";
116
		String expression = "/Definition";
Line 145... Line 155...
145
			users.put(name, user);
155
			users.put(name, user);
146
		}
156
		}
147
		
157
		
148
	}
158
	}
149
	
159
	
-
 
160
	/**
-
 
161
	 * 
-
 
162
	 * @return Singleton instance of user manager
150
	
163
	 */
151
	public static UserManager getUserManager(){
164
	public static UserManager getUserManager(){
152
		return userManager;
165
		return userManager;
153
	}
166
	}
154
	
167
	
-
 
168
	/**
-
 
169
	 * 
-
 
170
	 * @return All users with roles.
-
 
171
	 */
155
	public Collection<User> getAllUsers(){
172
	public Collection<User> getAllUsers(){
156
		Collection<User> allUsers = users.values();
173
		Collection<User> allUsers = users.values();
157
		return allUsers;
174
		return allUsers;
158
	}
175
	}
159
 
176
 
160
	
177
	
-
 
178
	/**
-
 
179
	 * 
-
 
180
	 * @return Names of all users.
-
 
181
	 */
161
	public Set<String> getAllUserNames(){
182
	public Set<String> getAllUserNames(){
162
		Set<String> allUsers = users.keySet();
183
		Set<String> allUsers = users.keySet();
163
		return allUsers;
184
		return allUsers;
164
	}
185
	}
165
 
186
 
-
 
187
	/**
-
 
188
	 * 
-
 
189
	 * @return Name of the admin.
-
 
190
	 */
166
	public String getAdminName(){
191
	public String getAdminName(){
167
		return this.admin;
192
		return this.admin;
168
	}
193
	}
169
	
194
	
-
 
195
	/**
-
 
196
	 * 
-
 
197
	 * @param role
-
 
198
	 * @return all users for a particular role
-
 
199
	 */
170
	public List<User> getAllUsersForRole(Role role){
200
	public List<User> getAllUsersForRole(Role role){
171
		Collection<User> allUsers = users.values();
201
		Collection<User> allUsers = users.values();
172
		List<User> usersForRole = new ArrayList<User>();
202
		List<User> usersForRole = new ArrayList<User>();
173
		for(User user: allUsers){
203
		for(User user: allUsers){
174
			if(user.getRole() == role){
204
			if(user.getRole() == role){
Line 177... Line 207...
177
		}
207
		}
178
		return usersForRole;
208
		return usersForRole;
179
	}
209
	}
180
	
210
	
181
	
211
	
-
 
212
	/**
-
 
213
	 * Create a new user.
-
 
214
	 * @param username
-
 
215
	 * @param password
-
 
216
	 * @param role
-
 
217
	 * @return false if user already exists else true 
-
 
218
	 */
182
	public boolean createUser(String username, String password, Role role){
219
	public boolean createUser(String username, String password, Role role){
183
		User user = new User(username, password, role);
220
		User user = new User(username, password, role);
184
		if(users.containsKey(username)){
221
		if(users.containsKey(username)){
185
			return false;
222
			return false;
186
		}
223
		}
187
		users.put(username, user);
224
		users.put(username, user);
188
		return true;
225
		return true;
189
	}
226
	}
190
	
227
	
-
 
228
	/**
-
 
229
	 * Authenticate user
-
 
230
	 * @param username
-
 
231
	 * @param password
-
 
232
	 * @return true if user is authenticated else false
-
 
233
	 */
191
	public boolean authenticateUser(String username, String password){
234
	public boolean authenticateUser(String username, String password){
192
		User user = users.get(username);
235
		User user = users.get(username);
193
		if(user != null && user.getPassword().equals(password)){
236
		if(user != null && user.getPassword().equals(password)){
194
			return true;	
237
			return true;	
195
		}
238
		}
196
		return false;
239
		return false;
197
	}
240
	}
198
 
241
 
-
 
242
	/**
-
 
243
	 * Get user for given username
-
 
244
	 * @param username
-
 
245
	 * @return User
-
 
246
	 */
199
	public User getUser(String username){
247
	public User getUser(String username){
200
		return users.get(username);
248
		return users.get(username);
201
	}
249
	}
202
 
250
 
-
 
251
	/**
-
 
252
	 * 
-
 
253
	 * @param username
-
 
254
	 * @return Role
-
 
255
	 */
203
	public Role getUserRole(String username){
256
	public Role getUserRole(String username){
204
		return users.get(username).getRole();
257
		return users.get(username).getRole();
205
	}
258
	}
206
 
259
 
-
 
260
	/**
-
 
261
	 * Checks weather user have permission to view the entity?
-
 
262
	 * 
-
 
263
	 * @param username
-
 
264
	 * @param entityId
-
 
265
	 * @return
-
 
266
	 */
207
	public boolean canView(String username, long entityId){
267
	public boolean canView(String username, long entityId){
208
		return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.VIEW);
268
		return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.VIEW);
209
	}
269
	}
210
	
270
	
-
 
271
	/**
-
 
272
	 * 
-
 
273
	 * @param username
-
 
274
	 * @param entityId
-
 
275
	 * @return
-
 
276
	 */
211
	public boolean canDelete(String username, long entityId){
277
	public boolean canDelete(String username, long entityId){
212
		if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.DELETE)){
278
		if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.DELETE)){
213
			EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
279
			EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
214
			if(state.getStatus() != EntityStatus.READY){
280
			if(state.getStatus() != EntityStatus.READY){
215
				return true;
281
				return true;
216
			}
282
			}
217
		}
283
		}
218
		return false;
284
		return false;
219
	}
285
	}
220
	
286
	
-
 
287
	/**
-
 
288
	 * 
-
 
289
	 * @param username
-
 
290
	 * @param entityId
-
 
291
	 * @return
-
 
292
	 */
221
	public boolean canEdit(String username, long entityId){
293
	public boolean canEdit(String username, long entityId){
222
		if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.EDIT)){
294
		if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.EDIT)){
223
			EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
295
			EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
224
			if(state.getStatus() ==  EntityStatus.ASSIGNED &&  state.getAssignedTo().equalsIgnoreCase(username)){
296
			if(state.getStatus() ==  EntityStatus.ASSIGNED &&  state.getAssignedTo().equalsIgnoreCase(username)){
225
				return true;
297
				return true;
226
			}
298
			}
227
		}
299
		}
228
		return false;
300
		return false;
229
	}
301
	}
230
	
302
	
-
 
303
	/**
-
 
304
	 * 
-
 
305
	 * @param username
-
 
306
	 * @param entityId
-
 
307
	 * @return
-
 
308
	 */
231
	public boolean canAssign(String username, long entityId){
309
	public boolean canAssign(String username, long entityId){
232
		if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.ASSIGN)){
310
		if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.ASSIGN)){
233
			EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
311
			EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
234
			if(state.getStatus() !=  EntityStatus.READY){
312
			if(state.getStatus() !=  EntityStatus.READY){
235
				return true;
313
				return true;
Line 240... Line 318...
240
			}
318
			}
241
		}
319
		}
242
		return false;
320
		return false;
243
	}
321
	}
244
	
322
	
-
 
323
	/**
-
 
324
	 * 
-
 
325
	 * @param username
-
 
326
	 * @param entityId
-
 
327
	 * @return
-
 
328
	 */
245
	public boolean canComplete(String username, long entityId){
329
	public boolean canComplete(String username, long entityId){
246
		if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.COMPLETE)){
330
		if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.COMPLETE)){
247
			EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
331
			EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
248
			if(state.getStatus() == EntityStatus.ASSIGNED && state.getAssignedTo().equalsIgnoreCase(username)){
332
			if(state.getStatus() == EntityStatus.ASSIGNED && state.getAssignedTo().equalsIgnoreCase(username)){
249
				return true;
333
				return true;
250
			}
334
			}
251
		}
335
		}
252
		return false;
336
		return false;
253
	}
337
	}
254
	
338
	
-
 
339
	/**
-
 
340
	 * 
-
 
341
	 * @param username
-
 
342
	 * @return
255
	
343
	 */
256
	public boolean canCreate(String username){
344
	public boolean canCreate(String username){
257
		return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.CREATE);
345
		return RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.CREATE);
258
	}
346
	}
259
	
347
	
-
 
348
	/**
-
 
349
	 * 
-
 
350
	 * @param username
-
 
351
	 * @param entityId
-
 
352
	 * @return
-
 
353
	 */
260
	public boolean canMarkReady(String username, long entityId){
354
	public boolean canMarkReady(String username, long entityId){
261
		if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.READY)){
355
		if(RoleManager.getRoleManager().hasPermission(userManager.getUserRole(username), Action.READY)){
262
		EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
356
		EntityState state = StorageManager.getStorageManager().getEntityMetadata(entityId);
263
			if(state.getStatus() ==  EntityStatus.COMPLETE){
357
			if(state.getStatus() ==  EntityStatus.COMPLETE){
264
				return true;
358
				return true;