KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > client > util > UserUtilities


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.client.util;
20
21 import java.io.*;
22 import java.security.*;
23 import java.util.*;
24 import sun.misc.BASE64Encoder;
25 import sun.misc.CharacterEncoder;
26
27 import cowsultants.itracker.ejb.client.exceptions.*;
28 import cowsultants.itracker.ejb.client.models.*;
29 import cowsultants.itracker.ejb.client.resources.*;
30
31 public class UserUtilities implements AuthenticationConstants {
32     public static final char[] alphabet = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
33
34     public static final int STATUS_DELETED = -1;
35     public static final int STATUS_ACTIVE = 1;
36     public static final int STATUS_LOCKED = 2;
37
38     /** User Admin Permission. Currently this is equivalent to super user, since the permission can't be granted, and is only available to an admin. */
39     public static final int PERMISSION_USER_ADMIN = -1;
40     /** Product Admin Permission */
41     public static final int PERMISSION_PRODUCT_ADMIN = 1;
42     /** Issue Create Permission */
43     public static final int PERMISSION_CREATE = 2;
44     /** Issue Edit Permission. Users with this permission can edit any issue in the project. */
45     public static final int PERMISSION_EDIT = 3;
46     /** Issue Close Permission. Users with this permission can close issues in the project. */
47     public static final int PERMISSION_CLOSE = 4;
48     /** Issue Assign to Self Permission. Users with this permission can assign issues to themselves. */
49     public static final int PERMISSION_ASSIGN_SELF = 5;
50     /** Issue Assign to Others Permissions. Users with this permission can assign issues to anyone, given than those users have the ability to recieve the assignment. */
51     public static final int PERMISSION_ASSIGN_OTHERS = 6;
52     /** View All Issues Permission. Users can view all issues in the project. */
53     public static final int PERMISSION_VIEW_ALL = 7;
54     /** View Users Issues Permission. Users can view thier own issues. This includes ones they are the creator or owner of. */
55     public static final int PERMISSION_VIEW_USERS = 8;
56     /** Edit Users Issues Permission. Users with this permission can edit any issue they created or own.
57       * They are limited to editing the description, adding history entries, and adding attachments. */

58     public static final int PERMISSION_EDIT_USERS = 9;
59     /** Issue Unassign Self Permission. Users with this permission can unassign issues they own. */
60     public static final int PERMISSION_UNASSIGN_SELF = 10;
61     /** Issue Assignable. Users with this permission can be assigned any issue in the system. To determine if a user can
62       * be assigned an issue, it will be a combination of users with EDIT_ALL, users with EDIT_USERS if they are the creator,
63       * and users with this permission and EDIT_USERS. */

64     public static final int PERMISSION_ASSIGNABLE = 11;
65     /** Create for Others. Users with this permission are allowed to create issues on behalf of other users. The system
66       * will treat the issue as if the other user had created it. The actual creator will be logged in the audit log. */

67     public static final int PERMISSION_CREATE_OTHERS = 12;
68     /** Full edit permission. This defines what levelof editing a user has for an issue. Without this permission, users will
69         be limited to editing only the description, attachments, custom fields, and history of an issue. */

70     public static final int PERMISSION_EDIT_FULL = 13;
71
72     public static final int REGISTRATION_TYPE_ADMIN = 1;
73     public static final int REGISTRATION_TYPE_SELF = 2;
74     public static final int REGISTRATION_TYPE_IMPORT = 3;
75
76     public static final int PREF_HIDE_ASSIGNED = 1;
77     public static final int PREF_HIDE_UNASSIGNED = 2;
78     public static final int PREF_HIDE_CREATED = 4;
79     public static final int PREF_HIDE_WATCHED = 8;
80
81     public UserUtilities() {
82     }
83
84     public static String JavaDoc getStatusName(int value) {
85         return getStatusName(value, ITrackerResources.getLocale());
86     }
87
88     public static String JavaDoc getStatusName(int value, Locale locale) {
89         return ITrackerResources.getString(ITrackerResources.KEY_BASE_USER_STATUS + value, locale);
90     }
91
92     public static HashMap getStatusNames() {
93         return getStatusNames(ITrackerResources.getLocale());
94     }
95
96     public static HashMap getStatusNames(Locale locale) {
97         HashMap statuses = new HashMap();
98         statuses.put(Integer.toString(STATUS_DELETED), getStatusName(STATUS_DELETED, locale));
99         statuses.put(Integer.toString(STATUS_ACTIVE), getStatusName(STATUS_ACTIVE, locale));
100         statuses.put(Integer.toString(STATUS_LOCKED), getStatusName(STATUS_LOCKED, locale));
101         return statuses;
102     }
103
104     public static String JavaDoc getPermissionName(int value) {
105         return getPermissionName(value, ITrackerResources.getLocale());
106     }
107
108     public static String JavaDoc getPermissionName(int value, Locale locale) {
109         return ITrackerResources.getString(ITrackerResources.KEY_BASE_PERMISSION + value, locale);
110     }
111
112     public static NameValuePairModel[] getPermissionNames() {
113         return getPermissionNames(ITrackerResources.getLocale());
114     }
115
116     public static NameValuePairModel[] getPermissionNames(Locale locale) {
117         NameValuePairModel[] permissions = new NameValuePairModel[13];
118         permissions[0] = new NameValuePairModel(getPermissionName(PERMISSION_CREATE, locale), Integer.toString(PERMISSION_CREATE));
119         permissions[1] = new NameValuePairModel(getPermissionName(PERMISSION_CREATE_OTHERS, locale), Integer.toString(PERMISSION_CREATE_OTHERS));
120         permissions[2] = new NameValuePairModel(getPermissionName(PERMISSION_EDIT, locale), Integer.toString(PERMISSION_EDIT));
121         permissions[3] = new NameValuePairModel(getPermissionName(PERMISSION_EDIT_USERS, locale), Integer.toString(PERMISSION_EDIT_USERS));
122         permissions[4] = new NameValuePairModel(getPermissionName(PERMISSION_EDIT_FULL, locale), Integer.toString(PERMISSION_EDIT_FULL));
123         permissions[5] = new NameValuePairModel(getPermissionName(PERMISSION_CLOSE, locale), Integer.toString(PERMISSION_CLOSE));
124         permissions[6] = new NameValuePairModel(getPermissionName(PERMISSION_ASSIGNABLE, locale), Integer.toString(PERMISSION_ASSIGNABLE));
125         permissions[7] = new NameValuePairModel(getPermissionName(PERMISSION_ASSIGN_SELF, locale), Integer.toString(PERMISSION_ASSIGN_SELF));
126         permissions[8] = new NameValuePairModel(getPermissionName(PERMISSION_UNASSIGN_SELF, locale), Integer.toString(PERMISSION_UNASSIGN_SELF));
127         permissions[9] = new NameValuePairModel(getPermissionName(PERMISSION_ASSIGN_OTHERS, locale), Integer.toString(PERMISSION_ASSIGN_OTHERS));
128         permissions[10] = new NameValuePairModel(getPermissionName(PERMISSION_VIEW_ALL, locale), Integer.toString(PERMISSION_VIEW_ALL));
129         permissions[11] = new NameValuePairModel(getPermissionName(PERMISSION_VIEW_USERS, locale), Integer.toString(PERMISSION_VIEW_USERS));
130         permissions[12] = new NameValuePairModel(getPermissionName(PERMISSION_PRODUCT_ADMIN, locale), Integer.toString(PERMISSION_PRODUCT_ADMIN));
131         return permissions;
132     }
133
134     /**
135       * Genrates a new random password. The password that is returned is in plain text.
136       * @return a new ramdon plaintext password
137       */

138     public static String JavaDoc generatePassword() throws PasswordException {
139         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
140         Random rand = new Random();
141         for(int i = 0; i < 8; i++) {
142             buf.append((rand.nextInt(2) == 0 ? Character.toUpperCase(alphabet[rand.nextInt(34)]) : alphabet[rand.nextInt(34)]));
143         }
144         return buf.toString();
145     }
146
147     /**
148       * Returns an encrypted (digest) password from a plain text password.
149       * @param password the plain text password to encrypt.
150       * @return the encrypted password
151       */

152     public static String JavaDoc encryptPassword(String JavaDoc password) throws PasswordException {
153         String JavaDoc hash = null;
154         if(password != null && ! password.equals("")) {
155             try {
156                 MessageDigest md = MessageDigest.getInstance("SHA");
157                 md.update(password.getBytes("UTF-8"));
158                 byte raw[] = md.digest();
159                 hash = (new BASE64Encoder()).encode(raw);
160             } catch(NoSuchAlgorithmException nsae) {
161                 throw new PasswordException(PasswordException.SYSTEM_ERROR);
162             } catch(UnsupportedEncodingException uee) {
163                 throw new PasswordException(PasswordException.SYSTEM_ERROR);
164             }
165         }
166         return hash;
167     }
168
169     /**
170       * Checks to see if the user is a super user.
171       * @param permissions the permissions of a user to check
172       * @return true is the user is a super user
173       */

174     public static boolean isSuperUser(HashMap permissions) {
175         if(permissions == null) {
176             return false;
177         }
178
179         Set keySet = permissions.keySet();
180
181         Boolean JavaDoc superUser = (Boolean JavaDoc) permissions.get(Integer.toString(-1));
182         if(superUser != null && superUser.booleanValue()) {
183             return true;
184         }
185
186         return false;
187     }
188
189     /**
190       * Returns true if the user has the required permission in any project.
191       * @param permissions a HashMap of the user's permissions
192       * @param permissionNeeded the permission to check for
193       */

194     public static boolean hasPermission(HashMap permissions, int permissionNeeded) {
195         if(permissions == null) {
196             return false;
197         }
198
199         Set keySet = permissions.keySet();
200
201         Boolean JavaDoc superUser = (Boolean JavaDoc) permissions.get(Integer.toString(-1));
202         if(superUser != null && superUser.booleanValue()) {
203             return true;
204         }
205
206         for(Iterator iterator = keySet.iterator(); iterator.hasNext(); ) {
207             Integer JavaDoc projectId = (Integer JavaDoc) iterator.next();
208             if(hasPermission(permissions, projectId, permissionNeeded)) {
209                 return true;
210             }
211         }
212         return false;
213     }
214
215     /**
216       * Returns true if the user has any of required permissions in any project.
217       * @param permissions a HashMap of the user's permissions
218       * @param permissionsNeeded a list of permissions that can fulfill the permission check
219       */

220     public static boolean hasPermission(HashMap permissions, int[] permissionsNeeded) {
221         if(permissions == null) {
222             return false;
223         }
224
225         Set keySet = permissions.keySet();
226
227         Boolean JavaDoc superUser = (Boolean JavaDoc) permissions.get(Integer.toString(-1));
228         if(superUser != null && superUser.booleanValue()) {
229             return true;
230         }
231
232         for(Iterator iterator = keySet.iterator(); iterator.hasNext(); ) {
233             Integer JavaDoc projectId = (Integer JavaDoc) iterator.next();
234             if(hasPermission(permissions, projectId, permissionsNeeded)) {
235                 return true;
236             }
237         }
238         return false;
239     }
240
241     /**
242       * Returns true if the user has the required permission for the given project.
243       * @param permissions a HashMap of the user's permissions
244       * @param projectId the project that the permission is required for
245       * @param permissionNeeded the permission to check for
246       */

247     public static boolean hasPermission(HashMap permissions, Integer JavaDoc projectId, int permissionNeeded) {
248         if(permissions == null) {
249             return false;
250         }
251
252         Boolean JavaDoc superUser = (Boolean JavaDoc) permissions.get(Integer.toString(-1));
253         if(superUser != null && superUser.booleanValue()) {
254             return true;
255         }
256
257         if(projectId != null && projectId.intValue() > 0) {
258             HashSet projectPermissions = (HashSet) permissions.get(projectId);
259             if(projectPermissions != null && projectPermissions.contains(Integer.toString(permissionNeeded))) {
260                 return true;
261             }
262         }
263         return false;
264     }
265
266     /**
267       * Returns true if the user has any of required permissions for the given project.
268       * @param permissions a HashMap of the user's permissions
269       * @param projectId the project that the permission is required for
270       * @param permissionsNeeded a list of permissions that can fulfill the permission check
271       */

272     public static boolean hasPermission(HashMap permissions, Integer JavaDoc projectId, int[] permissionsNeeded) {
273         if(permissions == null) {
274             return false;
275         }
276
277         Boolean JavaDoc superUser = (Boolean JavaDoc) permissions.get(Integer.toString(-1));
278         if(superUser != null && superUser.booleanValue()) {
279             return true;
280         }
281
282         if(projectId != null && projectId.intValue() > 0) {
283             HashSet projectPermissions = (HashSet) permissions.get(projectId);
284
285             if(projectPermissions != null) {
286                 for(int i = 0; i < permissionsNeeded.length; i++) {
287                     if(projectPermissions.contains(Integer.toString(permissionsNeeded[i]))) {
288                         return true;
289                     }
290                 }
291             }
292         }
293         return false;
294     }
295
296     public static String JavaDoc getInitial(String JavaDoc name) {
297         return (name != null && name.length() > 0 ? name.substring(0,1).toUpperCase() + "." : "");
298     }
299
300     public static String JavaDoc getPermissionsToString(HashMap permissions) {
301         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
302         if(permissions != null) {
303             Set keySet = permissions.keySet();
304
305             Boolean JavaDoc superAdmin = (Boolean JavaDoc) permissions.get(Integer.toString(-1));
306             if(superAdmin != null && superAdmin.booleanValue()) {
307                 buf.append("ITracker Super User\n\n");
308             }
309             for(Iterator iterator = keySet.iterator(); iterator.hasNext(); ) {
310                 Object JavaDoc project = iterator.next();
311                 if(project instanceof Integer JavaDoc) {
312                     Integer JavaDoc projectId = (Integer JavaDoc) project;
313                     if(projectId.intValue() >= 0) {
314                         HashSet projectPermissions = (HashSet) permissions.get(projectId);
315                         if(projectPermissions != null) {
316                             for(Iterator iteratorInner = projectPermissions.iterator(); iteratorInner.hasNext(); ) {
317                                 buf.append("Project: " + projectId + " Permission: " + getPermissionName(Integer.parseInt((String JavaDoc) iteratorInner.next())) + "\n");
318                             }
319                         }
320                     }
321                 }
322             }
323         }
324         if(buf.length() == 0) {
325             buf.append("User has no permissions.");
326         }
327         return buf.toString();
328     }
329
330     public static PermissionModel[] createPermissionArray(UserModel user, Integer JavaDoc projectId, int[] permissions) {
331         PermissionModel[] permissionsArray = new PermissionModel[0];
332
333         Vector permissionsVector = new Vector();
334         if(user.isSuperUser()) {
335             permissionsVector.add(new PermissionModel(new Integer JavaDoc(-1), -1, user.getLogin(), user.getId()));
336         }
337         for(int i = 0; i < permissions.length; i++) {
338             permissionsVector.add(new PermissionModel(projectId, permissions[i], user.getLogin(), user.getId()));
339         }
340         permissionsArray = new PermissionModel[permissionsVector.size()];
341         permissionsVector.copyInto(permissionsArray);
342
343         return permissionsArray;
344     }
345
346     public static HashMap permissionsArrayToMap(PermissionModel[] permissionsArray) {
347         HashMap permissionMap = new HashMap();
348
349         if(permissionsArray != null) {
350             for(int i = 0; i < permissionsArray.length; i++) {
351                 if(permissionsArray[i].getProjectId() == null || permissionsArray[i].getUserId() == null) {
352                     continue;
353                 }
354
355                 if(permissionsArray[i].getProjectId().intValue() == -1 && permissionsArray[i].getPermissionType() == -1) {
356                     permissionMap.put(Integer.toString(-1), new Boolean JavaDoc(true));
357                 } else {
358                     if(permissionMap.get(permissionsArray[i].getProjectId()) == null) {
359                         HashSet projectPermissions = new HashSet();
360                         permissionMap.put(permissionsArray[i].getProjectId(), projectPermissions);
361                     }
362                     ((HashSet) permissionMap.get(permissionsArray[i].getProjectId())).add(Integer.toString(permissionsArray[i].getPermissionType()));
363                 }
364             }
365         }
366         return permissionMap;
367     }
368
369     public static PermissionModel[] permissionsMapToArray(HashMap permissions) {
370         PermissionModel[] permissionsArray = new PermissionModel[0];
371         Vector permissionsVector = new Vector();
372
373         if(permissions != null) {
374             Set keySet = permissions.keySet();
375
376             for(Iterator iterator = keySet.iterator(); iterator.hasNext(); ) {
377                 Object JavaDoc project = iterator.next();
378                 if(! (project instanceof Integer JavaDoc)) {
379                     continue;
380                 }
381
382                 Integer JavaDoc projectId = (Integer JavaDoc) project;
383                 if(projectId.intValue() < 0) {
384                     continue;
385                 }
386
387                 HashSet projectPermissions = (HashSet) permissions.get(projectId);
388                 if(projectPermissions == null) {
389                     continue;
390                 }
391                 for(Iterator iteratorInner = projectPermissions.iterator(); iteratorInner.hasNext(); ) {
392                     try {
393                         permissionsVector.add(new PermissionModel(projectId, Integer.parseInt((String JavaDoc) iteratorInner.next())));
394                     } catch(Exception JavaDoc e) {
395                     }
396                 }
397             }
398         }
399         permissionsArray = new PermissionModel[permissionsVector.size()];
400         permissionsVector.copyInto(permissionsArray);
401         return permissionsArray;
402     }
403
404     /**
405       * Returns whether the user is currently hiding a particular section on the myItracker page.
406       * @param section the section to check if it is hidden
407       * @param an integer of all sections the user is hiding
408       * @return true if the current section is hidden
409       */

410     public static boolean hideIndexSection(int section, int sections) {
411         return ((section & sections) == section);
412     }
413
414     public static Integer JavaDoc[] getHiddenIndexSections(int sections) {
415         Vector sectionsVector = new Vector();
416         if(hideIndexSection(PREF_HIDE_ASSIGNED, sections)) {
417             sectionsVector.add(new Integer JavaDoc(PREF_HIDE_ASSIGNED));
418         }
419         if(hideIndexSection(PREF_HIDE_UNASSIGNED, sections)) {
420             sectionsVector.add(new Integer JavaDoc(PREF_HIDE_UNASSIGNED));
421         }
422         if(hideIndexSection(PREF_HIDE_CREATED, sections)) {
423             sectionsVector.add(new Integer JavaDoc(PREF_HIDE_CREATED));
424         }
425         if(hideIndexSection(PREF_HIDE_WATCHED, sections)) {
426             sectionsVector.add(new Integer JavaDoc(PREF_HIDE_WATCHED));
427         }
428         Integer JavaDoc[] sectionsArray = new Integer JavaDoc[sectionsVector.size()];
429         sectionsVector.copyInto(sectionsArray);
430         return sectionsArray;
431     }
432
433 }
434
Popular Tags