001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): Michel-Ange ANTON
022: * --------------------------------------------------------------------------
023: * $Id: BaseMemoryRealmAction.java 9680 2006-10-06 12:08:33Z danesa $
024: * --------------------------------------------------------------------------
025: */
026:
027: package org.objectweb.jonas.webapp.jonasadmin.security;
028:
029: import java.util.ArrayList;
030: import java.util.Arrays;
031: import java.util.Collections;
032:
033: import javax.management.ObjectName;
034: import javax.servlet.http.HttpServletRequest;
035:
036: import org.apache.struts.action.ActionMapping;
037: import org.objectweb.jonas.jmx.JonasManagementRepr;
038: import org.objectweb.jonas.jmx.JonasObjectName;
039: import org.objectweb.jonas.webapp.jonasadmin.Jlists;
040:
041: /**
042: *
043: */
044:
045: abstract public class BaseMemoryRealmAction extends BaseSecurityAction {
046:
047: // --------------------------------------------------------- Public Methods
048:
049: // --------------------------------------------------------- Protected Methods
050: protected MemoryRealmForm getForm(ActionMapping p_Mapping,
051: HttpServletRequest p_Request) {
052: // Form used
053: MemoryRealmForm oForm = null;
054: // Memory realm to edit
055: String sResource = p_Request.getParameter("resource");
056:
057: // Build a new form
058: if (sResource != null) {
059: oForm = new MemoryRealmForm();
060: m_Session.setAttribute("memoryRealmForm", oForm);
061: oForm.reset(p_Mapping, p_Request);
062: oForm.setResource(sResource);
063: // free old items of session
064: m_Session.removeAttribute("userMemoryRealmForm");
065: m_Session.removeAttribute("roleMemoryRealmForm");
066: m_Session.removeAttribute("groupMemoryRealmForm");
067: m_Session.removeAttribute("itemsMemoryRealmForm");
068: } else {
069: oForm = (MemoryRealmForm) m_Session
070: .getAttribute("memoryRealmForm");
071: }
072: return oForm;
073: }
074:
075: /**
076: * Remove of session the <code>ItemsMemoryRealmForm</code> instance
077: * if the given type is different of the current type.
078: *
079: * @param p_Type Current type (user, role, group)
080: */
081: protected void removeItemsMemoryRealmForm(String p_Type) {
082: ItemsMemoryRealmForm oForm = (ItemsMemoryRealmForm) m_Session
083: .getAttribute("itemsMemoryRealmForm");
084: if (oForm != null) {
085: if ((oForm.getType() != null)
086: && (oForm.getType().equals(p_Type) == false)) {
087: m_Session.removeAttribute("itemsMemoryRealmForm");
088: }
089: }
090: }
091:
092: /**
093: * Populate the <code>UserMemoryRealmForm</code> with MBeans.
094: * If the user name is null, the User MBean is not called to populate the form.
095: *
096: * @param p_RealmForm Used for the resource name
097: * @param p_UserForm Form to populate
098: * @param p_UserName The user (Can be null)
099: * @throws Exception
100: */
101: protected void populateUserForm(MemoryRealmForm p_RealmForm,
102: UserMemoryRealmForm p_UserForm, String p_UserName,
103: String jonasServerName) throws Exception {
104:
105: if (p_UserName != null) {
106: // Populate with Mbean 'user'
107: ObjectName oObjectName = JonasObjectName.user(p_RealmForm
108: .getResource(), p_UserName);
109: p_UserForm.setUser(getStringAttribute(oObjectName, "Name"));
110:
111: p_UserForm.setListGroupsUser(new ArrayList(Arrays
112: .asList((String[]) JonasManagementRepr
113: .getAttribute(oObjectName, "ArrayGroups",
114: jonasServerName))));
115: p_UserForm.setListGroupsUsed(new ArrayList(p_UserForm
116: .getListGroupsUser()));
117:
118: p_UserForm.setListRolesUser(new ArrayList(Arrays
119: .asList((String[]) JonasManagementRepr
120: .getAttribute(oObjectName, "ArrayRoles",
121: jonasServerName))));
122: p_UserForm.setListRolesUsed(new ArrayList(p_UserForm
123: .getListRolesUser()));
124: }
125: // Populate with Mbean 'realm'
126: ObjectName oObjectName = JonasObjectName
127: .securityMemoryFactory(p_RealmForm.getResource());
128: p_UserForm.setListGroupsRealm(new ArrayList(Arrays
129: .asList((String[]) JonasManagementRepr.invoke(
130: oObjectName, "listGroups", null, null,
131: jonasServerName))));
132: p_UserForm.setListRolesRealm(new ArrayList(Arrays
133: .asList((String[]) JonasManagementRepr.invoke(
134: oObjectName, "listRoles", null, null,
135: jonasServerName))));
136:
137: // Calculate Unused
138: ArrayList alUnused = new ArrayList(p_UserForm
139: .getListGroupsRealm());
140: alUnused.removeAll(p_UserForm.getListGroupsUser());
141: Collections.sort(alUnused);
142: p_UserForm.setListGroupsNotused(alUnused);
143:
144: alUnused = new ArrayList(p_UserForm.getListRolesRealm());
145: alUnused.removeAll(p_UserForm.getListRolesUser());
146: Collections.sort(alUnused);
147: p_UserForm.setListRolesNotused(alUnused);
148:
149: // Format list to string
150: p_UserForm.setGroupsUsed(Jlists.getString(p_UserForm
151: .getListGroupsUsed(), Jlists.SEPARATOR));
152: p_UserForm.setGroupsNotused(Jlists.getString(p_UserForm
153: .getListGroupsNotused(), Jlists.SEPARATOR));
154: p_UserForm.setRolesUsed(Jlists.getString(p_UserForm
155: .getListRolesUsed(), Jlists.SEPARATOR));
156: p_UserForm.setRolesNotused(Jlists.getString(p_UserForm
157: .getListRolesNotused(), Jlists.SEPARATOR));
158: }
159:
160: /**
161: * Encrypt a password with MBean security service method.
162: *
163: * @param p_Password Password to encrypt
164: * @param p_EncrypMethod MD5 or SHA string
165: * @return The encrypted password
166: * @throws Exception
167: */
168: protected String encryptPassword(String p_Password,
169: String p_EncrypMethod, String jonasServerName)
170: throws Exception {
171: ObjectName onSecurityService = JonasObjectName
172: .securityService();
173: String[] asParam = { p_Password, p_EncrypMethod };
174: String[] asSignature = { "java.lang.String", "java.lang.String" };
175: return (String) JonasManagementRepr.invoke(onSecurityService,
176: "encryptPassword", asParam, asSignature,
177: jonasServerName);
178: }
179:
180: /**
181: * Populate the <code>RoleMemoryRealmForm</code> with MBeans.
182: * If the Role name is null, the Role MBean is not called to populate the form.
183: *
184: * @param p_RealmForm Used for the resource name
185: * @param p_RoleForm Form to populate
186: * @param p_RoleName The user (Can be null)
187: * @throws Exception
188: */
189: protected void populateRoleForm(MemoryRealmForm p_RealmForm,
190: RoleMemoryRealmForm p_RoleForm, String p_RoleName)
191: throws Exception {
192:
193: if (p_RoleName != null) {
194: // Populate with Mbean 'Role'
195: ObjectName oObjectName = JonasObjectName.role(p_RealmForm
196: .getResource(), p_RoleName);
197: p_RoleForm.setRole(getStringAttribute(oObjectName, "Name"));
198: p_RoleForm.setDescription(getStringAttribute(oObjectName,
199: "Description"));
200: }
201: }
202:
203: /**
204: * Populate the <code>GroupMemoryRealmForm</code> with MBeans.
205: * If the user name is null, the Group MBean is not called to populate the form.
206: *
207: * @param p_RealmForm Used for the resource name
208: * @param p_GroupForm Form to populate
209: * @param p_GroupName The user (Can be null)
210: * @throws Exception
211: */
212: protected void populateGroupForm(MemoryRealmForm p_RealmForm,
213: GroupMemoryRealmForm p_GroupForm, String p_GroupName,
214: String jonasServerName) throws Exception {
215:
216: if (p_GroupName != null) {
217: // Populate with Mbean 'group'
218: ObjectName oObjectName = JonasObjectName.group(p_RealmForm
219: .getResource(), p_GroupName);
220: p_GroupForm
221: .setGroup(getStringAttribute(oObjectName, "Name"));
222: p_GroupForm.setDescription(getStringAttribute(oObjectName,
223: "Description"));
224:
225: p_GroupForm.setListRolesGroup(new ArrayList(Arrays
226: .asList((String[]) JonasManagementRepr
227: .getAttribute(oObjectName, "ArrayRoles",
228: jonasServerName))));
229: p_GroupForm.setListRolesUsed(new ArrayList(p_GroupForm
230: .getListRolesGroup()));
231: }
232: // Populate with Mbean 'realm'
233: ObjectName oObjectName = JonasObjectName
234: .securityMemoryFactory(p_RealmForm.getResource());
235: p_GroupForm.setListRolesRealm(new ArrayList(Arrays
236: .asList((String[]) JonasManagementRepr.invoke(
237: oObjectName, "listRoles", null, null,
238: jonasServerName))));
239:
240: // Calculate Unused
241: ArrayList alUnused = new ArrayList(p_GroupForm
242: .getListRolesRealm());
243: alUnused.removeAll(p_GroupForm.getListRolesGroup());
244: Collections.sort(alUnused);
245: p_GroupForm.setListRolesNotused(alUnused);
246:
247: // Format list to string
248: p_GroupForm.setRolesUsed(Jlists.getString(p_GroupForm
249: .getListRolesUsed(), Jlists.SEPARATOR));
250: p_GroupForm.setRolesNotused(Jlists.getString(p_GroupForm
251: .getListRolesNotused(), Jlists.SEPARATOR));
252: }
253: }
|