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     */
017    
018    package org.apache.geronimo.console.securitymanager;
019    
020    import java.io.IOException;
021    import java.util.Collection;
022    import java.util.Hashtable;
023    
024    import javax.portlet.ActionRequest;
025    import javax.portlet.ActionResponse;
026    import javax.portlet.PortletConfig;
027    import javax.portlet.PortletContext;
028    import javax.portlet.PortletException;
029    import javax.portlet.RenderRequest;
030    import javax.portlet.RenderResponse;
031    import javax.portlet.WindowState;
032    
033    import org.apache.geronimo.console.util.SERealmGroupHelper;
034    import org.apache.geronimo.console.util.SERealmUserHelper;
035    
036    /**
037     * @version $Rev: 540258 $ $Date: 2007-05-21 16:01:37 -0400 (Mon, 21 May 2007) $
038     */
039    public class SEUsersPortlet extends AbstractSecurityManagerPortlet {
040    
041        protected void doView(RenderRequest renderRequest,
042                RenderResponse renderResponse) throws PortletException, IOException {
043            if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
044                return;
045            }
046    
047            String errorMessage = renderRequest.getParameter("errorMessage");
048    
049            if (errorMessage != null) {
050                renderRequest.setAttribute("errorMessage", errorMessage);
051                errorView.include(renderRequest, renderResponse);
052            } else {
053                try {
054                    String[] users = SERealmUserHelper.getUsers();
055    
056                    Hashtable userInfo = new Hashtable();
057                    for (int i = 0; i < users.length; i++) {
058                        String currentUser = users[i];
059                        // We really shouldn't be attaching everyone's password as portlet data!!!
060                        // And the current Portlet doesn't use it, so why send it....
061                        // userInfo.put(currentUser, SERealmUserHelper.getPassword(currentUser.toString()));
062                        userInfo.put(currentUser, currentUser.toString());
063                    }
064    
065                    String currAction = renderRequest.getParameter("currAction");
066                    renderRequest.setAttribute("message", renderRequest.getParameter("message"));
067    
068                    if ("new".equals(currAction)) {
069                        String[] groups = SERealmGroupHelper.getGroups();
070                        Hashtable groupsInfo = new Hashtable();
071                        for (int i = 0; i < groups.length; i++) {
072                            String currentGroup = groups[i];
073                            groupsInfo.put(currentGroup, SERealmGroupHelper.getUsers(currentGroup));
074                        }
075                        renderRequest.setAttribute("groupsInfo", groupsInfo);
076                        addMaximizedView.include(renderRequest, renderResponse);
077                    } else if ("edit".equals(currAction)) {
078                        String user = renderRequest.getParameter("user");
079                        renderRequest.setAttribute("userID", user);
080                        // We really shouldn't be sending the user's password as portlet data!!!
081                        // And the current Portlet doesn't use it, so why send it....
082                        //renderRequest.setAttribute("password", SERealmUserHelper.getPassword(user));
083                        // Current Portlet doesn't use groupsInfo, so why send it 
084                        //renderRequest.setAttribute("groupsInfo", groupsInfo);
085                        addMaximizedView.include(renderRequest, renderResponse);
086                    } else {
087                        if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
088                            renderRequest.setAttribute("userInfo", userInfo);
089                            normalView.include(renderRequest, renderResponse);
090                        } else {
091                            renderRequest.setAttribute("userInfo", userInfo);
092                            maximizedView.include(renderRequest, renderResponse);
093                        }
094                    }
095                } catch (Exception e) {
096                    errorMessage = e.getMessage();
097                    renderRequest.setAttribute("errorMessage", errorMessage);
098                    errorView.include(renderRequest, renderResponse);
099                }
100            }
101        }
102    
103        public void init(PortletConfig portletConfig) throws PortletException {
104            super.init(portletConfig);
105            PortletContext pc = portletConfig.getPortletContext();
106            normalView = pc
107                    .getRequestDispatcher("/WEB-INF/view/securityrealmmanager/se/users/normal.jsp");
108            addNormalView = pc
109                    .getRequestDispatcher("/WEB-INF/view/securityrealmmanager/se/users/addnormal.jsp");
110            maximizedView = pc
111                    .getRequestDispatcher("/WEB-INF/view/securityrealmmanager/se/users/maximized.jsp");
112            addMaximizedView = pc
113                    .getRequestDispatcher("/WEB-INF/view/securityrealmmanager/se/users/addmaximized.jsp");
114            helpView = pc
115                    .getRequestDispatcher("/WEB-INF/view/securityrealmmanager/se/users/help.jsp");
116            errorView = pc
117                    .getRequestDispatcher("/WEB-INF/view/securityrealmmanager/derby/groups/error.jsp");
118        }
119    
120        public void processAction(ActionRequest actionRequest,
121                ActionResponse actionResponse) throws PortletException, IOException {
122            String action = actionRequest.getParameter("action").trim();
123            String cancel = actionRequest.getParameter("cancel");
124            String currAction = "";
125            if (cancel != null) {
126                action = "";
127            }
128            String user = actionRequest.getParameter("userId");
129            String password = actionRequest.getParameter("password");
130            String group = actionRequest.getParameter("group");
131    
132            try {
133                if ("delete".equals(action)) {
134                    try {
135                        String[] groups = SERealmGroupHelper.getGroups();
136                        for (int i = 0; i < groups.length; i++) {
137                            String currentGroup = groups[i];
138                            if (SERealmGroupHelper.isGroupMember(currentGroup, user)) {
139                                Collection list = SERealmGroupHelper.getUsers(currentGroup);
140                                list.remove(user);
141                                String[] groupUsers = (String[]) list.toArray(new String[0]);
142                                SERealmGroupHelper.updateGroup(currentGroup, groupUsers);
143                            }
144                        }
145                        SERealmUserHelper.deleteUser(user);
146                    } catch (Exception e) {
147                        actionResponse.setRenderParameter("message",
148                                "ERROR: Error in SEUsersPortlet while deleting user "+user+". Cause: "+e.getMessage());
149                    }
150                } else if ("update".equals(action)) {
151                    if(password != null && !password.equals("")) {
152                        // Update the password only when it is not blank.
153                        SERealmUserHelper.updateUser(user, password);
154                    }
155                } else if ("add".equals(action)) {
156                    try {
157                        SERealmUserHelper.addUser(user, password);
158                        if ((group != null) && (!group.equals(""))) {
159                            Collection list = SERealmGroupHelper.getUsers(group);
160                            list.add(user);
161                            String[] groupUsers = (String[]) list.toArray(new String[0]);
162                            SERealmGroupHelper.updateGroup(group, groupUsers);
163                        }
164                    } catch (Exception e) {
165                        actionResponse.setRenderParameter("message",
166                                "ERROR: Error in SEUsersPortlet while adding user "+user+" to group "+group+". Cause: "+e.getMessage());
167                    }
168                } else if ("new".equals(action)) {
169                    currAction = "new";
170                } else if ("edit".equals(action)) {
171                    currAction = "edit";
172                }
173                actionResponse.setRenderParameter("currAction", currAction);
174                if (user != null) {
175                    actionResponse.setRenderParameter("user", user);
176                }
177            } catch (Exception e) {
178                actionResponse.setRenderParameter("message",
179                        "Error encountered in SEUsersPortlet. Cause: "
180                                + e.getMessage());
181            }
182        }
183    
184    }