KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > policyframework > actions > AccessRightsDispatchAction


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.policyframework.actions;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.Globals;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34 import org.apache.struts.action.ActionMessage;
35 import org.apache.struts.action.ActionMessages;
36
37 import com.sslexplorer.core.forms.CoreForm;
38 import com.sslexplorer.input.MultiSelectDataSource;
39 import com.sslexplorer.policyframework.AccessRight;
40 import com.sslexplorer.policyframework.AccessRights;
41 import com.sslexplorer.policyframework.DelegatedPoliciesDataSource;
42 import com.sslexplorer.policyframework.NoPermissionException;
43 import com.sslexplorer.policyframework.Permission;
44 import com.sslexplorer.policyframework.PolicyConstants;
45 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
46 import com.sslexplorer.policyframework.Resource;
47 import com.sslexplorer.policyframework.ResourceUtil;
48 import com.sslexplorer.policyframework.forms.AbstractResourceForm;
49 import com.sslexplorer.policyframework.forms.AccessRightsForm;
50 import com.sslexplorer.security.LogonControllerFactory;
51 import com.sslexplorer.security.SessionInfo;
52
53
54 /**
55  * Implementation of a {@link com.sslexplorer.policyframework.actions.AbstractResourceDispatchAction}
56  * that allows viewing and editing of a single <i>Resource Permission</i>.
57  *
58  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
59  */

60 public class AccessRightsDispatchAction extends AbstractResourceDispatchAction {
61
62     final static Log log = LogFactory.getLog(AccessRightsDispatchAction.class);
63     
64     /**
65      * Constructor
66      */

67     public AccessRightsDispatchAction() {
68         super(PolicyConstants.ACCESS_RIGHTS_RESOURCE_TYPE, null, null, null, null, null);
69     }
70
71     /* (non-Javadoc)
72      * @see com.sslexplorer.policyframework.actions.AbstractResourceDispatchAction#createResource(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
73      */

74     public Resource createResource(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
75                     throws Exception JavaDoc {
76         throw new Exception JavaDoc("Create not supported.");
77     }
78
79     /**
80      * Add the selected permission to the list
81      *
82      * @param mapping mapping
83      * @param form form
84      * @param request request
85      * @param response response
86      * @return forward
87      * @throws Exception on any error
88      */

89     public ActionForward addSelected(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
90                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
91         AccessRightsForm drpf = (AccessRightsForm) form;
92         String JavaDoc[] sel = drpf.getSelectedAvailablePermissions();
93         List JavaDoc l = drpf.getResourceTypePermissions();
94         for (int i = 0; i < sel.length; i++) {
95             l.add(new AccessRight(drpf.getResourceType(), drpf.getResourceType().getPermission(
96                 Integer.parseInt(sel[i]))));
97         }
98         drpf.rebuild();
99         return display(mapping, form, request, response);
100     }
101
102     /**
103      * Add the selected permission to the list
104      *
105      * @param mapping mapping
106      * @param form form
107      * @param request request
108      * @param response response
109      * @return forward
110      * @throws Exception on any error
111      */

112     public ActionForward removeSelected(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
113                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
114         AccessRightsForm drpf = (AccessRightsForm) form;
115         String JavaDoc[] sel = drpf.getSelectedResourceTypePermissions();
116         if(sel != null) {
117             List JavaDoc l = drpf.getResourceTypePermissions();
118             for (int i = 0; i < sel.length; i++) {
119                 int idx = sel[i].indexOf('-');
120                 int resourceTypeId = Integer.parseInt(sel[i].substring(0, idx));
121                 int permissionId = Integer.parseInt(sel[i].substring(idx + 1));
122                 AccessRight sp = null;
123                 for (Iterator JavaDoc j = l.iterator(); j.hasNext() && sp == null;) {
124                     AccessRight p = (AccessRight) j.next();
125                     if (p.getResourceType().getResourceTypeId() == resourceTypeId && p.getPermission().getId() == permissionId) {
126                         sp = p;
127                     }
128                 }
129                 if (sp != null) {
130                     l.remove(sp);
131                 }
132             }
133             drpf.rebuild();
134         }
135         return display(mapping, form, request, response);
136     }
137
138     /**
139      * Change the selected resource type
140      *
141      * @param mapping mapping
142      * @param form form
143      * @param request request
144      * @param response response
145      * @return forward
146      * @throws Exception on error
147      */

148     public ActionForward selectResourceType(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
149                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
150         AccessRightsForm drpf = (AccessRightsForm) form;
151         drpf.setResourceType(PolicyDatabaseFactory.getInstance().getResourceType(drpf.getSelectedResourceType()));
152         drpf.rebuild();
153         return display(mapping, form, request, response);
154     }
155
156     /* (non-Javadoc)
157      * @see com.sslexplorer.policyframework.actions.AbstractResourceDispatchAction#commit(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
158      */

159     public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
160                     throws Exception JavaDoc {
161         ActionMessages msgs = new ActionMessages();
162         msgs.add(Globals.MESSAGE_KEY, new ActionMessage("editAccessRights.message.saved"));
163         this.addMessages(request, msgs);
164         return super.commit(mapping, form, request, response);
165     }
166
167     /* (non-Javadoc)
168      * @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)
169      */

170     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
171         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
172     }
173
174     /* (non-Javadoc)
175      * @see com.sslexplorer.policyframework.actions.AbstractResourceDispatchAction#checkValid(com.sslexplorer.policyframework.Resource, com.sslexplorer.policyframework.Permission, org.apache.struts.action.ActionMapping, com.sslexplorer.policyframework.forms.AbstractResourceForm, javax.servlet.http.HttpServletRequest)
176      */

177     protected void checkValid(Resource r, Permission[] permissions, ActionMapping mapping, AbstractResourceForm form,
178                     HttpServletRequest JavaDoc request) throws NoPermissionException {
179         ResourceUtil.checkAccessRightsValid((AccessRights)r, this.getSessionInfo(request), ((CoreForm) form).getActionTarget());
180     }
181
182     /* (non-Javadoc)
183      * @see com.sslexplorer.policyframework.actions.AbstractResourceDispatchAction#createAvailablePoliciesDataSource(com.sslexplorer.policyframework.Resource, org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
184      */

185     protected MultiSelectDataSource createAvailablePoliciesDataSource(Resource resource, ActionMapping mapping, ActionForm form,
186                     HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
187         return new DelegatedPoliciesDataSource(null, null, ((AccessRights) resource).getAccessRightsClass(), LogonControllerFactory.getInstance().getUser(request));
188     }
189
190     @Override JavaDoc
191     protected void doUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
192         super.doUpdate(mapping, form, request, response);
193         // we now need to rebuild any menus, as more or less could be visable.
194
LogonControllerFactory.getInstance().applyMenuItemChanges(request);
195     }
196
197 }
Popular Tags