1 19 20 package za.org.coefficient.util.ejb; 21 22 import za.org.coefficient.authentication.CoefficientUser; 23 import za.org.coefficient.authentication.ProjectMember; 24 import za.org.coefficient.authentication.Role; 25 import za.org.coefficient.core.Project; 26 import za.org.coefficient.util.common.InvokerFactory; 27 28 import java.io.Serializable ; 29 30 import java.security.MessageDigest ; 31 import java.security.NoSuchAlgorithmException ; 32 33 import java.util.ArrayList ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.TreeMap ; 38 39 43 public class SecurityUtil implements Serializable { 44 46 public static final String SITE_ADMIN_ROLE_DESC = "Site Administrator"; 47 public static final String SITE_MODERATOR_ROLE_DESC = "Site Moderator"; 48 public static final String PROJECT_CHAMPION_ROLE_DESC = "Project Champion"; 49 public static final String PROJECT_MEMBER_ROLE_DESC = "Project Member"; 50 public static final String SITE_MEMBER_ROLE_DESC = "Site Member"; 51 public static final String GUEST_ROLE_DESC = "Guest"; 52 public static final long SITE_ADMIN_ROLE_VAL = 0; 53 public static final long SITE_MODERATOR_ROLE_VAL = 50; 54 public static final long PROJECT_CHAMPION_ROLE_VAL = 100; 55 public static final long PROJECT_MEMBER_ROLE_VAL = 200; 56 public static final long SITE_MEMBER_ROLE_VAL = 250; 57 public static final long GUEST_ROLE_VAL = 300; 58 private static final String [] HEX_ALPHABET = 59 { 60 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", 61 "E", "F" 62 }; 63 private static HashMap roleDescCache = new HashMap (); 64 private static TreeMap roleValCache = new TreeMap (); 65 66 static { 68 try { 69 List roles = (List )InvokerFactory.getRemoteInvoker() 72 .invokeMethodOnService("Role", "getAllRoles", new Object [0]); 73 for (Iterator it = roles.iterator(); it.hasNext();) { 74 Role role = (Role) it.next(); 75 roleDescCache.put(role.getDescription(), role); 76 roleValCache.put(new Long (role.getRoleValue()), role); 77 } 78 79 InvokerFactory.getRemoteInvoker() 81 .invokeGetterOnModule("UserAdmin", "moduleName"); 82 } catch (Exception e) { 83 e.printStackTrace(); 84 } 85 } 86 87 89 public static List getAllUsers() { 90 List retVals = null; 91 try { 92 retVals = (List )InvokerFactory.getRemoteInvoker() 93 .invokeGetterOnModule("UserAdmin", "getAllUsers"); 94 } catch (Exception e) { 95 retVals = new ArrayList (); 97 } 98 99 return retVals; 100 } 101 102 121 public static Role getHighestRoleForUser(CoefficientUser user, 122 Project project) { 123 if (user == null) { 124 return getRoleForDescription(GUEST_ROLE_DESC); 125 } else { 126 Role retVal = user.getSystemRole(); 127 if (retVal.getDescription() 128 .equals(SITE_ADMIN_ROLE_DESC)) { 129 return retVal; 130 } else if (project != null) { 131 List projectMembers = project.getMembers(); 132 for (Iterator it = projectMembers.iterator(); it.hasNext();) { 133 ProjectMember member = (ProjectMember) it.next(); 134 if (member.getCoefficientUser() 135 .equals(user)) { 136 if (member.getProjectRole() 137 .getRoleValue() < retVal.getRoleValue()) { 138 retVal = member.getProjectRole(); 139 } 140 } 141 } 142 } 143 144 return retVal; 145 } 146 } 147 148 public static Role getRoleForDescription(String roleDescription) { 149 return (Role) roleDescCache.get(roleDescription); 150 } 151 152 public static synchronized Role getRoleForValue(long roleValue) { 153 return (Role) roleValCache.get(new Long (roleValue)); 154 } 155 156 public static synchronized List getRoles() { 157 return new ArrayList (roleValCache.values()); 158 } 159 160 public static List getUsersWithSystemRole(Role role) { 161 List retVals = null; 162 try { 163 retVals = (List )InvokerFactory.getRemoteInvoker() 164 .invokeMethodOnModule("UserAdmin", "getUsersWithSystemRole", 165 new Object []{role}); 166 } catch (Exception e) { 167 retVals = new ArrayList (); 169 } 170 171 return retVals; 172 } 173 174 180 public static byte[] md5(String text) { 181 if (text == null) { 183 throw new NullPointerException ("null text"); 184 } 185 186 try { 187 MessageDigest md = MessageDigest.getInstance("MD5"); 188 md.update(text.getBytes()); 189 190 return md.digest(); 191 } catch (NoSuchAlgorithmException e) { 192 throw new RuntimeException ("Cannot find MD5 algorithm"); 193 } 194 } 195 196 203 public static String md5AsHexString(String text) { 204 byte[] bytes = md5(text); 205 StringBuffer hex = new StringBuffer (); 206 for (int i = 0; i < bytes.length; i++) { 207 hex.append(HEX_ALPHABET[(bytes[i] & 0XF0) >> 4]); 208 hex.append(HEX_ALPHABET[bytes[i] & 0X0F]); 209 } 210 211 return hex.toString(); 212 } 213 } 214 | Popular Tags |