KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > jonasadmin > security > ApplyUserMemoryRealmAction


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Michel-Ange ANTON
22  * --------------------------------------------------------------------------
23  * $Id: ApplyUserMemoryRealmAction.java,v 1.3 2004/03/19 14:31:48 sauthieg Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.webapp.jonasadmin.security;
28
29 import java.io.IOException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31
32 import javax.management.ObjectName JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 import org.apache.struts.action.ActionForm;
38 import org.apache.struts.action.ActionForward;
39 import org.apache.struts.action.ActionMapping;
40 import org.objectweb.jonas.jmx.JonasManagementRepr;
41 import org.objectweb.jonas.jmx.JonasObjectName;
42 import org.objectweb.jonas.webapp.jonasadmin.Jlists;
43
44 /**
45  *
46  */

47
48 public class ApplyUserMemoryRealmAction extends BaseMemoryRealmAction {
49
50 // --------------------------------------------------------- Public Methods
51

52     /**
53      */

54     public ActionForward executeAction(ActionMapping p_Mapping, ActionForm p_Form
55         , HttpServletRequest JavaDoc p_Request, HttpServletResponse JavaDoc p_Response)
56         throws IOException JavaDoc, ServletException JavaDoc {
57
58         // Realm Form used
59
MemoryRealmForm oRealmForm = getForm(p_Mapping, p_Request);
60
61         // Form used
62
UserMemoryRealmForm oForm = (UserMemoryRealmForm) p_Form;
63
64         oForm.setListRolesUsed(Jlists.getArrayList(oForm.getRolesUsed(), Jlists.SEPARATOR));
65         oForm.setListRolesNotused(Jlists.getArrayList(oForm.getRolesNotused(), Jlists.SEPARATOR));
66         oForm.setListGroupsUsed(Jlists.getArrayList(oForm.getGroupsUsed(), Jlists.SEPARATOR));
67         oForm.setListGroupsNotused(Jlists.getArrayList(oForm.getGroupsNotused(), Jlists.SEPARATOR));
68
69         ArrayList JavaDoc alAddRoles = new ArrayList JavaDoc(oForm.getListRolesUsed());
70         alAddRoles.removeAll(oForm.getListRolesUser());
71         ArrayList JavaDoc alRemoveRoles = new ArrayList JavaDoc(oForm.getListRolesNotused());
72         alRemoveRoles.retainAll(oForm.getListRolesUser());
73         ArrayList JavaDoc alAddGroups = new ArrayList JavaDoc(oForm.getListGroupsUsed());
74         alAddGroups.removeAll(oForm.getListGroupsUser());
75         ArrayList JavaDoc alRemoveGroups = new ArrayList JavaDoc(oForm.getListGroupsNotused());
76         alRemoveGroups.retainAll(oForm.getListGroupsUser());
77
78         // Populate MBean
79
try {
80             ObjectName JavaDoc onUser = null;
81             // Create a new user
82
if (oForm.getAction().equals("create") == true) {
83                 String JavaDoc sEncrypted = encryptPassword(oForm.getPassword(), "MD5");
84                 ObjectName JavaDoc onRealm = JonasObjectName.securityMemoryFactory(oRealmForm.getResource());
85                 String JavaDoc[] asParam = {
86                     oForm.getUser(), sEncrypted};
87                 String JavaDoc[] asSignature = {
88                     "java.lang.String", "java.lang.String"};
89                 JonasManagementRepr.invoke(onRealm, "addUser", asParam, asSignature);
90                 // Search created user
91
onUser = JonasObjectName.user(oRealmForm.getResource(), oForm.getUser());
92             }
93             else {
94                 // Modify existing user
95
onUser = JonasObjectName.user(oRealmForm.getResource(), oForm.getUser());
96                 // Password
97
if (oForm.getPassword().length() > 0) {
98                     // Encrypt and set password
99
setStringAttribute(onUser, "Password", encryptPassword(oForm.getPassword()
100                         , "MD5"));
101                 }
102             }
103             // Roles
104
if (alAddRoles.size() > 0) {
105                 for (int i = 0; i < alAddRoles.size(); i++) {
106                     String JavaDoc[] asParam = {
107                         alAddRoles.get(i).toString()};
108                     String JavaDoc[] asSignature = {
109                         "java.lang.String"};
110                     JonasManagementRepr.invoke(onUser, "addRole", asParam, asSignature);
111                 }
112             }
113             if (alRemoveRoles.size() > 0) {
114                 for (int i = 0; i < alRemoveRoles.size(); i++) {
115                     String JavaDoc[] asParam = {
116                         alRemoveRoles.get(i).toString()};
117                     String JavaDoc[] asSignature = {
118                         "java.lang.String"};
119                     JonasManagementRepr.invoke(onUser, "removeRole", asParam, asSignature);
120                 }
121             }
122             // Groups
123
if (alAddGroups.size() > 0) {
124                 for (int i = 0; i < alAddGroups.size(); i++) {
125                     String JavaDoc[] asParam = {
126                         alAddGroups.get(i).toString()};
127                     String JavaDoc[] asSignature = {
128                         "java.lang.String"};
129                     JonasManagementRepr.invoke(onUser, "addGroup", asParam, asSignature);
130                 }
131             }
132             if (alRemoveGroups.size() > 0) {
133                 for (int i = 0; i < alRemoveGroups.size(); i++) {
134                     String JavaDoc[] asParam = {
135                         alRemoveGroups.get(i).toString()};
136                     String JavaDoc[] asSignature = {
137                         "java.lang.String"};
138                     JonasManagementRepr.invoke(onUser, "removeGroup", asParam, asSignature);
139                 }
140             }
141
142         }
143         catch (Throwable JavaDoc t) {
144             addGlobalError(t);
145             saveErrors(p_Request, m_Errors);
146             return (p_Mapping.findForward("Global Error"));
147         }
148         // Forward to the jsp.
149
return (p_Mapping.findForward("ActionEditMemoryRealmUsers"));
150     }
151
152 // --------------------------------------------------------- Protected Methods
153

154 }
155
Popular Tags