1 19 20 package com.sslexplorer.security.forms; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import javax.servlet.http.HttpServletRequest ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.struts.Globals; 32 import org.apache.struts.action.ActionErrors; 33 import org.apache.struts.action.ActionMapping; 34 import org.apache.struts.action.ActionMessage; 35 import org.apache.struts.util.MessageResources; 36 37 import com.sslexplorer.boot.CodedException; 38 import com.sslexplorer.boot.ContextHolder; 39 import com.sslexplorer.boot.PropertyClass; 40 import com.sslexplorer.boot.PropertyClassManager; 41 import com.sslexplorer.boot.PropertyDefinition; 42 import com.sslexplorer.boot.PropertyList; 43 import com.sslexplorer.core.CoreException; 44 import com.sslexplorer.core.CoreUtil; 45 import com.sslexplorer.core.UserDatabaseManager; 46 import com.sslexplorer.core.forms.CoreForm; 47 import com.sslexplorer.policyframework.PolicyUtil; 48 import com.sslexplorer.properties.Property; 49 import com.sslexplorer.properties.attributes.AttributeDefinition; 50 import com.sslexplorer.properties.attributes.AttributeValueItem; 51 import com.sslexplorer.properties.impl.userattributes.UserAttributeKey; 52 import com.sslexplorer.properties.impl.userattributes.UserAttributes; 53 import com.sslexplorer.security.LogonControllerFactory; 54 import com.sslexplorer.security.Role; 55 import com.sslexplorer.security.SessionInfo; 56 import com.sslexplorer.security.User; 57 import com.sslexplorer.security.UserDatabase; 58 import com.sslexplorer.tabs.TabModel; 59 60 73 public class UserAccountForm extends CoreForm implements TabModel { 74 static Log log = LogFactory.getLog(UserAccountForm.class); 75 76 78 private String username; 79 private String email; 80 private String fullname; 81 private boolean setPassword; 82 private boolean enabled; 83 private PropertyList roles; 84 private List userAttributeValueItems; 85 private String selectedTab = "details"; 86 private List categoryIds; 87 private List categoryTitles; 88 private PropertyClass propertyClass; 89 private String realmName; 90 91 public UserAccountForm() { 92 propertyClass = PropertyClassManager.getInstance().getPropertyClass(UserAttributes.NAME); 93 } 94 95 98 public String getRealmName() { 99 return realmName; 100 } 101 102 105 public void setRealmName(String realmName) { 106 this.realmName = realmName; 107 } 108 109 114 public String getUsername() { 115 return username; 116 } 117 118 123 public void setUsername(String username) { 124 this.username = username.trim(); 125 } 126 127 132 public String getFullname() { 133 return fullname; 134 } 135 136 141 public void setFullname(String fullname) { 142 this.fullname = fullname.trim(); 143 } 144 145 150 public boolean isEnabled() { 151 return enabled; 152 } 153 154 159 public void setEnabled(boolean enabled) { 160 this.enabled = enabled; 161 } 162 163 168 public String getEmail() { 169 return email; 170 } 171 172 177 public void setEmail(String email) { 178 this.email = email.trim(); 179 } 180 181 186 public void setSetPassword(boolean setPassword) { 187 this.setPassword = setPassword; 188 } 189 190 195 public boolean isSetPassword() { 196 return setPassword; 197 } 198 199 207 public void initialize(User user, boolean editing, HttpServletRequest request) throws Exception { 208 209 username = user == null ? "" : user.getPrincipalName(); 210 realmName = user == null ? "" : user.getRealm().getResourceName(); 211 email = user == null ? "" : user.getEmail(); 212 fullname = user == null ? "" : user.getFullname(); 213 try { 214 enabled = user == null ? true : PolicyUtil.isEnabled(user); 215 } catch (Exception e) { 216 log.warn("Failed to determine if user is enabled, defaulting to disabled."); 217 enabled = false; 218 } 219 setActionTarget("commit"); 220 setPassword = false; 221 this.editing = editing; 222 roles = new PropertyList(); 223 Role[] allRoles = user == null ? new Role[0] : user.getRoles(); 224 for (int i = 0; i < allRoles.length; i++) { 225 roles.add(allRoles[i].getPrincipalName()); 226 } 227 228 232 233 userAttributeValueItems = new ArrayList (); 234 for (PropertyDefinition d : propertyClass.getDefinitions()) { 235 AttributeDefinition def = (AttributeDefinition)d; 236 if (!def.isHidden()) { 237 if (def.getVisibility() != AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) { 238 String value = def.getDefaultValue(); 239 if (user != null) { 240 value = Property.getProperty(new UserAttributeKey(user, def.getName())); 241 } 242 AttributeValueItem item = new AttributeValueItem(def, request, value); 243 userAttributeValueItems.add(item); 244 } 245 } 246 } 247 248 251 252 Collections.sort(userAttributeValueItems); 253 categoryIds = new ArrayList (); 254 categoryTitles = new ArrayList (); 255 for (Iterator i = userAttributeValueItems.iterator(); i.hasNext();) { 256 AttributeValueItem item = (AttributeValueItem) i.next(); 257 int idx = categoryIds.indexOf(item.getCategoryId()); 258 if (idx == -1) { 259 categoryIds.add(item.getCategoryId()); 260 categoryTitles.add(item.getCategoryLabel()); 261 } 262 } 263 } 264 265 270 public List getCategoryIds() { 271 return categoryIds; 272 } 273 274 279 public List getAttributeValueItems() { 280 return userAttributeValueItems; 281 } 282 283 289 public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request) { 290 enabled = false; 296 if (userAttributeValueItems != null) { 300 for (Iterator i = userAttributeValueItems.iterator(); i.hasNext();) { 301 AttributeValueItem item = (AttributeValueItem) i.next(); 302 if (item.getDefinition().getType() == PropertyDefinition.TYPE_BOOLEAN) { 303 item.setSelected(false); 304 } 305 } 306 } 307 } 308 309 316 public String getUsernameDisabled() { 317 return String.valueOf(getEditing()); 318 } 319 320 326 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 327 ActionErrors errors = new ActionErrors(); 328 if (isCommiting()) { 329 UserDatabase udb; 330 try { 331 udb = UserDatabaseManager.getInstance().getUserDatabase(LogonControllerFactory.getInstance().getUser(request).getRealm()); 332 } catch (Exception e1) { 333 errors.add(Globals.ERROR_KEY, new ActionMessage("availableRoles.noUserDatabase")); 334 return errors; 335 } 336 if (username == null || username.length() == 0) { 337 errors.add(Globals.ERROR_KEY, new ActionMessage("createAccount.error.noUsername")); 338 } 339 if (username.length() > 32) { 340 errors.add(Globals.ERROR_KEY, new ActionMessage("createAccount.error.usernameExceeds32Chars")); 341 } 342 if (udb.supportsAccountCreation()) { 343 if (fullname == null || fullname.length() == 0) { 344 errors.add(Globals.ERROR_KEY, new ActionMessage("createAccount.error.noFullName")); 345 } 346 if (fullname.length() > 32) { 347 errors.add(Globals.ERROR_KEY, new ActionMessage("createAccount.error.fullNameExceeds32Chars")); 348 } 349 } 350 User currentUser; 351 try { 352 currentUser = ContextHolder.getContext().isSetupMode() ? null : LogonControllerFactory.getInstance() 353 .getUser(request); 354 if (currentUser != null && getEditing() && currentUser.getPrincipalName().equals(getUsername())) { 355 if (!isEnabled()) { 357 errors.add(Globals.ERROR_KEY, new ActionMessage("createAccount.error.cantDisableYourself")); 358 } 359 } 360 if (!editing) { 361 try { 362 udb.getAccount(username); 363 errors.add(Globals.ERROR_KEY, new ActionMessage("createAccount.error.userAlreadyExists", username)); 364 } catch (Exception e) { 365 } 366 } 367 for (Iterator i = userAttributeValueItems.iterator(); i.hasNext();) { 368 AttributeValueItem item = (AttributeValueItem) i.next(); 369 PropertyDefinition def = item.getDefinition(); 370 try { 371 def.validate(item.getValue().toString(), getClass().getClassLoader()); 372 } catch (CoreException ce) { 373 ce.getBundleActionMessage().setArg3(item.getLabel()); 374 errors.add(Globals.ERROR_KEY, ce.getBundleActionMessage()); 375 } catch (CodedException ce) { 376 errors.add(Globals.ERROR_KEY, new ActionMessage("[Err:" + ce.getCode() + "] " + ce.getMessage())); 377 } 378 } 379 380 for (String role : getRolesList()) { 382 try { 383 UserDatabaseManager.getInstance().getDefaultUserDatabase().getRole(role); 384 } catch (Exception e) { 385 errors.add(Globals.ERROR_KEY, new ActionMessage("createAccount.error.invalidRole", role)); 386 } 387 } 388 } catch (Exception e) { 389 log.warn("Failed to validate user form.", e); 390 e.printStackTrace(); 391 } 392 } 393 return errors; 394 } 395 396 401 public PropertyList getRolesList() { 402 return roles; 403 } 404 405 410 public String getSelectedRoles() { 411 return roles.getAsTextFieldText(); 412 } 413 414 419 public void setSelectedRoles(String selectedRoles) { 420 roles.setAsTextFieldText(selectedRoles); 421 } 422 423 428 public int getTabCount() { 429 return 1 + (categoryIds.size()); 430 } 431 432 437 public String getTabName(int idx) { 438 switch (idx) { 439 case 0: 440 return "details"; 441 default: 442 return (String ) categoryIds.get(idx - 1); 443 } 444 } 445 446 451 public String getSelectedTab() { 452 return selectedTab; 453 } 454 455 460 public void setSelectedTab(String selectedTab) { 461 this.selectedTab = selectedTab; 462 } 463 464 469 public String getTabTitle(int idx) { 470 switch (idx) { 471 case 0: 472 return null; 473 default: 474 return (String ) categoryTitles.get(idx - 1); 475 } 476 } 477 478 481 public String getTabBundle(int idx) { 482 return null; 483 } 484 } 485 | Popular Tags |