001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.servlets;
017:
018: import java.util.Iterator;
019: import java.util.Locale;
020: import java.util.Map;
021: import java.util.TreeMap;
022: import java.util.Vector;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025: import org.acegisecurity.context.SecurityContextHolder;
026: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
027: import org.acegisecurity.ui.WebAuthenticationDetails;
028: import org.apache.commons.lang.LocaleUtils;
029: import org.apache.commons.lang.StringUtils;
030: import org.jamwiki.WikiBase;
031: import org.jamwiki.WikiConfiguration;
032: import org.jamwiki.WikiException;
033: import org.jamwiki.WikiMessage;
034: import org.jamwiki.authentication.WikiUserAuth;
035: import org.jamwiki.model.Role;
036: import org.jamwiki.model.VirtualWiki;
037: import org.jamwiki.model.WikiUser;
038: import org.jamwiki.model.WikiUserInfo;
039: import org.jamwiki.utils.Encryption;
040: import org.jamwiki.utils.WikiLogger;
041: import org.jamwiki.utils.WikiUtil;
042: import org.springframework.web.servlet.ModelAndView;
043: import org.springframework.web.servlet.i18n.SessionLocaleResolver;
044:
045: /**
046: * Used to process new user account setup.
047: */
048: public class RegisterServlet extends JAMWikiServlet {
049:
050: private static final WikiLogger logger = WikiLogger
051: .getLogger(RegisterServlet.class.getName());
052: protected static final String JSP_REGISTER = "register.jsp";
053:
054: /**
055: *
056: */
057: protected ModelAndView handleJAMWikiRequest(
058: HttpServletRequest request, HttpServletResponse response,
059: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
060: if (request.getParameter("function") == null) {
061: view(request, next, pageInfo);
062: } else {
063: register(request, next, pageInfo);
064: }
065: return next;
066: }
067:
068: /**
069: *
070: */
071: private void loadDefaults(HttpServletRequest request,
072: ModelAndView next, WikiPageInfo pageInfo, WikiUser user,
073: WikiUserInfo userInfo) throws Exception {
074: if (StringUtils.isBlank(user.getDefaultLocale())
075: && request.getLocale() != null) {
076: user.setDefaultLocale(request.getLocale().toString());
077: }
078: TreeMap locales = new TreeMap();
079: Map translations = WikiConfiguration.getInstance()
080: .getTranslations();
081: Iterator iterator = translations.keySet().iterator();
082: while (iterator.hasNext()) {
083: String key = (String) iterator.next();
084: String value = key + " - " + (String) translations.get(key);
085: locales.put(value, key);
086: }
087: Locale[] localeArray = Locale.getAvailableLocales();
088: for (int i = 0; i < localeArray.length; i++) {
089: String key = localeArray[i].toString();
090: String value = key + " - "
091: + localeArray[i].getDisplayName(localeArray[i]);
092: locales.put(value, key);
093: }
094: next.addObject("locales", locales);
095: next.addObject("newuser", user);
096: next.addObject("newuserinfo", userInfo);
097: pageInfo.setSpecial(true);
098: pageInfo.setContentJsp(JSP_REGISTER);
099: pageInfo.setPageTitle(new WikiMessage("register.title"));
100: }
101:
102: /**
103: *
104: */
105: private void login(HttpServletRequest request, WikiUserAuth user) {
106: UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
107: user, user.getPassword(), user.getAuthorities());
108: authentication
109: .setDetails(new WebAuthenticationDetails(request));
110: SecurityContextHolder.getContext().setAuthentication(
111: authentication);
112: }
113:
114: /**
115: *
116: */
117: private void register(HttpServletRequest request,
118: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
119: String virtualWikiName = WikiUtil
120: .getVirtualWikiFromURI(request);
121: WikiUserAuth user = this .setWikiUser(request);
122: WikiUserInfo userInfo = this .setWikiUserInfo(request);
123: next.addObject("newuser", user);
124: next.addObject("newuserinfo", userInfo);
125: Vector errors = validate(request, user);
126: if (!errors.isEmpty()) {
127: next.addObject("errors", errors);
128: String oldPassword = request.getParameter("oldPassword");
129: String newPassword = request.getParameter("newPassword");
130: String confirmPassword = request
131: .getParameter("confirmPassword");
132: if (oldPassword != null) {
133: next.addObject("oldPassword", oldPassword);
134: }
135: if (newPassword != null) {
136: next.addObject("newPassword", newPassword);
137: }
138: if (confirmPassword != null) {
139: next.addObject("confirmPassword", confirmPassword);
140: }
141: this .loadDefaults(request, next, pageInfo, user, userInfo);
142: } else {
143: WikiBase.getDataHandler().writeWikiUser(user, userInfo,
144: null);
145: // login the user
146: this .login(request, user);
147: // update the locale key since the user may have changed default locale
148: if (!StringUtils.isBlank(user.getDefaultLocale())) {
149: Locale locale = LocaleUtils.toLocale(user
150: .getDefaultLocale());
151: request
152: .getSession()
153: .setAttribute(
154: SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
155: locale);
156: }
157: VirtualWiki virtualWiki = WikiBase.getDataHandler()
158: .lookupVirtualWiki(virtualWikiName);
159: String topic = virtualWiki.getDefaultTopicName();
160: ServletUtil.redirect(next, virtualWikiName, topic);
161: }
162: }
163:
164: /**
165: *
166: */
167: private WikiUserAuth setWikiUser(HttpServletRequest request)
168: throws Exception {
169: String username = request.getParameter("login");
170: WikiUserAuth user = new WikiUserAuth(username);
171: String userIdString = request.getParameter("userId");
172: if (!StringUtils.isBlank(userIdString)) {
173: int userId = new Integer(userIdString).intValue();
174: if (userId > 0) {
175: user = new WikiUserAuth(WikiBase.getDataHandler()
176: .lookupWikiUser(userId, null));
177: }
178: }
179: user.setDisplayName(request.getParameter("displayName"));
180: String newPassword = request.getParameter("newPassword");
181: if (!StringUtils.isBlank(newPassword)) {
182: user.setPassword(Encryption.encrypt(newPassword));
183: }
184: user.setDefaultLocale(request.getParameter("defaultLocale"));
185: // FIXME - need to distinguish between add & update
186: user.setCreateIpAddress(ServletUtil.getIpAddress(request));
187: user.setLastLoginIpAddress(ServletUtil.getIpAddress(request));
188: return user;
189: }
190:
191: /**
192: *
193: */
194: private WikiUserInfo setWikiUserInfo(HttpServletRequest request)
195: throws Exception {
196: WikiUserInfo userInfo = new WikiUserInfo();
197: String username = request.getParameter("login");
198: String userIdString = request.getParameter("userId");
199: if (!StringUtils.isBlank(userIdString)) {
200: int userId = new Integer(userIdString).intValue();
201: if (userId > 0) {
202: userInfo = WikiBase.getUserHandler()
203: .lookupWikiUserInfo(username);
204: }
205: }
206: if (!WikiBase.getUserHandler().isWriteable()) {
207: return userInfo;
208: }
209: userInfo.setUsername(username);
210: userInfo.setEmail(request.getParameter("email"));
211: userInfo.setFirstName(request.getParameter("firstName"));
212: userInfo.setLastName(request.getParameter("lastName"));
213: String newPassword = request.getParameter("newPassword");
214: if (!StringUtils.isBlank(newPassword)) {
215: userInfo
216: .setEncodedPassword(Encryption.encrypt(newPassword));
217: }
218: return userInfo;
219: }
220:
221: /**
222: *
223: */
224: private Vector validate(HttpServletRequest request, WikiUser user)
225: throws Exception {
226: Vector errors = new Vector();
227: try {
228: WikiUtil.validateUserName(user.getUsername());
229: } catch (WikiException e) {
230: errors.add(e.getWikiMessage());
231: }
232: String oldPassword = request.getParameter("oldPassword");
233: if (user.getUserId() > 0
234: && !WikiBase.getUserHandler().authenticate(
235: user.getUsername(), oldPassword)) {
236: errors.add(new WikiMessage(
237: "register.error.oldpasswordinvalid"));
238: }
239: String newPassword = request.getParameter("newPassword");
240: String confirmPassword = request
241: .getParameter("confirmPassword");
242: if (user.getUserId() < 1 && StringUtils.isBlank(newPassword)) {
243: errors.add(new WikiMessage("register.error.passwordempty"));
244: }
245: if (!WikiBase.getUserHandler().isWriteable()
246: && !WikiBase.getUserHandler().authenticate(
247: user.getUsername(), newPassword)) {
248: errors.add(new WikiMessage(
249: "register.error.oldpasswordinvalid"));
250: }
251: if (!StringUtils.isBlank(newPassword)
252: || !StringUtils.isBlank(confirmPassword)) {
253: if (StringUtils.isBlank(newPassword)) {
254: errors.add(new WikiMessage("error.newpasswordempty"));
255: } else if (WikiBase.getUserHandler().isWriteable()
256: && StringUtils.isBlank(confirmPassword)) {
257: errors.add(new WikiMessage("error.passwordconfirm"));
258: } else if (WikiBase.getUserHandler().isWriteable()
259: && !newPassword.equals(confirmPassword)) {
260: errors.add(new WikiMessage(
261: "admin.message.passwordsnomatch"));
262: }
263: }
264: if (user.getUserId() < 1
265: && WikiBase.getDataHandler().lookupWikiUser(
266: user.getUsername(), null) != null) {
267: errors.add(new WikiMessage("register.error.logininvalid",
268: user.getUsername()));
269: }
270: return errors;
271: }
272:
273: /**
274: *
275: */
276: private void view(HttpServletRequest request, ModelAndView next,
277: WikiPageInfo pageInfo) throws Exception {
278: // FIXME - i suspect initializing with a null login is bad
279: WikiUser user = new WikiUser("");
280: WikiUserInfo userInfo = new WikiUserInfo();
281: if (ServletUtil.currentUser().hasRole(Role.ROLE_USER)) {
282: user = ServletUtil.currentUser();
283: userInfo = WikiBase.getUserHandler().lookupWikiUserInfo(
284: user.getUsername());
285: }
286: this.loadDefaults(request, next, pageInfo, user, userInfo);
287: }
288: }
|