001: package com.technoetic.xplanner.forms;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Iterator;
006: import javax.servlet.http.HttpServletRequest;
007:
008: import org.apache.commons.collections.CollectionUtils;
009: import org.apache.commons.collections.Predicate;
010: import org.apache.commons.lang.StringUtils;
011: import org.apache.struts.action.ActionErrors;
012: import org.apache.struts.action.ActionMapping;
013:
014: import com.technoetic.xplanner.domain.Role;
015: import com.technoetic.xplanner.security.AuthenticationException;
016: import com.technoetic.xplanner.security.auth.SystemAuthorizer;
017:
018: public class PersonEditorForm extends AbstractEditorForm {
019:
020: private String name;
021: private String email;
022: private String phone;
023: private int personId;
024: private String initials;
025: private String userId;
026: private String password;
027: private String newPassword;
028: private String newPasswordConfirm;
029: private boolean isHidden;
030: private ArrayList projectIds = new ArrayList();
031: private ArrayList projectRoles = new ArrayList();
032: private boolean isSystemAdmin;
033: public static final String PASSWORD_MISMATCH_ERROR = "person.editor.password_mismatch";
034:
035: public ActionErrors validate(ActionMapping mapping,
036: HttpServletRequest request) {
037: ActionErrors errors = new ActionErrors();
038: if (isSubmitted()) {
039: require(errors, name, "person.editor.missing_name");
040: require(errors, userId, "person.editor.missing_user_id");
041: require(errors, email, "person.editor.missing_email");
042: require(errors, initials, "person.editor.missing_initials");
043: if (StringUtils.isNotEmpty(newPassword)
044: && !StringUtils.equals(newPassword,
045: newPasswordConfirm)) {
046: error(errors, PASSWORD_MISMATCH_ERROR);
047: }
048: }
049: return errors;
050: }
051:
052: public void reset(ActionMapping mapping, HttpServletRequest request) {
053: super .reset(mapping, request);
054: name = null;
055: email = null;
056: phone = null;
057: initials = null;
058: userId = null;
059: personId = 0;
060: newPassword = null;
061: newPasswordConfirm = null;
062: isHidden = false;
063: isSystemAdmin = false;
064: }
065:
066: public String getContainerId() {
067: return Integer.toString(personId);
068: }
069:
070: public void setName(String name) {
071: this .name = name;
072: }
073:
074: public String getName() {
075: return name;
076: }
077:
078: public void setEmail(String email) {
079: this .email = email;
080: }
081:
082: public String getEmail() {
083: return email;
084: }
085:
086: public void setPhone(String phone) {
087: this .phone = phone;
088: }
089:
090: public String getPhone() {
091: return phone;
092: }
093:
094: public void setPersonId(int personId) {
095: this .personId = personId;
096: }
097:
098: public int getPersonId() {
099: return personId;
100: }
101:
102: public void setInitials(String initials) {
103: this .initials = initials;
104: }
105:
106: public String getInitials() {
107: return initials;
108: }
109:
110: /** Alias for userId so field won't be filled in automatically by Mozilla, etc. */
111: public String getUserIdentifier() {
112: return getUserId();
113: }
114:
115: /** Alias for userId so field won't be filled in automatically by Mozilla, etc. */
116: public void setUserIdentifier(String userId) {
117: setUserId(userId);
118: }
119:
120: public String getUserId() {
121: return userId;
122: }
123:
124: public void setUserId(String userId) {
125: this .userId = userId;
126: }
127:
128: public String getNewPassword() {
129: return newPassword;
130: }
131:
132: public void setNewPassword(String newPassword) {
133: this .newPassword = newPassword;
134: }
135:
136: public String getNewPasswordConfirm() {
137: return newPasswordConfirm;
138: }
139:
140: public void setNewPasswordConfirm(String newPasswordConfirm) {
141: this .newPasswordConfirm = newPasswordConfirm;
142: }
143:
144: public String getPassword() {
145: return password;
146: }
147:
148: public void setPassword(String password) {
149: this .password = password;
150: }
151:
152: public boolean isHidden() {
153: return isHidden;
154: }
155:
156: public void setHidden(boolean hidden) {
157: isHidden = hidden;
158: }
159:
160: public void setProjectId(int index, String projectId) {
161: ensureSize(projectIds, index + 1);
162: projectIds.set(index, projectId);
163:
164: }
165:
166: public String getProjectId(int index) {
167: return (String) projectIds.get(index);
168: }
169:
170: public int getProjectIdAsInt(int index) {
171: return Integer.parseInt(getProjectId(index));
172: }
173:
174: public int getProjectCount() {
175: return projectIds.size();
176: }
177:
178: public void setProjectRole(int index, String role) {
179: ensureSize(projectRoles, index + 1);
180: projectRoles.set(index, role);
181: }
182:
183: public String getProjectRole(int index) {
184: return (String) projectRoles.get(index);
185: }
186:
187: public void setSystemAdmin(boolean isSystemAdmin) {
188: this .isSystemAdmin = isSystemAdmin;
189: }
190:
191: public boolean isSystemAdmin() {
192: return isSystemAdmin;
193: }
194:
195: // These helper functions should be refactored into the domain objects
196:
197: private boolean hasRole(Collection roles, String name) {
198: for (Iterator iterator = roles.iterator(); iterator.hasNext();) {
199: Role role = (Role) iterator.next();
200: if (role.getName().equals(name)) {
201: return true;
202: }
203: }
204: return false;
205: }
206:
207: // This is for 0.6. In the future there will be a more general role editing and
208: // management framework.
209: //DEBT remove all references to roles title. This hierarchy should be captured declaratively somewhere.
210: private String getEffectiveRole(Collection roles) {
211: if (hasRole(roles, "admin")) {
212: return "admin";
213: } else if (hasRole(roles, "editor")) {
214: return "editor";
215: } else if (hasRole(roles, "viewer")) {
216: return "viewer";
217: } else {
218: return "none";
219: }
220: }
221:
222: public String isRoleSelected(String role, int projectId)
223: throws AuthenticationException {
224: return getEffectiveRole(getRoles(projectId)).equals(role) ? "selected='selected'"
225: : "";
226: }
227:
228: public Collection getRoles(int projectId)
229: throws AuthenticationException {
230: return SystemAuthorizer.get().getRolesForPrincipalOnProject(
231: getId(), projectId, false);
232: }
233:
234: public ArrayList getProjectIds() {
235: return projectIds;
236: }
237:
238: public void setProjectIds(ArrayList projectIds) {
239: this .projectIds = projectIds;
240: }
241:
242: public ArrayList getProjectRoles() {
243: return projectRoles;
244: }
245:
246: public void setProjectRoles(ArrayList projectRoles) {
247: this .projectRoles = projectRoles;
248: }
249:
250: public boolean isSysAdmin() throws AuthenticationException {
251: return CollectionUtils.find(getRoles(0), new Predicate() {
252: public boolean evaluate(Object o) {
253: return (((Role) o).getName().equals("sysadmin"));
254: }
255: }) != null;
256: }
257: }
|