1 18 19 package cowsultants.itracker.ejb.authentication; 20 21 import java.rmi.*; 22 import java.rmi.server.*; 23 import java.util.*; 24 import javax.ejb.*; 25 26 import cowsultants.itracker.ejb.client.exceptions.*; 27 import cowsultants.itracker.ejb.client.interfaces.*; 28 import cowsultants.itracker.ejb.client.models.*; 29 import cowsultants.itracker.ejb.client.util.*; 30 31 36 public class DefaultAuthenticator extends AbstractPluggableAuthenticator { 37 38 public DefaultAuthenticator() { 39 } 40 41 51 public UserModel checkLogin(String login, Object authentication, int authType, int reqSource) throws AuthenticatorException { 52 Logger.logDebug("Checking login for " + login + " using DefaultAuthenticator"); 53 54 if(login != null && authentication != null && ! login.equals("")) { 55 UserModel user = getUserHandler().getUserByLogin(login); 56 57 if(user == null) { 58 throw new AuthenticatorException(AuthenticatorException.UNKNOWN_USER); 59 } 60 if(user.getStatus() != UserUtilities.STATUS_ACTIVE) { 61 throw new AuthenticatorException(AuthenticatorException.INACTIVE_ACCOUNT); 62 } 63 64 String userPassword = getUserHandler().getUserPasswordByLogin(login); 65 if(userPassword == null || userPassword.equals("")) { 66 throw new AuthenticatorException(AuthenticatorException.INVALID_PASSWORD); 67 } 68 69 try { 70 if(! userPassword.endsWith("=")) { 71 Logger.logInfo("User " + login + " has old style password. Converting to SHA1 hash."); 72 try { 73 user.setPassword(UserUtilities.encryptPassword(userPassword)); 74 getUserHandler().updateUser(user); 75 } catch(UserException ue) { 76 Logger.logInfo("User password conversion failed for user " + login); 77 } 78 } 79 80 if(authType == AUTH_TYPE_PASSWORD_PLAIN) { 81 if(! userPassword.equals(UserUtilities.encryptPassword((String ) authentication))) { 82 throw new AuthenticatorException(AuthenticatorException.INVALID_PASSWORD); 83 } 84 } else if(authType == AUTH_TYPE_PASSWORD_ENC) { 85 if(! userPassword.equals((String ) authentication)) { 86 throw new AuthenticatorException(AuthenticatorException.INVALID_PASSWORD); 87 } 88 } else { 89 throw new AuthenticatorException(AuthenticatorException.INVALID_AUTHENTICATION_TYPE); 90 } 91 } catch(ClassCastException cce) { 92 Logger.logDebug("Authenticator was of wrong type.", cce); 93 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 94 } catch(PasswordException pe) { 95 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 96 } 97 98 return user; 99 } 100 101 throw new AuthenticatorException(AuthenticatorException.INVALID_DATA); 102 } 103 104 111 public PermissionModel[] getUserPermissions(UserModel user, int reqSource) throws AuthenticatorException { 112 if(user == null || user.getId() == null) { 113 throw new AuthenticatorException(AuthenticatorException.INVALID_DATA); 114 } 115 116 PermissionModel[] permissionArray = new PermissionModel[0]; 117 permissionArray = getUserHandler().getUserPermissionsLocal(user); 118 119 if(user.isSuperUser()) { 120 PermissionModel[] augmentedPermissions = new PermissionModel[permissionArray.length + 1]; 121 augmentedPermissions[0] = new PermissionModel(new Integer (-1), -1, user.getLogin(), user.getId()); 122 System.arraycopy(permissionArray, 0, augmentedPermissions, 1, permissionArray.length); 123 permissionArray = augmentedPermissions; 124 } 125 126 return permissionArray; 127 } 128 129 public UserModel[] getUsersWithProjectPermission(PermissionModel[] permissions, boolean requireAll, boolean activeOnly, int reqSource) throws AuthenticatorException { 130 UserModel[] userArray = new UserModel[0]; 131 132 try { 133 HashMap userMap = new HashMap(); 134 135 for(int i = 0; i < permissions.length; i++) { 136 UserModel[] explicitUsers = getUserHandler().getUsersWithPermissionLocal(permissions[i]); 137 if(! requireAll || permissions.length == 1) { 138 for(int j = 0; j < explicitUsers.length; j++) { 139 userMap.put(explicitUsers[j].getId(), explicitUsers[j]); 140 } 141 } else { 142 if(i == 0) { 143 for(int j = 0; j < explicitUsers.length; j++) { 144 userMap.put(explicitUsers[j].getId(), explicitUsers[j]); 145 } 146 } else { 147 for(Iterator iter = userMap.keySet().iterator(); iter.hasNext(); ) { 148 boolean found = false; 149 Integer userId = (Integer ) iter.next(); 150 for(int j = 0; j < explicitUsers.length; j++) { 151 if(userId.equals(explicitUsers[j].getId())) { 152 found = true; 153 break; 154 } 155 } 156 if(! found) { 157 iter.remove(); 158 } 159 } 160 } 161 } 162 } 163 164 UserModel[] superUsers = getUserHandler().getSuperUsers(); 165 for(int i = 0; i < superUsers.length; i++) { 166 if(! activeOnly || superUsers[i].getStatus() == UserUtilities.STATUS_ACTIVE) { 167 userMap.put(superUsers[i].getId(), superUsers[i]); 168 } 169 } 170 171 int i = 0; 172 userArray = new UserModel[userMap.size()]; 173 for(Iterator iter = userMap.values().iterator(); iter.hasNext(); i++) { 174 userArray[i] = (UserModel) iter.next(); 175 } 176 } catch(Exception e) { 177 Logger.logDebug("Error retreiving users with permissions.", e); 178 throw new AuthenticatorException(); 179 } 180 181 return userArray; 182 } 183 184 192 public boolean allowRegistration(UserModel user, Object authentication, int authType, int reqSource) throws AuthenticatorException { 193 return true; 194 } 195 196 197 205 public boolean allowProfileCreation(UserModel user, Object authentication, int authType, int reqSource) throws AuthenticatorException { 206 return true; 207 } 208 209 218 public boolean allowProfileUpdates(UserModel user, Object authentication, int authType, int reqSource) throws AuthenticatorException { 219 return true; 220 } 221 222 231 public boolean allowPasswordUpdates(UserModel user, Object authentication, int authType, int reqSource) throws AuthenticatorException { 232 return true; 233 } 234 235 244 public boolean allowPermissionUpdates(UserModel user, Object authentication, int authType, int reqSource) throws AuthenticatorException { 245 return true; 246 } 247 248 257 public boolean allowPreferenceUpdates(UserModel user, Object authentication, int authType, int reqSource) throws AuthenticatorException { 258 return true; 259 } 260 261 270 public boolean createProfile(UserModel user, Object authentication, int authType, int reqSource) throws AuthenticatorException { 271 return false; 272 } 273 274 284 public boolean updateProfile(UserModel user, int updateType, Object authentication, int authType, int reqSource) throws AuthenticatorException { 285 return false; 286 } 287 } 288 | Popular Tags |