1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.control.form;
16
17 import org.figure8.join.control.JoinForm;
18 import org.figure8.join.control.action.UserActions;
19 import org.figure8.join.control.action.PermissionActions;
20 import org.figure8.join.businessobjects.security.User;
21 import org.figure8.join.businessobjects.security.Permission;
22
23 import org.apache.struts.action.ActionMapping;
24
25 import javax.servlet.http.HttpSession;
26 import javax.servlet.http.HttpServletRequest;
27
28 import java.util.Map;
29 import java.util.List;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.ArrayList;
33 /**
34 * Form object used for manipulating Roles and Permissions into Join application.
35 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
36 * @version $Revision: 1.1 $
37 *
38 * @struts.form name="permissionForm"
39 */
40 public class PermissionForm extends JoinForm{
41
42
43
44 /** Name of security role associated to permission */
45 private String name;
46 /** Description of security role associated to permission */
47 private String description;
48 /** FQN of PermissionResourceResolver implementation to use for role */
49 private String permissionResourceResolverClass;
50
51 /** The unique identifier of user associated to permissions */
52 private String userId;
53
54 /**
55 * The map of security roles endorsed by the associated user. Keys
56 * are the name of role, values are boolean telling if role is acquired.
57 */
58 private Map roles = new HashMap();
59 /**
60 * The map of permissions resources for the associated user. Keys are the name
61 * of security roles, values are array of resource identifiers.
62 */
63 private Map resources = new HashMap();
64
65
66
67
68 /** Creates a new instance of PermissionForm. */
69 public PermissionForm(){
70 }
71
72
73
74
75 /** @return Name of security role associated to permission */
76 public String getName(){
77 return name;
78 }
79 /** @param name Name of security role to associate with permission */
80 public void setName(String name){
81 this.name = name;
82 }
83 /** @return Description of security role associated to permission */
84 public String getDescription(){
85 return description;
86 }
87 /** @param description Description of security role to associate with permission */
88 public void setDescription(String description){
89 this.description = description;
90 }
91 /** @return FQN of PermissionResourceResolver implementation to use for role */
92 public String getPermissionResourceResolverClass(){
93 return permissionResourceResolverClass;
94 }
95 /** @param permissionResourceResolverClass FQN of PermissionResourceResolver implementation to use for role */
96 public void setPermissionResourceResolverClass(String permissionResourceResolverClass){
97 this.permissionResourceResolverClass = permissionResourceResolverClass;
98 }
99
100 /** @return The identifier of user currently associated with permissions */
101 public String getUserId(){
102 return userId;
103 }
104 /** @param userId The identifier of user to associate with permissions */
105 public void setUserId(String userId){
106 this.userId = userId;
107 }
108
109 /** @return Map of roles associated to user. */
110 public Map getRoles(){
111 return roles;
112 }
113 /**
114 * @param key Name of role to get association for
115 * @return "on" string if role is associated to user, null otherwise
116 */
117 public Object getMappedRole(String key){
118 return roles.get(key);
119 }
120 /**
121 * @param key Name of role to set association for
122 * @param value Value of association ("on" || "off" || boolean value)
123 */
124 public void setMappedRole(String key, Object value){
125 roles.put(key, value);
126 }
127
128 /** @return Map of permissions resources associated to user. */
129 public Map getResources(){
130 return resources;
131 }
132 /**
133 * @param key Name of role to get resources for
134 * @return Array of resources for permissions, null if no permission
135 */
136 public String[] getMappedResourcesAsStrings(String key){
137 String[] result = null;
138 Object[] objects = (Object[])resources.get(key);
139 if (objects != null){
140 result = new String[objects.length];
141 for (int i=0; i<objects.length; i++)
142 result[i] = objects[i].toString();
143 }
144 return result;
145 }
146 /**
147 * @param key Name of role to get resources for
148 * @return Array of resources for permissions, null if no permission
149 */
150 public Object[] getMappedResources(String key){
151 return (Object[])resources.get(key);
152 }
153 /**
154 * @param key Name of role to set permissions for
155 * @param value Array of resources for permissions
156 */
157 public void setMappedResources(String key, Object[] value){
158 resources.put(key, value);
159 }
160
161 /**
162 * @param permission Existing user's permission to add to mapped ones
163 */
164 public void addPermission(Permission permission){
165
166 setMappedRole(permission.getRole().getName(), "on");
167
168 if (permission.isResourcePermission()){
169 String[] res = getMappedResourcesAsStrings(permission.getRole().getName());
170 if (res == null) res = new String[]{};
171
172 ArrayList resList = new ArrayList(Arrays.asList(res));
173 resList.add(permission.getResourceId());
174
175 res = (String[])resList.toArray(res);
176 setMappedResources(permission.getRole().getName(), res);
177 }
178 }
179
180
181
182
183 /**
184 * Validation of form attributes.
185 * @param operation String representing the operation to invoke on Action
186 * @param mapping Mapping between forwards name and path for this action
187 * @param request The servlet container request wrapper
188 */
189 public void doValidate(String operation, ActionMapping mapping, HttpServletRequest request){
190
191 if (PermissionActions.LOAD_ROLE_OP.equals(operation)){
192
193 validateEntityObjectId();
194 }
195 else if (PermissionActions.SAVE_ROLE_OP.equals(operation)){
196
197 if (name == null || name.length() == 0)
198 addActionError("errors.required", getGuiMessageResources().getMessage("label.role.name"));
199
200 if (description == null || description.length() == 0)
201 addActionError("errors.required", getGuiMessageResources().getMessage("label.role.description"));
202 }
203 else if (PermissionActions.LOAD_PERMISSIONS_OP.equals(operation)){
204
205 if (userId == null || userId.length() == 0){
206 HttpSession session = request.getSession();
207 User user = (User)session.getAttribute(UserActions.USER_KEY);
208 if (user == null)
209 addActionError("errors.required", getGuiMessageResources().getMessage("label.permission.associateduser"));
210 }
211 }
212 }
213
214
215
216
217 /**
218 * Reset form attributes.
219 * @param mapping Mapping between forwards name and path for this action
220 * @param request The servlet container request wrapper
221 */
222 public void reset(ActionMapping mapping, HttpServletRequest request){
223 super.reset(mapping, request);
224 name = null;
225 description = null;
226 permissionResourceResolverClass = null;
227 userId = null;
228 roles = new HashMap();
229 resources = new HashMap();
230 }
231 }