1 package com.sslexplorer.security.forms; 2 3 import java.util.Iterator ; 4 import java.util.List ; 5 6 import javax.servlet.http.HttpServletRequest ; 7 8 import org.apache.struts.Globals; 9 import org.apache.struts.action.ActionErrors; 10 import org.apache.struts.action.ActionMapping; 11 import org.apache.struts.action.ActionMessage; 12 13 import com.sslexplorer.boot.PropertyList; 14 import com.sslexplorer.core.UserDatabaseManager; 15 import com.sslexplorer.core.forms.CoreForm; 16 import com.sslexplorer.security.LogonControllerFactory; 17 import com.sslexplorer.security.User; 18 import com.sslexplorer.security.UserDatabase; 19 20 21 27 public class RoleForm extends CoreForm { 28 29 31 private String rolename; 32 private PropertyList users; 33 34 40 public String getUsers() { 41 return users.getAsTextFieldText(); 42 } 43 44 50 public void setUsers(String users) { 51 this.users.setAsTextFieldText(users); 52 } 53 54 59 public String getRolename() { 60 return rolename; 61 } 62 63 68 public void setRolename(String rolename) { 69 this.rolename = rolename.trim(); 70 } 71 72 77 public void initialize(List users) { 78 rolename = ""; 79 this.users = new PropertyList(); 80 for(Iterator i = users.iterator(); i.hasNext(); ) { 81 this.users.add(((User)i.next()).getPrincipalName()); 82 } 83 } 84 85 88 public ActionErrors validate(ActionMapping arg0, HttpServletRequest request) { 89 ActionErrors errors = new ActionErrors(); 90 if (isCommiting()) { 91 if (rolename.equals("")) { 92 errors.add(Globals.ERROR_KEY, new ActionMessage("availableRoles.noRolenameSupplied")); 93 } 94 if (rolename.length() > 32) { 95 errors.add(Globals.ERROR_KEY, new ActionMessage("availableRoles.roleNameExceeds32Chars")); 96 } 97 98 UserDatabase udb; 99 try { 100 udb = UserDatabaseManager.getInstance().getUserDatabase( 101 LogonControllerFactory.getInstance().getUser(request).getRealm().getResourceId()); 102 User[] u = udb.listAllUsers("*"); 103 List <String > users = getUserList(); 104 for (String user : users) { 105 if(!isAccountExists(user, u)) 106 errors.add(Globals.ERROR_KEY, new ActionMessage("availableRoles.noExistingAccount", user)); 107 } 108 if (!getEditing()) { 109 try { 110 udb.getRole(rolename); 111 errors.add(Globals.ERROR_KEY, new ActionMessage("availableRoles.roleAlreadyExists", rolename)); 112 } catch (Exception e) { 113 } 114 } 115 } catch (Exception e1) { 116 errors.add(Globals.ERROR_KEY, new ActionMessage("availableRoles.noUserDatabase")); 117 } 118 } 119 return errors; 120 } 121 122 boolean isAccountExists(String accountName, User[] users) { 123 for (int i = 0; i < users.length; i++) { 124 if (null != users[i] && users[i].getPrincipalName().equals(accountName)) 125 return true; 126 } 127 return false; 128 } 129 130 135 public List getUserList() { 136 return users; 137 } 138 } 139 | Popular Tags |