KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > forms > AuthenticationSchemeForm


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.forms;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26
27 import org.apache.struts.Globals;
28 import org.apache.struts.action.ActionErrors;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.action.ActionMessage;
31
32 import com.sslexplorer.boot.PropertyList;
33 import com.sslexplorer.input.MultiSelectSelectionModel;
34 import com.sslexplorer.policyframework.Policy;
35 import com.sslexplorer.policyframework.PolicyConstants;
36 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
37 import com.sslexplorer.policyframework.Resource;
38 import com.sslexplorer.policyframework.ResourceUtil;
39 import com.sslexplorer.policyframework.forms.AbstractResourceForm;
40 import com.sslexplorer.security.AuthenticationModuleDefinition;
41 import com.sslexplorer.security.AuthenticationModuleManager;
42 import com.sslexplorer.security.AuthenticationScheme;
43 import com.sslexplorer.security.DefaultAuthenticationScheme;
44 import com.sslexplorer.security.LogonControllerFactory;
45 import com.sslexplorer.security.SessionInfo;
46 import com.sslexplorer.security.User;
47
48 /**
49  * Implementation of a
50  * {@link com.sslexplorer.policyframework.forms.AbstractResourceForm} that
51  * allows an administrator to edit an <i>Authentication Scheme</i>.
52  *
53  * @author Brett Smith <a HREF="mailto:brett@3sp.com">&lt;brett@3sp.com&gt;</a>
54  * @see com.sslexplorer.security.AuthenticationScheme
55  */

56 public class AuthenticationSchemeForm extends AbstractResourceForm {
57     protected String JavaDoc selectedTab = "details";
58     protected MultiSelectSelectionModel moduleModel;
59     protected PropertyList selectedModules;
60     protected boolean isSystem = false;
61
62     /*
63      * (non-Javadoc)
64      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping,
65      * javax.servlet.http.HttpServletRequest)
66      */

67     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
68         ActionErrors errs = super.validate(mapping, request);
69         if (isCommiting()) {
70             PropertyList l = getSelectedModulesList();
71             if (l.size() < 1) {
72                 errs.add(Globals.ERROR_KEY, new ActionMessage("editAuthenticationScheme.error.noModulesSelected"));
73             } else {
74                 AuthenticationModuleDefinition def = AuthenticationModuleManager.getInstance().getModuleDefinition(l.get(0).toString());
75                 if (!def.getPrimary() && (!def.getPrimaryIfSecondardExists() && l.size() > 1)) {
76                     errs.add(Globals.ERROR_KEY, new ActionMessage("editAuthenticationScheme.error.firstModuleNotPrimary"));
77                 }
78             }
79
80             try {
81                 List JavaDoc granted = ResourceUtil.getGrantedResource(LogonControllerFactory.getInstance()
82                                 .getSessionInfo(request), PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE);
83                 // does the user have any other signonable auth schemes?
84
boolean found = false;
85                 SessionInfo info = LogonControllerFactory.getInstance().getSessionInfo(request);
86                 for (Iterator JavaDoc iter = granted.iterator(); iter.hasNext();) {
87                     AuthenticationScheme element = (DefaultAuthenticationScheme) iter.next();
88                     if (element.getEnabled() && !element.isSystemScheme() && element.getResourceId() != this.getResourceId()) {
89
90                         for (Iterator JavaDoc iterator = PolicyDatabaseFactory.getInstance()
91                                         .getPoliciesAttachedToResource(element, info.getUser().getRealm()).iterator(); iterator.hasNext();) {
92                             Policy policy = (Policy) iterator.next();
93                             if (PolicyDatabaseFactory.getInstance().isPolicyGrantedToUser(policy,
94                                             LogonControllerFactory.getInstance().getSessionInfo(request).getUser())) {
95                                 found = true;
96                             }
97                         }
98                     }
99                 }
100                 // if no other schemes autherised, then ensure that this one is.
101
if (!found) {
102                     for (Iterator JavaDoc iter2 = this.getSelectedPoliciesList().iterator(); iter2.hasNext();) {
103                         String JavaDoc id = (String JavaDoc) iter2.next();
104                         if (PolicyDatabaseFactory.getInstance().isPolicyGrantedToUser(
105                                         PolicyDatabaseFactory.getInstance().getPolicy(Integer.parseInt(id)),
106                                         LogonControllerFactory.getInstance().getSessionInfo(request).getUser())) {
107                             found = true;
108                         }
109                     }
110                 }
111
112                 if (!found) {
113                     errs.add(Globals.ERROR_KEY, new ActionMessage("authenticationSchemes.error.mustHavePolicySuperUserAssociation"));
114                 }
115             } catch (Exception JavaDoc e) {
116                 errs.add(Globals.ERROR_KEY, new ActionMessage("authenticationSchemes.error.failedToValidateSuperUserAuthSchemeConnection"));
117             }
118
119         }
120         return errs;
121     }
122
123     /*
124      * (non-Javadoc)
125      * @see com.sslexplorer.policyframework.forms.AbstractResourceForm#applyToResource()
126      */

127     public void applyToResource() throws Exception JavaDoc {
128         AuthenticationScheme seq = (DefaultAuthenticationScheme) getResource();
129         seq.clearModules();
130         for (Iterator JavaDoc i = getSelectedModulesList().iterator(); i.hasNext();) {
131             seq.addModule((String JavaDoc) i.next());
132         }
133     }
134
135     /*
136      * (non-Javadoc)
137      * @see com.sslexplorer.tabs.TabModel#getTabCount()
138      */

139     public int getTabCount() {
140         return 3;
141     }
142
143     /*
144      * (non-Javadoc)
145      * @see com.sslexplorer.tabs.TabModel#getTabName1(int)
146      */

147     public String JavaDoc getTabName(int idx) {
148         switch (idx) {
149             case 0:
150                 return "details";
151             case 1:
152                 return "modules";
153             default:
154                 return "policies";
155         }
156     }
157
158     /*
159      * (non-Javadoc)
160      * @see com.sslexplorer.tabs.TabModel#getSelectedTab()
161      */

162     public String JavaDoc getSelectedTab() {
163         return selectedTab;
164     }
165
166     /*
167      * (non-Javadoc)
168      * @see com.sslexplorer.tabs.TabModel#setSelectedTab(java.lang.String)
169      */

170     public void setSelectedTab(String JavaDoc selectedTab) {
171         this.selectedTab = selectedTab;
172     }
173
174     /*
175      * (non-Javadoc)
176      * @see com.sslexplorer.tabs.TabModel#getTabTitle(int)
177      */

178     public String JavaDoc getTabTitle(int i) {
179         return null;
180     }
181
182     /* (non-Javadoc)
183      * @see com.sslexplorer.policyframework.forms.AbstractResourceForm#getResourceByName(java.lang.String, com.sslexplorer.security.SessionInfo)
184      */

185     public Resource getResourceByName(String JavaDoc resourceName, SessionInfo session) throws Exception JavaDoc {
186         return PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE.getResourceByName(resourceName, session);
187     }
188
189     /**
190      * Get the module model
191      * @return the module model.
192      */

193     public MultiSelectSelectionModel getModuleModel() {
194         return moduleModel;
195     }
196
197     /**
198      * Set the module model
199      * @param moduleModel model to set.
200      */

201     public void setModuleModel(MultiSelectSelectionModel moduleModel) {
202         this.moduleModel = moduleModel;
203     }
204
205     /**
206      * Get the selected modules as a list
207      * @return selected modules list
208      */

209     public PropertyList getSelectedModulesList() {
210         return selectedModules;
211     }
212
213     /**
214      * Get the selected modules as a string suitable for the multi select components
215      * @return selected modules as string
216      */

217     public String JavaDoc getSelectedModules() {
218         return selectedModules.getAsTextFieldText();
219     }
220
221     /**
222      * Set the selected modules as a string from the multi select components
223      * @param selectedModules selected modules as string
224      */

225     public void setSelectedModules(String JavaDoc selectedModules) {
226         this.selectedModules.setAsTextFieldText(selectedModules);
227     }
228
229     /**
230      * Set the selected modules list
231      * @param selectedModules selected modules list
232      */

233     public void setSelectedModulesList(PropertyList selectedModules) {
234         this.selectedModules = selectedModules;
235     }
236
237     /*
238      * (non-Javadoc)
239      * @see com.sslexplorer.tabs.TabModel#getTabBundle(int)
240      */

241     public String JavaDoc getTabBundle(int idx) {
242         return null;
243     }
244
245     public void initialise(User user, Resource resource, boolean editing, MultiSelectSelectionModel policyModel,
246                     PropertyList selectedPolicies, User owner, boolean assignOnly) throws Exception JavaDoc {
247         super.initialise(user, resource, editing, policyModel, selectedPolicies, owner, assignOnly);
248         this.isSystem = (((DefaultAuthenticationScheme) resource).isSystemScheme());
249     }
250
251     /**
252      * @return boolean
253      */

254     public boolean isSystem() {
255         return isSystem;
256     }
257 }
Popular Tags