KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > policyframework > wizards > actions > AccessRightsPermissionsAction


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.wizards.actions;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32
33 import com.sslexplorer.core.CoreUtil;
34 import com.sslexplorer.policyframework.AccessRight;
35 import com.sslexplorer.policyframework.Permission;
36 import com.sslexplorer.policyframework.wizards.forms.AccessRightsPermissionsForm;
37 import com.sslexplorer.security.SessionInfo;
38 import com.sslexplorer.wizard.AbstractWizardSequence;
39 import com.sslexplorer.wizard.actions.AbstractWizardAction;
40 import com.sslexplorer.wizard.forms.AbstractWizardForm;
41
42 /**
43  * <p>
44  * Action for selecting access rights permissions
45  *
46  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
47  *
48  */

49 public class AccessRightsPermissionsAction extends AbstractWizardAction {
50
51     /**
52      * Constructor
53      */

54     public AccessRightsPermissionsAction() {
55         super();
56     }
57
58     /*
59      * (non-Javadoc)
60      *
61      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
62      * org.apache.struts.action.ActionForm,
63      * javax.servlet.http.HttpServletRequest,
64      * javax.servlet.http.HttpServletResponse)
65      */

66     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
67         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
68     }
69
70     /**
71      * @param mapping
72      * @param form
73      * @param request
74      * @param response
75      * @return ActionForward
76      * @throws Exception
77      */

78     public ActionForward selectResourceType(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
79                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
80         applyToSequence(mapping, (AbstractWizardForm) form, request, response);
81         return unspecified(mapping, form, request, response);
82     }
83
84     /*
85      * (non-Javadoc)
86      *
87      * @see com.sslexplorer.wizard.actions.AbstractWizardAction#unspecified(org.apache.struts.action.ActionMapping,
88      * org.apache.struts.action.ActionForm,
89      * javax.servlet.http.HttpServletRequest,
90      * javax.servlet.http.HttpServletResponse)
91      */

92     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
93                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
94         CoreUtil.addRequiredFieldMessage(this, request);
95         return super.unspecified(mapping, form, request, response);
96     }
97
98     /**
99      * @param mapping
100      * @param form
101      * @param request
102      * @param response
103      * @return ActionForward
104      * @throws Exception
105      */

106     public ActionForward addSelected(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
107                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
108         applyToSequence(mapping, (AbstractWizardForm) form, request, response);
109         AccessRightsPermissionsForm drpf = (AccessRightsPermissionsForm) form;
110         String JavaDoc[] sel = drpf.getSelectedAvailablePermissions();
111         AbstractWizardSequence seq = getWizardSequence(request);
112         List JavaDoc l = (List JavaDoc) seq.getAttribute(AccessRightsPermissionsForm.ATTR_RESOURCE_TYPE_PERMISSIONS, new ArrayList JavaDoc());
113         for (int i = 0; i < sel.length; i++) {
114             // Make sure the selected permission was one that the current user
115
// is actually allowed to use
116
Permission perm = drpf.getResourceType().getPermission(Integer.parseInt(sel[i]));
117             if (!drpf.getAvailablePermissions().contains(perm)) {
118                 throw new Exception JavaDoc("Permission denied.");
119             }
120             l.add(new AccessRight(drpf.getResourceType(), perm));
121         }
122         seq.putAttribute(AccessRightsPermissionsForm.ATTR_RESOURCE_TYPE_PERMISSIONS, l);
123         return unspecified(mapping, form, request, response);
124     }
125
126     /**
127      * @param mapping
128      * @param form
129      * @param request
130      * @param response
131      * @return ActionForward
132      * @throws Exception
133      */

134     public ActionForward removeSelected(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
135                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
136         applyToSequence(mapping, (AbstractWizardForm) form, request, response);
137         AccessRightsPermissionsForm drpf = (AccessRightsPermissionsForm) form;
138         AbstractWizardSequence seq = getWizardSequence(request);
139         String JavaDoc[] sel = drpf.getSelectedResourceTypePermissions();
140         if (sel != null) {
141             List JavaDoc l = (List JavaDoc) seq.getAttribute(AccessRightsPermissionsForm.ATTR_RESOURCE_TYPE_PERMISSIONS, new ArrayList JavaDoc());
142             for (int i = 0; i < sel.length; i++) {
143                 int idx = sel[i].indexOf('-');
144                 int resourceTypeId = Integer.parseInt(sel[i].substring(0, idx));
145                 int permissionId = Integer.parseInt(sel[i].substring(idx + 1));
146                 AccessRight sp = null;
147                 for (Iterator JavaDoc j = l.iterator(); j.hasNext() && sp == null;) {
148                     AccessRight p = (AccessRight) j.next();
149                     if (p.getResourceType().getResourceTypeId() == resourceTypeId && p.getPermission().getId() == permissionId) {
150                         sp = p;
151                     }
152                 }
153                 if (sp != null) {
154                     l.remove(sp);
155                 }
156             }
157         }
158         return unspecified(mapping, form, request, response);
159     }
160 }
161
Popular Tags