KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > forms > RoleForm


1 package com.sslexplorer.security.forms;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5
6 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
22  * Implementation of a {@link CoreForm} that allows an administrator to
23  * create or edit a <i>Group</i> of users (previously known as a <i>Role</i>).
24  *
25  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
26  */

27 public class RoleForm extends CoreForm {
28     
29     // Private instance variables
30

31     private String JavaDoc rolename;
32     private PropertyList users;
33     
34     /**
35      * Get the list of selected users in <i>Text Field Text</i> format
36      *
37      * @return selected users
38      * @see PropertyList
39      */

40     public String JavaDoc getUsers() {
41         return users.getAsTextFieldText();
42     }
43     
44     /**
45      * Set the list of selected users in <i>Text Field Text</i> format
46      *
47      * @param users selected users
48      * @see PropertyList
49      */

50     public void setUsers(String JavaDoc users) {
51         this.users.setAsTextFieldText(users);
52     }
53     
54     /**
55      * Get the name of the group.
56      *
57      * @return group
58      */

59     public String JavaDoc getRolename() {
60         return rolename;
61     }
62
63     /**
64      * Set the name of the role
65      *
66      * @param rolename name of role
67      */

68     public void setRolename(String JavaDoc rolename) {
69         this.rolename = rolename.trim();
70     }
71
72     /**
73      * Initialise the form
74      *
75      * @param users list of {@link User} objects attached to the role
76      */

77     public void initialize(List JavaDoc users) {
78         rolename = "";
79         this.users = new PropertyList();
80         for(Iterator JavaDoc i = users.iterator(); i.hasNext(); ) {
81             this.users.add(((User)i.next()).getPrincipalName());
82         }
83     }
84     
85     /* (non-Javadoc)
86      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
87      */

88     public ActionErrors validate(ActionMapping arg0, HttpServletRequest JavaDoc 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 JavaDoc<String JavaDoc> users = getUserList();
104                 for (String JavaDoc 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 JavaDoc e) {
113                     }
114                 }
115             } catch (Exception JavaDoc e1) {
116                 errors.add(Globals.ERROR_KEY, new ActionMessage("availableRoles.noUserDatabase"));
117             }
118         }
119         return errors;
120     }
121     
122     boolean isAccountExists(String JavaDoc 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     /**
131      * Get the selected as users as a list of strings
132      *
133      * @return selected users as a list of strings
134      */

135     public List JavaDoc getUserList() {
136         return users;
137     }
138 }
139
Popular Tags