001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.console.securitymanager;
017:
018: import java.io.IOException;
019: import java.util.Collection;
020: import java.util.Hashtable;
021:
022: import javax.portlet.ActionRequest;
023: import javax.portlet.ActionResponse;
024: import javax.portlet.PortletConfig;
025: import javax.portlet.PortletContext;
026: import javax.portlet.PortletException;
027: import javax.portlet.RenderRequest;
028: import javax.portlet.RenderResponse;
029: import javax.portlet.WindowState;
030:
031: import org.apache.geronimo.console.util.SERealmGroupHelper;
032: import org.apache.geronimo.console.util.SERealmUserHelper;
033:
034: /**
035: * @version $Rev: 540258 $ $Date: 2007-05-21 13:01:37 -0700 (Mon, 21 May 2007) $
036: */
037: public class SEUsersPortlet extends AbstractSecurityManagerPortlet {
038:
039: protected void doView(RenderRequest renderRequest,
040: RenderResponse renderResponse) throws PortletException,
041: IOException {
042: if (WindowState.MINIMIZED
043: .equals(renderRequest.getWindowState())) {
044: return;
045: }
046:
047: String errorMessage = renderRequest
048: .getParameter("errorMessage");
049:
050: if (errorMessage != null) {
051: renderRequest.setAttribute("errorMessage", errorMessage);
052: errorView.include(renderRequest, renderResponse);
053: } else {
054: try {
055: String[] users = SERealmUserHelper.getUsers();
056:
057: Hashtable userInfo = new Hashtable();
058: for (int i = 0; i < users.length; i++) {
059: String currentUser = users[i];
060: // We really shouldn't be attaching everyone's password as portlet data!!!
061: // And the current Portlet doesn't use it, so why send it....
062: // userInfo.put(currentUser, SERealmUserHelper.getPassword(currentUser.toString()));
063: userInfo.put(currentUser, currentUser.toString());
064: }
065:
066: String currAction = renderRequest
067: .getParameter("currAction");
068: renderRequest.setAttribute("message", renderRequest
069: .getParameter("message"));
070:
071: if ("new".equals(currAction)) {
072: String[] groups = SERealmGroupHelper.getGroups();
073: Hashtable groupsInfo = new Hashtable();
074: for (int i = 0; i < groups.length; i++) {
075: String currentGroup = groups[i];
076: groupsInfo.put(currentGroup, SERealmGroupHelper
077: .getUsers(currentGroup));
078: }
079: renderRequest
080: .setAttribute("groupsInfo", groupsInfo);
081: addMaximizedView.include(renderRequest,
082: renderResponse);
083: } else if ("edit".equals(currAction)) {
084: String user = renderRequest.getParameter("user");
085: renderRequest.setAttribute("userID", user);
086: // We really shouldn't be sending the user's password as portlet data!!!
087: // And the current Portlet doesn't use it, so why send it....
088: //renderRequest.setAttribute("password", SERealmUserHelper.getPassword(user));
089: // Current Portlet doesn't use groupsInfo, so why send it
090: //renderRequest.setAttribute("groupsInfo", groupsInfo);
091: addMaximizedView.include(renderRequest,
092: renderResponse);
093: } else {
094: if (WindowState.NORMAL.equals(renderRequest
095: .getWindowState())) {
096: renderRequest
097: .setAttribute("userInfo", userInfo);
098: normalView.include(renderRequest,
099: renderResponse);
100: } else {
101: renderRequest
102: .setAttribute("userInfo", userInfo);
103: maximizedView.include(renderRequest,
104: renderResponse);
105: }
106: }
107: } catch (Exception e) {
108: errorMessage = e.getMessage();
109: renderRequest
110: .setAttribute("errorMessage", errorMessage);
111: errorView.include(renderRequest, renderResponse);
112: }
113: }
114: }
115:
116: public void init(PortletConfig portletConfig)
117: throws PortletException {
118: super .init(portletConfig);
119: PortletContext pc = portletConfig.getPortletContext();
120: normalView = pc
121: .getRequestDispatcher("/WEB-INF/view/securityrealmmanager/se/users/normal.jsp");
122: addNormalView = pc
123: .getRequestDispatcher("/WEB-INF/view/securityrealmmanager/se/users/addnormal.jsp");
124: maximizedView = pc
125: .getRequestDispatcher("/WEB-INF/view/securityrealmmanager/se/users/maximized.jsp");
126: addMaximizedView = pc
127: .getRequestDispatcher("/WEB-INF/view/securityrealmmanager/se/users/addmaximized.jsp");
128: helpView = pc
129: .getRequestDispatcher("/WEB-INF/view/securityrealmmanager/se/users/help.jsp");
130: errorView = pc
131: .getRequestDispatcher("/WEB-INF/view/securityrealmmanager/derby/groups/error.jsp");
132: }
133:
134: public void processAction(ActionRequest actionRequest,
135: ActionResponse actionResponse) throws PortletException,
136: IOException {
137: String action = actionRequest.getParameter("action").trim();
138: String cancel = actionRequest.getParameter("cancel");
139: String currAction = "";
140: if (cancel != null) {
141: action = "";
142: }
143: String user = actionRequest.getParameter("userId");
144: String password = actionRequest.getParameter("password");
145: String group = actionRequest.getParameter("group");
146:
147: try {
148: if ("delete".equals(action)) {
149: try {
150: String[] groups = SERealmGroupHelper.getGroups();
151: for (int i = 0; i < groups.length; i++) {
152: String currentGroup = groups[i];
153: if (SERealmGroupHelper.isGroupMember(
154: currentGroup, user)) {
155: Collection list = SERealmGroupHelper
156: .getUsers(currentGroup);
157: list.remove(user);
158: String[] groupUsers = (String[]) list
159: .toArray(new String[0]);
160: SERealmGroupHelper.updateGroup(
161: currentGroup, groupUsers);
162: }
163: }
164: SERealmUserHelper.deleteUser(user);
165: } catch (Exception e) {
166: actionResponse.setRenderParameter("message",
167: "ERROR: Error in SEUsersPortlet while deleting user "
168: + user + ". Cause: "
169: + e.getMessage());
170: }
171: } else if ("update".equals(action)) {
172: if (password != null && !password.equals("")) {
173: // Update the password only when it is not blank.
174: SERealmUserHelper.updateUser(user, password);
175: }
176: } else if ("add".equals(action)) {
177: try {
178: SERealmUserHelper.addUser(user, password);
179: if ((group != null) && (!group.equals(""))) {
180: Collection list = SERealmGroupHelper
181: .getUsers(group);
182: list.add(user);
183: String[] groupUsers = (String[]) list
184: .toArray(new String[0]);
185: SERealmGroupHelper.updateGroup(group,
186: groupUsers);
187: }
188: } catch (Exception e) {
189: actionResponse.setRenderParameter("message",
190: "ERROR: Error in SEUsersPortlet while adding user "
191: + user + " to group " + group
192: + ". Cause: " + e.getMessage());
193: }
194: } else if ("new".equals(action)) {
195: currAction = "new";
196: } else if ("edit".equals(action)) {
197: currAction = "edit";
198: }
199: actionResponse.setRenderParameter("currAction", currAction);
200: if (user != null) {
201: actionResponse.setRenderParameter("user", user);
202: }
203: } catch (Exception e) {
204: actionResponse.setRenderParameter("message",
205: "Error encountered in SEUsersPortlet. Cause: "
206: + e.getMessage());
207: }
208: }
209:
210: }
|