001: package com.technoetic.xplanner.forms;
002:
003: import com.technoetic.xplanner.domain.Role;
004: import com.technoetic.xplanner.security.AuthenticationException;
005: import com.technoetic.xplanner.security.auth.SystemAuthorizer;
006: import org.apache.commons.collections.CollectionUtils;
007: import org.apache.commons.collections.Predicate;
008: import org.apache.struts.action.ActionErrors;
009: import org.apache.struts.action.ActionMapping;
010:
011: import javax.servlet.http.HttpServletRequest;
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.Iterator;
015:
016: public class RoleEditorForm extends AbstractEditorForm {
017: private ArrayList personIds = new ArrayList();
018: private ArrayList personRoles = new ArrayList();
019:
020: public ActionErrors validate(ActionMapping mapping,
021: HttpServletRequest request) {
022: ActionErrors errors = new ActionErrors();
023: return errors;
024: }
025:
026: public void reset(ActionMapping mapping, HttpServletRequest request) {
027: super .reset(mapping, request);
028: }
029:
030: public void reset() {
031:
032: }
033:
034: /* Renvoi du nombre de personnes concernées par le projet*/
035: public int getPersonCount() {
036: return personIds.size();
037: }
038:
039: /* Renvoi de la valeur de l'id d'une personne du tableau*/
040:
041: public String getPersonId(int index) {
042: return (String) personIds.get(index);
043: }
044:
045: public int getPersonIdAsInt(int index) {
046: if (getPersonId(index) == null) {
047: return -1;
048: } else
049: return Integer.parseInt(getPersonId(index));
050: }
051:
052: public void setPersonId(int index, String personId) {
053: ensureSize(personIds, index + 1);
054: personIds.set(index, personId);
055:
056: }
057:
058: /* Définir le rôle d'une personne*/
059: public void setPersonRole(int index, String role) {
060: ensureSize(personRoles, index + 1);
061: personRoles.set(index, role);
062: }
063:
064: /* Renvoi du rôle d'une personne*/
065: public String getPersonRole(int index) {
066: return (String) personRoles.get(index);
067: }
068:
069: private boolean hasRole(Collection roles, String name) {
070: for (Iterator iterator = roles.iterator(); iterator.hasNext();) {
071: Role role = (Role) iterator.next();
072: if (role.getName().equals(name)) {
073: return true;
074: }
075: }
076: return false;
077: }
078:
079: private String getEffectiveRole(Collection roles) {
080: if (hasRole(roles, "admin")) {
081: return "admin";
082: } else if (hasRole(roles, "editor")) {
083: return "editor";
084: } else if (hasRole(roles, "viewer")) {
085: return "viewer";
086: } else {
087: return "none";
088: }
089: }
090:
091: public String isRoleSelected(String role, int personId)
092: throws AuthenticationException {
093: return getEffectiveRole(getRoles(personId)).equals(role) ? "selected='selected'"
094: : "";
095: }
096:
097: public Collection getRoles(int personId)
098: throws AuthenticationException {
099: return SystemAuthorizer.get().getRolesForPrincipalOnProject(
100: personId, getId(), false);
101: }
102:
103: public boolean isSysAdmin() throws AuthenticationException {
104: return CollectionUtils.find(getRoles(0), new Predicate() {
105: public boolean evaluate(Object o) {
106: return (((Role) o).getName().equals("sysadmin"));
107: }
108: }) != null;
109: }
110:
111: }
|