KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > admin > ManageUsers


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002,2003 Fraunhofer Gesellschaft
5  * Fraunhofer Institut for Computer Architecture and Software Technology
6  * All Rights Reserved.
7  *
8  * Please visit http://snipsnap.org/ for updates and contact.
9  *
10  * --LICENSE NOTICE--
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  * --LICENSE NOTICE--
25  */

26 package org.snipsnap.net.admin;
27
28 import org.snipsnap.config.Configuration;
29 import org.snipsnap.user.UserManager;
30 import org.snipsnap.user.User;
31 import org.snipsnap.user.Roles;
32 import org.snipsnap.container.Components;
33 import org.snipsnap.snip.HomePage;
34 import org.snipsnap.app.Application;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Set JavaDoc;
40 import java.util.HashSet JavaDoc;
41
42 public class ManageUsers implements SetupHandler {
43   public String JavaDoc getName() {
44     return "users";
45   }
46
47   public Map JavaDoc setup(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Configuration config, Map JavaDoc errors) {
48     UserManager um = (UserManager) Components.getComponent(UserManager.class);
49     if (request.getParameter("remove") != null) {
50       User user = um.load(request.getParameter("remove"));
51       if(user != null) {
52         um.remove(user);
53       }
54       return errors;
55     } else if (request.getParameter("edit") != null) {
56       User user = um.load(request.getParameter("login"));
57       request.setAttribute("editUser", user);
58       request.setAttribute("edit", "true");
59       if(null == user) {
60         request.setAttribute("editUser", new User());
61         request.setAttribute("create", "true");
62       }
63     } else if (request.getParameter("save") != null || request.getParameter("create") != null) {
64       request.setAttribute("edit", "true");
65       String JavaDoc login = request.getParameter("config.users.login");
66       User user = um.load(login);
67       if(request.getParameter("create") != null) {
68         request.setAttribute("create", request.getParameter("create"));
69
70         if(user != null || null == login) {
71           errors.put("users.login", "users.login");
72         } else {
73           User tmp = new User(login, "", "");
74           if(update(request, errors, tmp)) {
75             user = um.create(login, tmp.getPasswd(), tmp.getEmail());
76             update(request, errors, user);
77             if(errors.size() == 0) {
78               User currUser = Application.get().getUser();
79               Application.get().setUser(user);
80               HomePage.create(user.getLogin());
81               Application.get().setUser(currUser);
82             }
83           }
84         }
85       } else if(null == user) {
86         errors.put("users.login", "users.login");
87         return errors;
88       }
89
90       if (update(request, errors, user)) {
91         um.store(user);
92       }
93       request.setAttribute("editUser", user);
94       if(errors.size() == 0) {
95         request.removeAttribute("edit");
96       }
97     }
98
99     return errors;
100   }
101
102   /**
103    * Update a user, check validity and equality of the input.
104    */

105   private boolean update(HttpServletRequest JavaDoc request, Map JavaDoc errors, User user) {
106
107     String JavaDoc email = request.getParameter("config.users.email");
108     String JavaDoc nPass = request.getParameter("config.users.password");
109     String JavaDoc nPass2 = request.getParameter("config.users.password.vrfy");
110     String JavaDoc status = request.getParameter("config.users.status");
111     String JavaDoc roles[] = request.getParameterValues("config.users.roles");
112
113     boolean modified = false;
114     if ((user.getEmail() == null && email != null) ||
115       (user.getEmail() != null && !user.getEmail().equals(email))) {
116       modified = true;
117       user.setEmail(email);
118     }
119
120     if ((user.getStatus() == null && status != null) ||
121       (user.getStatus() != null && !user.getStatus().equals(status))) {
122       modified = true;
123       user.setStatus(status);
124     }
125
126     Roles newRoles = new Roles(parseRoles(roles));
127     if (!user.getRoles().equals(newRoles)) {
128       if (!newRoles.getRoleSet().isEmpty() && !Roles.allRoles().containsAll(newRoles.getRoleSet())) {
129         errors.put("users.roles", "users.roles");
130       } else {
131         modified = true;
132         user.setRoles(newRoles);
133       }
134     }
135
136
137     if (nPass != null && nPass.length() > 0) {
138       if (nPass.equals(nPass2)) {
139         modified = true;
140         user.setPasswd(nPass);
141       } else {
142         errors.put("users.password", "users.password");
143       }
144     }
145
146     return modified;
147   }
148
149   private Set JavaDoc parseRoles(String JavaDoc roles[]) {
150     Set JavaDoc list = new HashSet JavaDoc();
151     for (int i = 0; roles != null && i < roles.length; i++) {
152       list.add(roles[i]);
153     }
154     return list;
155   }
156 }
157
Popular Tags