ApplyUserMemoryRealmAction.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * Initial developer(s): Michel-Ange ANTON
00022  * --------------------------------------------------------------------------
00023  * $Id: ApplyUserMemoryRealmAction.java,v 1.3 2004/03/19 14:31:48 sauthieg Exp $
00024  * --------------------------------------------------------------------------
00025  */
00026 
00027 package org.objectweb.jonas.webapp.jonasadmin.security;
00028 
00029 import java.io.IOException;
00030 import java.util.ArrayList;
00031 
00032 import javax.management.ObjectName;
00033 import javax.servlet.ServletException;
00034 import javax.servlet.http.HttpServletRequest;
00035 import javax.servlet.http.HttpServletResponse;
00036 
00037 import org.apache.struts.action.ActionForm;
00038 import org.apache.struts.action.ActionForward;
00039 import org.apache.struts.action.ActionMapping;
00040 import org.objectweb.jonas.jmx.JonasManagementRepr;
00041 import org.objectweb.jonas.jmx.JonasObjectName;
00042 import org.objectweb.jonas.webapp.jonasadmin.Jlists;
00043 
00048 public class ApplyUserMemoryRealmAction extends BaseMemoryRealmAction {
00049 
00050 // --------------------------------------------------------- Public Methods
00051 
00054     public ActionForward executeAction(ActionMapping p_Mapping, ActionForm p_Form
00055         , HttpServletRequest p_Request, HttpServletResponse p_Response)
00056         throws IOException, ServletException {
00057 
00058         // Realm Form used
00059         MemoryRealmForm oRealmForm = getForm(p_Mapping, p_Request);
00060 
00061         // Form used
00062         UserMemoryRealmForm oForm = (UserMemoryRealmForm) p_Form;
00063 
00064         oForm.setListRolesUsed(Jlists.getArrayList(oForm.getRolesUsed(), Jlists.SEPARATOR));
00065         oForm.setListRolesNotused(Jlists.getArrayList(oForm.getRolesNotused(), Jlists.SEPARATOR));
00066         oForm.setListGroupsUsed(Jlists.getArrayList(oForm.getGroupsUsed(), Jlists.SEPARATOR));
00067         oForm.setListGroupsNotused(Jlists.getArrayList(oForm.getGroupsNotused(), Jlists.SEPARATOR));
00068 
00069         ArrayList alAddRoles = new ArrayList(oForm.getListRolesUsed());
00070         alAddRoles.removeAll(oForm.getListRolesUser());
00071         ArrayList alRemoveRoles = new ArrayList(oForm.getListRolesNotused());
00072         alRemoveRoles.retainAll(oForm.getListRolesUser());
00073         ArrayList alAddGroups = new ArrayList(oForm.getListGroupsUsed());
00074         alAddGroups.removeAll(oForm.getListGroupsUser());
00075         ArrayList alRemoveGroups = new ArrayList(oForm.getListGroupsNotused());
00076         alRemoveGroups.retainAll(oForm.getListGroupsUser());
00077 
00078         // Populate MBean
00079         try {
00080             ObjectName onUser = null;
00081             // Create a new user
00082             if (oForm.getAction().equals("create") == true) {
00083                 String sEncrypted = encryptPassword(oForm.getPassword(), "MD5");
00084                 ObjectName onRealm = JonasObjectName.securityMemoryFactory(oRealmForm.getResource());
00085                 String[] asParam = {
00086                     oForm.getUser(), sEncrypted};
00087                 String[] asSignature = {
00088                     "java.lang.String", "java.lang.String"};
00089                 JonasManagementRepr.invoke(onRealm, "addUser", asParam, asSignature);
00090                 // Search created user
00091                 onUser = JonasObjectName.user(oRealmForm.getResource(), oForm.getUser());
00092             }
00093             else {
00094                 // Modify existing user
00095                 onUser = JonasObjectName.user(oRealmForm.getResource(), oForm.getUser());
00096                 // Password
00097                 if (oForm.getPassword().length() > 0) {
00098                     // Encrypt and set password
00099                     setStringAttribute(onUser, "Password", encryptPassword(oForm.getPassword()
00100                         , "MD5"));
00101                 }
00102             }
00103             // Roles
00104             if (alAddRoles.size() > 0) {
00105                 for (int i = 0; i < alAddRoles.size(); i++) {
00106                     String[] asParam = {
00107                         alAddRoles.get(i).toString()};
00108                     String[] asSignature = {
00109                         "java.lang.String"};
00110                     JonasManagementRepr.invoke(onUser, "addRole", asParam, asSignature);
00111                 }
00112             }
00113             if (alRemoveRoles.size() > 0) {
00114                 for (int i = 0; i < alRemoveRoles.size(); i++) {
00115                     String[] asParam = {
00116                         alRemoveRoles.get(i).toString()};
00117                     String[] asSignature = {
00118                         "java.lang.String"};
00119                     JonasManagementRepr.invoke(onUser, "removeRole", asParam, asSignature);
00120                 }
00121             }
00122             // Groups
00123             if (alAddGroups.size() > 0) {
00124                 for (int i = 0; i < alAddGroups.size(); i++) {
00125                     String[] asParam = {
00126                         alAddGroups.get(i).toString()};
00127                     String[] asSignature = {
00128                         "java.lang.String"};
00129                     JonasManagementRepr.invoke(onUser, "addGroup", asParam, asSignature);
00130                 }
00131             }
00132             if (alRemoveGroups.size() > 0) {
00133                 for (int i = 0; i < alRemoveGroups.size(); i++) {
00134                     String[] asParam = {
00135                         alRemoveGroups.get(i).toString()};
00136                     String[] asSignature = {
00137                         "java.lang.String"};
00138                     JonasManagementRepr.invoke(onUser, "removeGroup", asParam, asSignature);
00139                 }
00140             }
00141 
00142         }
00143         catch (Throwable t) {
00144             addGlobalError(t);
00145             saveErrors(p_Request, m_Errors);
00146             return (p_Mapping.findForward("Global Error"));
00147         }
00148         // Forward to the jsp.
00149         return (p_Mapping.findForward("ActionEditMemoryRealmUsers"));
00150     }
00151 
00152 // --------------------------------------------------------- Protected Methods
00153 
00154 }

Generated on Tue Feb 15 15:05:30 2005 for JOnAS by  doxygen 1.3.9.1