001: /*
002: * Copyright (c) Mateusz Prokopowicz. All Rights Reserved.
003: */
004:
005: package com.technoetic.xplanner.actions;
006:
007: import java.util.Iterator;
008: import java.util.Map;
009:
010: import org.apache.commons.lang.StringUtils;
011: import org.apache.log4j.Logger;
012:
013: import com.technoetic.xplanner.domain.Person;
014: import com.technoetic.xplanner.domain.repository.RepositoryException;
015: import com.technoetic.xplanner.domain.repository.RoleAssociationRepository;
016: import com.technoetic.xplanner.security.AuthenticationException;
017: import com.technoetic.xplanner.security.LoginModule;
018: import com.technoetic.xplanner.security.auth.Authorizer;
019: import com.technoetic.xplanner.security.auth.SystemAuthorizer;
020:
021: /**
022: * User: Mateusz Prokopowicz
023: * Date: Dec 9, 2004
024: * Time: 11:03:47 AM
025: */
026: public class EditPersonHelper {
027: private static Logger log = Logger
028: .getLogger(EditPersonHelper.class);
029: public static final String SYSADMIN_ROLE_NAME = "sysadmin";
030: private static final int ANY_PROJECT_ID = 0;
031: private RoleAssociationRepository roleAssociationRepository;
032: private Authorizer authorizer;
033:
034: public void setAuthorizer(Authorizer authorizer) {
035: Authorizer systemAuthorizer = SystemAuthorizer.get();
036: if (authorizer != systemAuthorizer) {
037: log.warn("Which authorizer do you want me to use? "
038: + authorizer + " or what SystemAuthorizer has, "
039: + systemAuthorizer + "?????");
040: }
041:
042: this .authorizer = authorizer;
043: }
044:
045: public void setRoleAssociationRepository(
046: RoleAssociationRepository roleAssociationRepository) {
047: this .roleAssociationRepository = roleAssociationRepository;
048: }
049:
050: public void modifyRoles(Map projectRoleMap, Person person,
051: boolean isSystemAdmin, int remoteUserId)
052: throws AuthenticationException, RepositoryException {
053: for (Iterator iterator = projectRoleMap.keySet().iterator(); iterator
054: .hasNext();) {
055: String projectId = (String) iterator.next();
056: String role = (String) projectRoleMap.get(projectId);
057: if (isCurrentUserAdminOfProject(
058: Integer.parseInt(projectId), remoteUserId)) {
059: setRoleOnProject(Integer.parseInt(projectId), person,
060: role);
061: }
062: }
063: if (isCurrentUserAdminOfProject(ANY_PROJECT_ID, remoteUserId)) {
064: roleAssociationRepository.deleteForPersonOnProject(
065: SYSADMIN_ROLE_NAME, person.getId(), ANY_PROJECT_ID);
066: if (isSystemAdmin) {
067: setSysadmin(person);
068: }
069: }
070: }
071:
072: void setSysadmin(Person person) throws RepositoryException {
073: addRoleAssociationForProject(ANY_PROJECT_ID, person.getId(),
074: SYSADMIN_ROLE_NAME);
075: }
076:
077: public void setRoleOnProject(int projectId, Person person,
078: String role) throws RepositoryException {
079: deleteRoleAssociationsForProject(projectId, person.getId());
080: addRoleAssociationForProject(projectId, person.getId(), role);
081: }
082:
083: private boolean isCurrentUserAdminOfProject(int projectId,
084: int remoteUserId) throws AuthenticationException {
085: return authorizer.hasPermission(projectId, remoteUserId,
086: "system.project", projectId, "admin.edit.role");
087: }
088:
089: private void addRoleAssociationForProject(int projectId,
090: int personId, String roleName) throws RepositoryException {
091: roleAssociationRepository.insertForPersonOnProject(roleName,
092: personId, projectId);
093: }
094:
095: private void deleteRoleAssociationsForProject(int projectId,
096: int personId) throws RepositoryException {
097: roleAssociationRepository.deleteAllForPersonOnProject(personId,
098: projectId);
099: }
100:
101: public void changeUserPassword(String newPassword, String userId,
102: LoginModule loginModule) throws AuthenticationException {
103: if (StringUtils.isNotEmpty(newPassword)) {
104: if (loginModule != null) {
105: loginModule.changePassword(userId, newPassword);
106: }
107: }
108: }
109:
110: }
|