01: package com.technoetic.xplanner.actions;
02:
03: import com.technoetic.xplanner.domain.Project;
04: import com.technoetic.xplanner.domain.Role;
05: import com.technoetic.xplanner.domain.RoleAssociation;
06: import com.technoetic.xplanner.forms.RoleEditorForm;
07: import com.technoetic.xplanner.security.AuthenticationException;
08: import com.technoetic.xplanner.security.SecurityHelper;
09: import com.technoetic.xplanner.security.auth.SystemAuthorizer;
10: import net.sf.hibernate.Hibernate;
11: import net.sf.hibernate.HibernateException;
12: import net.sf.hibernate.Session;
13: import net.sf.hibernate.type.Type;
14: import org.apache.struts.action.ActionForm;
15: import org.apache.struts.action.ActionMapping;
16:
17: import javax.servlet.http.HttpServletRequest;
18: import javax.servlet.http.HttpServletResponse;
19: import java.util.List;
20:
21: public class EditRoleAction extends EditObjectAction {
22: public static final String SYSADMIN_ROLE_NAME = "sysadmin";
23:
24: protected void beforeObjectCommit(Object object, Session session,
25: ActionMapping actionMapping, ActionForm actionForm,
26: HttpServletRequest request, HttpServletResponse reply)
27: throws Exception {
28:
29: RoleEditorForm roleForm = (RoleEditorForm) actionForm;
30:
31: Project project = (Project) object;
32:
33: int projectId = project.getId();
34:
35: for (int index = 0; index < roleForm.getPersonCount(); index++) {
36: int personId = roleForm.getPersonIdAsInt(index);
37:
38: if (isAuthorizedRoleAdministratorForProject(request,
39: projectId)) {
40:
41: deleteRoleAssociationsForProject(session, projectId,
42: personId);
43: addRoleAssociationForProject(session, projectId,
44: personId, roleForm.getPersonRole(index));
45: }
46: }
47: }
48:
49: private boolean isAuthorizedRoleAdministratorForProject(
50: HttpServletRequest request, int projectId)
51: throws AuthenticationException {
52: return SystemAuthorizer.get().hasPermission(projectId,
53: SecurityHelper.getRemoteUserId(request),
54: "system.project", projectId, "admin.edit.role");
55: }
56:
57: private void addRoleAssociationForProject(Session session,
58: int projectId, int personId, String roleName)
59: throws HibernateException {
60: Role role = getRoleByName(session, roleName);
61: if (role != null) {
62: session.save(new RoleAssociation(projectId, personId, role
63: .getId()));
64: }
65: }
66:
67: private void deleteRoleAssociationsForProject(Session session,
68: int projectId, int personId) throws HibernateException {
69: session.delete("from assoc in " + RoleAssociation.class
70: + " where assoc.personId = ? and assoc.projectId = ?",
71: new Object[] { new Integer(personId),
72: new Integer(projectId) }, new Type[] {
73: Hibernate.INTEGER, Hibernate.INTEGER });
74: }
75:
76: private Role getRoleByName(Session session, String roleName)
77: throws HibernateException {
78: List role = session.find("from role in " + Role.class
79: + " where role.name = ?", roleName, Hibernate.STRING);
80: if (role.size() > 0) {
81: return (Role) role.get(0);
82: }
83: return null;
84: }
85:
86: }
|