KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > actions > admin > ManageRoles


1 package org.tigris.scarab.actions.admin;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49
50 // JDK classes
51

52 // Turbine Stuff
53
import org.apache.turbine.TemplateContext;
54 import org.apache.turbine.RunData;
55 import org.apache.turbine.tool.IntakeTool;
56 import org.apache.fulcrum.intake.model.Group;
57 import org.apache.fulcrum.security.TurbineSecurity;
58 import org.apache.fulcrum.security.entity.Role;
59 import org.apache.fulcrum.security.entity.Permission;
60 import org.apache.fulcrum.security.util.EntityExistsException;
61 import org.apache.fulcrum.security.util.PermissionSet;
62
63 // Scarab Stuff
64
import org.tigris.scarab.om.ScarabUser;
65 import org.tigris.scarab.tools.ScarabLocalizationTool;
66 import org.tigris.scarab.util.ScarabConstants;
67 import org.tigris.scarab.actions.base.RequireLoginFirstAction;
68
69 /**
70  * This class is responsible for dealing with the role management
71  * Action(s).
72  *
73  * @author <a HREF="mailto:dr@bitonic.com">Douglas B. Robertson</a>
74  * @version $Id: ManageRoles.java 9240 2004-11-05 09:06:25Z dep4b $
75  */

76 public class ManageRoles extends RequireLoginFirstAction
77 {
78     
79     /**
80      * Go to the Add Role page
81      */

82     public void doGotoaddrole(RunData data, TemplateContext context)
83         throws Exception JavaDoc
84     {
85         setTarget(data, "admin,AddRole.vm");
86     }
87     
88     /**
89      * Go to the Edit Role page
90      */

91     public void doGotoeditrole(RunData data, TemplateContext context)
92         throws Exception JavaDoc
93     {
94         checkParamValidity(data, context, "admin,EditRole.vm");
95     }
96     
97     /**
98      * Go to the Delete Role page
99      */

100     public void doGotodeleterole(RunData data, TemplateContext context)
101         throws Exception JavaDoc
102     {
103         checkParamValidity(data, context, "admin,DeleteRole.vm");
104     }
105     
106     /**
107      * Manages the adding of a new role when the 'Add Role' button is pressed.
108      */

109     public void doAddrole(RunData data, TemplateContext context)
110         throws Exception JavaDoc
111     {
112         IntakeTool intake = getIntakeTool(context);
113         ScarabLocalizationTool l10n = getLocalizationTool(context);
114
115         if (intake.isAllValid())
116         {
117             Object JavaDoc user = data.getUser().getTemp(ScarabConstants.SESSION_REGISTER);
118             
119             Group editRole = null;
120             if (user != null && user instanceof ScarabUser)
121             {
122                 editRole = intake.get("EditRole", ((ScarabUser)user).getQueryKey(), false);
123             }
124             else
125             {
126                 editRole = intake.get("EditRole", IntakeTool.DEFAULT_KEY, false);
127             }
128             String JavaDoc name = editRole.get("RoleName").toString();
129             
130             try
131             {
132                 Role role = TurbineSecurity.getNewRole(null);
133                 role.setName(name);
134                 
135                 TurbineSecurity.addRole(role);
136
137                 String JavaDoc msg = l10n.format("RoleCreated", name);
138                 getScarabRequestTool(context).setConfirmMessage(msg);
139
140                 data.getParameters().setString("name", name);
141                 doGotoeditrole(data, context);
142             }
143             catch (EntityExistsException eee)
144             {
145                 String JavaDoc msg = l10n.format("RoleExists", name);
146                 getScarabRequestTool(context).setConfirmMessage(msg);
147             }
148         }
149     }
150     
151     /**
152      * Manages the editing of an existing role when the 'Update Role' button is pressed.
153      */

154     public void doEditrole(RunData data, TemplateContext context)
155         throws Exception JavaDoc
156     {
157         /*
158          * Grab the role we are trying to update.
159          */

160         String JavaDoc name = data.getParameters().getString("name");
161         checkParamValidity(data, context, null);
162         Role role = TurbineSecurity.getRole(name);
163         
164         /*
165          * Grab the permissions for the role we are
166          * dealing with.
167          */

168         PermissionSet rolePermissions = role.getPermissions();
169         
170         /*
171          * Grab all the permissions.
172          */

173         Permission[] permissions = TurbineSecurity.getAllPermissions()
174             .getPermissionsArray();
175         
176         String JavaDoc roleName = role.getName();
177         
178         for (int i = 0; i < permissions.length; i++)
179         {
180             String JavaDoc permissionName = permissions[i].getName();
181             String JavaDoc rolePermission = roleName + permissionName;
182             
183             String JavaDoc formRolePermission = data.getParameters().getString(rolePermission);
184             Permission permission = TurbineSecurity.getPermission(permissionName);
185             
186             
187             if (formRolePermission != null && !rolePermissions.contains(permission))
188             {
189                 /*
190                  * Checkbox has been checked AND the role doesn't already
191                  * contain this permission. So assign the permission to
192                  * the role.
193                  */

194                 
195                 role.grant(permission);
196             }
197             else if (formRolePermission == null && rolePermissions.contains(permission))
198             {
199                 /*
200                  * Checkbox has not been checked AND the role
201                  * contains this permission. So remove this
202                  * permission from the role.
203                  */

204                 role.revoke(permission);
205             }
206         }
207     }
208     
209     /**
210      * This manages the clicking of the 'Confirm Delete' button and actually
211      * deletes the Role.
212      */

213     public void doDeleterole(RunData data, TemplateContext context)
214         throws Exception JavaDoc
215     {
216         /*
217          * Grab the role we are trying to delete.
218          */

219         String JavaDoc name = data.getParameters().getString("name");
220         Role role = TurbineSecurity.getRole(name);
221         TurbineSecurity.removeRole(role);
222         
223         ScarabLocalizationTool l10n = getLocalizationTool(context);
224
225         String JavaDoc msg = l10n.format("RoleDeleted", name);
226         getScarabRequestTool(context).setConfirmMessage(msg);
227         setTarget(data, data.getParameters()
228                       .getString(ScarabConstants.NEXT_TEMPLATE, "admin,ManageRoles.vm"));
229     }
230     
231     
232     /**
233      This manages clicking the Cancel button
234      */

235     public void doCancel(RunData data, TemplateContext context) throws Exception JavaDoc
236     {
237         setTarget(data, data.getParameters()
238                       .getString(ScarabConstants.CANCEL_TEMPLATE, "admin,AdminIndex.vm"));
239     }
240     
241     /**
242      calls doCancel()
243      */

244     public void doPerform(RunData data, TemplateContext context)
245         throws Exception JavaDoc
246     {
247         doCancel(data,context);
248     }
249
250     /**
251      * Spit out an error message to the user if the "name" parameter
252      * is null or empty.
253      *
254      * @param target Page to go to if "name" parameter is present. If
255      * null then don't go anywhere.
256      */

257     protected void checkParamValidity(RunData data, TemplateContext context,
258                                       String JavaDoc target)
259     {
260         String JavaDoc name = data.getParameters().getString("name");
261
262         if (name == null || name.length() == 0)
263         {
264             ScarabLocalizationTool l10n = getLocalizationTool(context);
265             String JavaDoc msg = l10n.get("NoRoleSelected");
266             getScarabRequestTool(context).setConfirmMessage(msg);
267             setTarget(data, "admin,ManageRoles.vm");
268         }
269         else
270         {
271             if (target != null)
272             {
273                 setTarget(data, target);
274             }
275         }
276     }
277 }
278
Popular Tags