KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > actions > ShowAvailableRolesDispatchAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
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. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security.actions;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.struts.Globals;
28 import org.apache.struts.action.ActionForm;
29 import org.apache.struts.action.ActionForward;
30 import org.apache.struts.action.ActionMapping;
31 import org.apache.struts.action.ActionMessage;
32 import org.apache.struts.action.ActionMessages;
33
34 import com.sslexplorer.boot.Util;
35 import com.sslexplorer.core.CoreAttributeConstants;
36 import com.sslexplorer.core.CoreEvent;
37 import com.sslexplorer.core.CoreEventConstants;
38 import com.sslexplorer.core.CoreServlet;
39 import com.sslexplorer.core.CoreUtil;
40 import com.sslexplorer.core.UserDatabaseManager;
41 import com.sslexplorer.policyframework.Permission;
42 import com.sslexplorer.policyframework.PolicyConstants;
43 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
44 import com.sslexplorer.policyframework.PolicyUtil;
45 import com.sslexplorer.security.Constants;
46 import com.sslexplorer.security.LogonControllerFactory;
47 import com.sslexplorer.security.Role;
48 import com.sslexplorer.security.SessionInfo;
49 import com.sslexplorer.security.UserDatabase;
50 import com.sslexplorer.security.forms.ShowAvailableRolesForm;
51 import com.sslexplorer.table.actions.AbstractPagerAction;
52
53 /**
54  * Implementation of an {@link AbstractPagerAction} that lists all of the
55  * configured <i>Groups</i> (previously known as <i>Roles</i>).
56  * <p>
57  * Depending onf the user database in use, different actions will be available
58  * (edit. create or delete).
59  * <p>
60  * With user databases that do not support account creation, the admin will
61  * be able to use the edit function, but this will not allow any information
62  * to be changed, only viewd.
63  *
64  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
65  */

66 public class ShowAvailableRolesDispatchAction extends AbstractPagerAction {
67     final static Log log = LogFactory.getLog(ShowAvailableRolesDispatchAction.class);
68     
69     /**
70      * Constructor.
71      */

72     public ShowAvailableRolesDispatchAction() {
73         super(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE, new Permission[] {
74                         PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN,
75                         PolicyConstants.PERM_DELETE
76         });
77     }
78
79     /* (non-Javadoc)
80      * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
81      */

82     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
83                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
84         return list(mapping, form, request, response);
85     }
86
87     /**
88      * List of all the available roles.
89      *
90      * @param mapping mapping
91      * @param form form
92      * @param request request
93      * @param response response
94      * @return forward
95      * @throws Exception on any error
96      */

97     public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
98                     throws Exception JavaDoc {
99         CoreUtil.clearFlow(request);
100         
101         try {
102             UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm());
103             Role[] roles = null;
104             try {
105                 roles = udb.listAllRoles("*");
106             } catch (Exception JavaDoc e) {
107                 log.error("Failed to get available roles.", e);
108                 ActionMessages errs = new ActionMessages();
109                 errs.add(Globals.ERROR_KEY, new ActionMessage("availableRoles.cannotListRoles", Util.getExceptionMessageChain(e)));
110                 saveErrors(request, errs);
111                 roles = new Role[0];
112             }
113             ((ShowAvailableRolesForm) form).initialize(roles, request.getSession());
114             ActionMessages msgs = new ActionMessages();
115             if (!udb.supportsAccountCreation()) {
116                 msgs.add(Globals.MESSAGE_KEY, new ActionMessage("availableRoles.noRoleCreation.text"));
117             }
118             if (msgs.size() > 0) {
119                 saveMessages(request, msgs);
120             }
121         } catch (Exception JavaDoc ex) {
122             log.error("Failed to reset administration form", ex);
123         }
124         return mapping.findForward("success");
125     }
126
127
128     /**
129      * Create a new role.
130      *
131      * @param mapping mapping
132      * @param form form
133      * @param request request
134      * @param response response
135      * @return forward
136      * @throws Exception on any error
137      */

138     public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
139                     throws Exception JavaDoc {
140         return mapping.findForward("create");
141     }
142
143     /**
144      * Edit an existing role.
145      *
146      * @param mapping mapping
147      * @param form form
148      * @param request request
149      * @param response response
150      * @return forward
151      * @throws Exception on any error
152      */

153     public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
154                     throws Exception JavaDoc {
155         UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm());
156         Role r = udb.getRole(
157                         ((ShowAvailableRolesForm) form).getSelectedItem());
158         request.setAttribute(Constants.EDITING_ITEM, r);
159         return mapping.findForward("edit");
160     }
161
162     /**
163      * Confirm deletion of an existing role.
164      *
165      * @param mapping mapping
166      * @param form form
167      * @param request request
168      * @param response response
169      * @return forward
170      * @throws Exception on any error
171      */

172     public ActionForward confirmRoleDeletion(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
173     throws Exception JavaDoc {
174         PolicyUtil.checkPermission(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE, PolicyConstants.PERM_DELETE, request);
175         String JavaDoc rolename = request.getParameter("rolename");
176         if (rolename == null) {
177             ActionMessages mesgs = new ActionMessages();
178             mesgs.add(Globals.ERROR_KEY, new ActionMessage("availableRoles.singleRoleNotSelected"));
179             saveErrors(request, mesgs);
180             return list(mapping, form, request, response);
181         } else {
182             return mapping.findForward("confirmRoleDeletion");
183         }
184     }
185
186     /**
187      * Delete an existing role.
188      *
189      * @param mapping mapping
190      * @param form form
191      * @param request request
192      * @param response response
193      * @return forward
194      * @throws Exception on any error
195      */

196     public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
197                     throws Exception JavaDoc {
198         PolicyUtil.checkPermission(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE, PolicyConstants.PERM_DELETE, request);
199         UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm());
200         String JavaDoc rolename = request.getParameter("rolename");
201         Role role = udb.getRole(rolename);
202         SessionInfo info = this.getSessionInfo(request);
203         try {
204             // Revoke all polices from the user
205
PolicyDatabaseFactory.getInstance().revokeAllPoliciesFromPrincipal(role);
206             
207             udb.deleteRole(rolename);
208             CoreServlet.getServlet().fireCoreEvent(
209                             new CoreEvent(this, CoreEventConstants.GROUP_REMOVED, role, info)
210                                     .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, rolename));
211             return mapping.findForward("refresh");
212         } catch (Exception JavaDoc e) {
213             CoreServlet.getServlet().fireCoreEvent(
214                             new CoreEvent(this, CoreEventConstants.GROUP_REMOVED, role, info, CoreEvent.STATE_UNSUCCESSFUL)
215                                     .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, rolename));
216             throw e;
217         }
218         finally{
219             // we need to reset the menu items as they could have changed here.
220
LogonControllerFactory.getInstance().applyMenuItemChanges(request);
221         }
222     }
223
224     /* (non-Javadoc)
225      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
226      */

227     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
228         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
229     }
230 }
Popular Tags