KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > user > AssignUserRolesAndGroupsAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.action.core.user;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.Group;
20 import com.blandware.atleap.model.core.Role;
21 import com.blandware.atleap.model.core.User;
22 import com.blandware.atleap.service.core.GroupManager;
23 import com.blandware.atleap.service.core.RoleManager;
24 import com.blandware.atleap.service.core.UserManager;
25 import com.blandware.atleap.webapp.acegi.UserManagerDaoImpl;
26 import com.blandware.atleap.webapp.action.core.BaseAction;
27 import com.blandware.atleap.webapp.form.core.SelectRolesAndGroupsForm;
28 import com.blandware.atleap.webapp.util.core.WebappConstants;
29 import com.blandware.atleap.webapp.util.core.CacheUtil;
30 import org.apache.commons.validator.GenericValidator;
31 import org.apache.struts.action.*;
32 import org.springframework.orm.ObjectOptimisticLockingFailureException;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39
40 /**
41  * <p>Assigns Roles and Groups to User
42  * </p>
43  * <p><a HREF="AssignUserRolesAndGroupsAction.java.htm"><i>View Source</i></a></p>
44  * <p/>
45  *
46  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
47  * @version $Revision: 1.4 $ $Date: 2006/03/25 11:27:53 $
48  * @struts.action path="/core/user/assignRolesAndGroups"
49  * name="selectRolesAndGroupsForm"
50  * scope="request"
51  * input="inputForward"
52  * validate="true"
53  * roles="core-user-assignRolesAndGroups"
54  * @struts.action-forward name="inputForward"
55  * path=".core.user.assignRolesAndGroups"
56  * @struts.action-forward name="viewUser"
57  * path="/core/user/view.do"
58  * redirect="true"
59  * @struts.action-forward name="listUsers"
60  * path="/core/user/list.do"
61  * redirect="true"
62  * @struts.action-forward name="callAssignRolesAndGroups"
63  * path="/core/user/callAssignRolesAndGroups.do"
64  * redirect="false"
65  * @struts.action-forward name="unsatisfiable"
66  * path="/core/user/list.do"
67  */

68 public final class AssignUserRolesAndGroupsAction extends BaseAction {
69     /**
70      * @param mapping The ActionMapping used to select this instance
71      * @param form The optional ActionForm bean for this request (if any)
72      * @param request The HTTP request we are proceeding
73      * @param response The HTTP response we are creating
74      * @return an ActionForward instance describing where and how
75      * control should be forwarded, or null if response
76      * has already been completed
77      */

78     public ActionForward execute(ActionMapping mapping, ActionForm form,
79                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
80
81         if ( !isCancelled(request) ) {
82
83             String JavaDoc userName = (String JavaDoc) request.getSession().getAttribute(WebappConstants.USER_NAME_KEY);
84             if ( GenericValidator.isBlankOrNull(userName) ) {
85                 if ( log.isWarnEnabled() ) {
86                     log.warn("Missing user name. Returning to list...");
87                 }
88                 return mapping.findForward("listUsers");
89             }
90
91             // acquire POJOs for all selected roles
92
RoleManager roleManager = (RoleManager) getBean(Constants.ROLE_MANAGER_BEAN);
93             SelectRolesAndGroupsForm selectRolesAndGroupsForm = (SelectRolesAndGroupsForm) form;
94             String JavaDoc[] selectedRoleNames = selectRolesAndGroupsForm.getSelectedRoles();
95
96             List JavaDoc roles = new ArrayList JavaDoc();
97             for ( int i = 0; i < selectedRoleNames.length; i++ ) {
98                 String JavaDoc roleName = selectedRoleNames[i];
99                 Role role = roleManager.retrieveRole(roleName);
100                 if ( role != null ) {
101                     roles.add(role);
102                 }
103             }
104
105             // acquire POJOs for all selected groups
106
GroupManager groupManager = (GroupManager) getBean(Constants.GROUP_MANAGER_BEAN);
107             String JavaDoc[] selectedGroupNames = selectRolesAndGroupsForm.getSelectedGroups();
108
109             List JavaDoc groups = new ArrayList JavaDoc();
110             for ( int i = 0; i < selectedGroupNames.length; i++ ) {
111                 String JavaDoc groupName = selectedGroupNames[i];
112                 Group group = groupManager.retrieveGroup(groupName);
113                 if ( group != null ) {
114                     groups.add(group);
115                 }
116             }
117
118             // update user roles and groups
119
UserManager userManager = (UserManager) getBean(Constants.USER_MANAGER_BEAN);
120             User user = userManager.retrieveUser(userName);
121
122             if ( user == null ) {
123                 // user not found. it might be deleted by someone else
124
ActionMessages errors = new ActionMessages();
125                 errors.add("userNotFound", new ActionMessage("core.user.errors.notFound"));
126                 saveErrors(request, errors);
127                 return mapping.findForward("listUsers");
128             }
129
130             user.setVersion(Long.valueOf(selectRolesAndGroupsForm.getVersion()));
131
132             // remove roles which were unselected
133
// list of roles is copied in order to prevent ConcurrentModificationException to be thrown
134
List JavaDoc userRoles = new ArrayList JavaDoc(user.getFreeRoles());
135             for ( int i = 0; i < userRoles.size(); i++ ) {
136                 Role role = (Role) userRoles.get(i);
137                 if ( !roles.contains(role) ) {
138                     user.removeFreeRole(role);
139                 }
140             }
141             // add new roles
142
for ( Iterator JavaDoc i = roles.iterator(); i.hasNext(); ) {
143                 Role role = (Role) i.next();
144                 user.addFreeRole(role);
145             }
146
147             // remove groups which were unselected
148
// list of groups is copied in order to prevent ConcurrentModificationException to be thrown
149
List JavaDoc userGroups = new ArrayList JavaDoc(user.getGroups());
150             for ( int i = 0; i < userGroups.size(); i++ ) {
151                 Group group = (Group) userGroups.get(i);
152                 if ( !groups.contains(group) ) {
153                     group.removeUser(user);
154                     List JavaDoc groupRoles = group.getRoles();
155                     for (int j = 0; j < groupRoles.size(); j++) {
156                         Role role = (Role) groupRoles.get(j);
157                         user.removeRole(role, group);
158                     }
159                 }
160             }
161             // add new groups
162
for ( Iterator JavaDoc i = groups.iterator(); i.hasNext(); ) {
163                 Group group = (Group) i.next();
164                 group.addUser(user);
165                 List JavaDoc groupRoles = group.getRoles();
166                 for (int j = 0; j < groupRoles.size(); j++) {
167                     Role role = (Role) groupRoles.get(j);
168                     user.addRole(role, group);
169                 }
170             }
171
172             try {
173                 userManager.updateUser(user);
174
175                 if (user.getName().equals(request.getRemoteUser())) {
176                     UserManagerDaoImpl userManagerDaoImpl = (UserManagerDaoImpl) getBean(Constants.USER_DETAILS_SERVICE_BEAN);
177                     userManagerDaoImpl.updateUser(user);
178                 }
179             } catch ( ObjectOptimisticLockingFailureException e ) {
180                 // user was updated or deleted by another transaction
181
ActionMessages errors = new ActionMessages();
182                 errors.add("updateFailed", new ActionMessage("core.user.errors.updateFailed"));
183                 saveErrors(request, errors);
184                 request.setAttribute(WebappConstants.USER_NAME_KEY, userName);
185                 return mapping.findForward("callAssignRolesAndGroups");
186             }
187
188             // flush CP cache to avoid a situation when user permissions for
189
// CP menu items are changed but he/she still obtains cached version
190
// of page
191
CacheUtil cacheUtil = CacheUtil.getInstance(request);
192             cacheUtil.flushContentPageCache();
193         }
194         return mapping.findForward("listUsers");
195     }
196 }
Popular Tags