1 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 39 public static final int PERMISSION_USER_ADMIN = -1; 40 41 public static final int PERMISSION_PRODUCT_ADMIN = 1; 42 43 public static final int PERMISSION_CREATE = 2; 44 45 public static final int PERMISSION_EDIT = 3; 46 47 public static final int PERMISSION_CLOSE = 4; 48 49 public static final int PERMISSION_ASSIGN_SELF = 5; 50 51 public static final int PERMISSION_ASSIGN_OTHERS = 6; 52 53 public static final int PERMISSION_VIEW_ALL = 7; 54 55 public static final int PERMISSION_VIEW_USERS = 8; 56 58 public static final int PERMISSION_EDIT_USERS = 9; 59 60 public static final int PERMISSION_UNASSIGN_SELF = 10; 61 64 public static final int PERMISSION_ASSIGNABLE = 11; 65 67 public static final int PERMISSION_CREATE_OTHERS = 12; 68 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 getStatusName(int value) { 85 return getStatusName(value, ITrackerResources.getLocale()); 86 } 87 88 public static String 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 getPermissionName(int value) { 105 return getPermissionName(value, ITrackerResources.getLocale()); 106 } 107 108 public static String 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 138 public static String generatePassword() throws PasswordException { 139 StringBuffer buf = new StringBuffer (); 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 152 public static String encryptPassword(String password) throws PasswordException { 153 String 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 174 public static boolean isSuperUser(HashMap permissions) { 175 if(permissions == null) { 176 return false; 177 } 178 179 Set keySet = permissions.keySet(); 180 181 Boolean superUser = (Boolean ) permissions.get(Integer.toString(-1)); 182 if(superUser != null && superUser.booleanValue()) { 183 return true; 184 } 185 186 return false; 187 } 188 189 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 superUser = (Boolean ) 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 projectId = (Integer ) iterator.next(); 208 if(hasPermission(permissions, projectId, permissionNeeded)) { 209 return true; 210 } 211 } 212 return false; 213 } 214 215 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 superUser = (Boolean ) 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 projectId = (Integer ) iterator.next(); 234 if(hasPermission(permissions, projectId, permissionsNeeded)) { 235 return true; 236 } 237 } 238 return false; 239 } 240 241 247 public static boolean hasPermission(HashMap permissions, Integer projectId, int permissionNeeded) { 248 if(permissions == null) { 249 return false; 250 } 251 252 Boolean superUser = (Boolean ) 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 272 public static boolean hasPermission(HashMap permissions, Integer projectId, int[] permissionsNeeded) { 273 if(permissions == null) { 274 return false; 275 } 276 277 Boolean superUser = (Boolean ) 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 getInitial(String name) { 297 return (name != null && name.length() > 0 ? name.substring(0,1).toUpperCase() + "." : ""); 298 } 299 300 public static String getPermissionsToString(HashMap permissions) { 301 StringBuffer buf = new StringBuffer (); 302 if(permissions != null) { 303 Set keySet = permissions.keySet(); 304 305 Boolean superAdmin = (Boolean ) 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 project = iterator.next(); 311 if(project instanceof Integer ) { 312 Integer projectId = (Integer ) 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 ) 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 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 (-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 (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 project = iterator.next(); 378 if(! (project instanceof Integer )) { 379 continue; 380 } 381 382 Integer projectId = (Integer ) 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 ) iteratorInner.next()))); 394 } catch(Exception e) { 395 } 396 } 397 } 398 } 399 permissionsArray = new PermissionModel[permissionsVector.size()]; 400 permissionsVector.copyInto(permissionsArray); 401 return permissionsArray; 402 } 403 404 410 public static boolean hideIndexSection(int section, int sections) { 411 return ((section & sections) == section); 412 } 413 414 public static Integer [] getHiddenIndexSections(int sections) { 415 Vector sectionsVector = new Vector(); 416 if(hideIndexSection(PREF_HIDE_ASSIGNED, sections)) { 417 sectionsVector.add(new Integer (PREF_HIDE_ASSIGNED)); 418 } 419 if(hideIndexSection(PREF_HIDE_UNASSIGNED, sections)) { 420 sectionsVector.add(new Integer (PREF_HIDE_UNASSIGNED)); 421 } 422 if(hideIndexSection(PREF_HIDE_CREATED, sections)) { 423 sectionsVector.add(new Integer (PREF_HIDE_CREATED)); 424 } 425 if(hideIndexSection(PREF_HIDE_WATCHED, sections)) { 426 sectionsVector.add(new Integer (PREF_HIDE_WATCHED)); 427 } 428 Integer [] sectionsArray = new Integer [sectionsVector.size()]; 429 sectionsVector.copyInto(sectionsArray); 430 return sectionsArray; 431 } 432 433 } 434 | Popular Tags |