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: ApplyUserMemoryRealmAction.java 9680 2006-10-06 12:08:33Z danesa $
024: * --------------------------------------------------------------------------
025: */
026:
027: package org.objectweb.jonas.webapp.jonasadmin.security;
028:
029: import java.io.IOException;
030: import java.util.ArrayList;
031:
032: import javax.management.ObjectName;
033: import javax.servlet.ServletException;
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036:
037: import org.apache.struts.action.ActionForm;
038: import org.apache.struts.action.ActionForward;
039: import org.apache.struts.action.ActionMapping;
040: import org.objectweb.jonas.jmx.JonasManagementRepr;
041: import org.objectweb.jonas.jmx.JonasObjectName;
042: import org.objectweb.jonas.webapp.jonasadmin.Jlists;
043: import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
044:
045: /**
046: *
047: */
048:
049: public class ApplyUserMemoryRealmAction extends BaseMemoryRealmAction {
050:
051: // --------------------------------------------------------- Public Methods
052:
053: /**
054: */
055: public ActionForward executeAction(ActionMapping p_Mapping,
056: ActionForm p_Form, HttpServletRequest p_Request,
057: HttpServletResponse p_Response) throws IOException,
058: ServletException {
059:
060: WhereAreYou oWhere = (WhereAreYou) p_Request.getSession()
061: .getAttribute(WhereAreYou.SESSION_NAME);
062: String serverName = oWhere.getCurrentJonasServerName();
063:
064: // Realm Form used
065: MemoryRealmForm oRealmForm = getForm(p_Mapping, p_Request);
066:
067: // Form used
068: UserMemoryRealmForm oForm = (UserMemoryRealmForm) p_Form;
069:
070: oForm.setListRolesUsed(Jlists.getArrayList(
071: oForm.getRolesUsed(), Jlists.SEPARATOR));
072: oForm.setListRolesNotused(Jlists.getArrayList(oForm
073: .getRolesNotused(), Jlists.SEPARATOR));
074: oForm.setListGroupsUsed(Jlists.getArrayList(oForm
075: .getGroupsUsed(), Jlists.SEPARATOR));
076: oForm.setListGroupsNotused(Jlists.getArrayList(oForm
077: .getGroupsNotused(), Jlists.SEPARATOR));
078:
079: ArrayList alAddRoles = new ArrayList(oForm.getListRolesUsed());
080: alAddRoles.removeAll(oForm.getListRolesUser());
081: ArrayList alRemoveRoles = new ArrayList(oForm
082: .getListRolesNotused());
083: alRemoveRoles.retainAll(oForm.getListRolesUser());
084: ArrayList alAddGroups = new ArrayList(oForm.getListGroupsUsed());
085: alAddGroups.removeAll(oForm.getListGroupsUser());
086: ArrayList alRemoveGroups = new ArrayList(oForm
087: .getListGroupsNotused());
088: alRemoveGroups.retainAll(oForm.getListGroupsUser());
089:
090: // Populate MBean
091: try {
092: ObjectName onUser = null;
093: // Create a new user
094: if (oForm.getAction().equals("create") == true) {
095: String sEncrypted = encryptPassword(
096: oForm.getPassword(), "MD5", serverName);
097: ObjectName onRealm = JonasObjectName
098: .securityMemoryFactory(oRealmForm.getResource());
099: String[] asParam = { oForm.getUser(), sEncrypted };
100: String[] asSignature = { "java.lang.String",
101: "java.lang.String" };
102: JonasManagementRepr.invoke(onRealm, "addUser", asParam,
103: asSignature, serverName);
104: // Search created user
105: onUser = JonasObjectName.user(oRealmForm.getResource(),
106: oForm.getUser());
107: } else {
108: // Modify existing user
109: onUser = JonasObjectName.user(oRealmForm.getResource(),
110: oForm.getUser());
111: // Password
112: if (oForm.getPassword().length() > 0) {
113: // Encrypt and set password
114: setStringAttribute(onUser, "Password",
115: encryptPassword(oForm.getPassword(), "MD5",
116: serverName));
117: }
118: }
119: // Roles
120: if (alAddRoles.size() > 0) {
121: for (int i = 0; i < alAddRoles.size(); i++) {
122: String[] asParam = { alAddRoles.get(i).toString() };
123: String[] asSignature = { "java.lang.String" };
124: JonasManagementRepr.invoke(onUser, "addRole",
125: asParam, asSignature, serverName);
126: }
127: }
128: if (alRemoveRoles.size() > 0) {
129: for (int i = 0; i < alRemoveRoles.size(); i++) {
130: String[] asParam = { alRemoveRoles.get(i)
131: .toString() };
132: String[] asSignature = { "java.lang.String" };
133: JonasManagementRepr.invoke(onUser, "removeRole",
134: asParam, asSignature, serverName);
135: }
136: }
137: // Groups
138: if (alAddGroups.size() > 0) {
139: for (int i = 0; i < alAddGroups.size(); i++) {
140: String[] asParam = { alAddGroups.get(i).toString() };
141: String[] asSignature = { "java.lang.String" };
142: JonasManagementRepr.invoke(onUser, "addGroup",
143: asParam, asSignature, serverName);
144: }
145: }
146: if (alRemoveGroups.size() > 0) {
147: for (int i = 0; i < alRemoveGroups.size(); i++) {
148: String[] asParam = { alRemoveGroups.get(i)
149: .toString() };
150: String[] asSignature = { "java.lang.String" };
151: JonasManagementRepr.invoke(onUser, "removeGroup",
152: asParam, asSignature, serverName);
153: }
154: }
155:
156: } catch (Throwable t) {
157: addGlobalError(t);
158: saveErrors(p_Request, m_Errors);
159: return (p_Mapping.findForward("Global Error"));
160: }
161: // Forward to the jsp.
162: return (p_Mapping.findForward("ActionEditMemoryRealmUsers"));
163: }
164:
165: // --------------------------------------------------------- Protected Methods
166:
167: }
|