001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.admin.struts.actions;
020:
021: import java.io.IOException;
022: import java.text.MessageFormat;
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import javax.servlet.ServletException;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.roller.ui.core.struts.actions.UserBaseAction;
033: import org.apache.struts.action.ActionError;
034: import org.apache.struts.action.ActionErrors;
035: import org.apache.struts.action.ActionForm;
036: import org.apache.struts.action.ActionForward;
037: import org.apache.struts.action.ActionMapping;
038: import org.apache.struts.action.ActionMessage;
039: import org.apache.struts.action.ActionMessages;
040: import org.apache.roller.RollerException;
041: import org.apache.roller.business.search.IndexManager;
042: import org.apache.roller.business.Roller;
043: import org.apache.roller.business.RollerFactory;
044: import org.apache.roller.business.UserManager;
045: import org.apache.roller.pojos.UserData;
046: import org.apache.roller.ui.core.BasePageModel;
047: import org.apache.roller.ui.core.RollerRequest;
048: import org.apache.roller.ui.core.RollerSession;
049: import org.apache.roller.ui.admin.struts.formbeans.UserAdminForm;
050: import org.apache.commons.lang.StringUtils;
051:
052: /////////////////////////////////////////////////////////////////////////////
053: /**
054: * Actions for administering a user allow user query, edit, and delete of users.
055: *
056: * @struts.action name="userAdminForm" path="/roller-ui/admin/user"
057: * scope="request" parameter="method"
058: *
059: * @struts.action-forward name="adminUser.page" path=".UserAdmin"
060: */
061: public final class UserAdminAction extends UserBaseAction {
062: private static Log mLogger = LogFactory.getFactory().getInstance(
063: UserAdminAction.class);
064:
065: //-----------------------------------------------------------------------
066: /**
067: * Show query for user page or, if userName specified in request,
068: * show the admin user page for the specified user.
069: */
070: public ActionForward edit(ActionMapping mapping,
071: ActionForm actionForm, HttpServletRequest request,
072: HttpServletResponse response) throws IOException,
073: ServletException {
074: ActionForward forward = mapping.findForward("adminUser.page");
075: try {
076: UserData user = null;
077: RollerRequest rreq = RollerRequest
078: .getRollerRequest(request);
079: RollerSession rollerSession = RollerSession
080: .getRollerSession(request);
081: if (rollerSession.isGlobalAdminUser()) {
082: UserAdminForm userForm = (UserAdminForm) actionForm;
083: UserManager mgr = RollerFactory.getRoller()
084: .getUserManager();
085: if (userForm != null && userForm.getUserName() != null
086: && !userForm.isNewUser()) {
087: ActionMessages msgs = getErrors(request);
088: msgs = (msgs == null) ? new ActionMessages() : msgs;
089: user = mgr.getUserByUserName(
090: userForm.getUserName(), null);
091: if (user != null) {
092: userForm.copyFrom(user, request.getLocale());
093: // User must set new password twice
094: userForm.setPasswordText(null);
095: userForm.setPasswordConfirm(null);
096: } else {
097: msgs
098: .add(
099: ActionErrors.GLOBAL_ERROR,
100: new ActionMessage(
101: "userAdmin.invalidNewUserName"));
102: userForm.setUserName("");
103: }
104: if (request.getSession()
105: .getAttribute("cookieLogin") != null) {
106: // TODO: make it possible to change passwords
107: // regardless of remember me
108: msgs.add(ActionErrors.GLOBAL_ERROR,
109: new ActionMessage(
110: "userAdmin.cookieLogin"));
111: }
112: saveErrors(request, msgs);
113: }
114: request.setAttribute("model", new UserAdminPageModel(
115: request, response, mapping, userForm, user));
116: } else {
117: forward = mapping.findForward("access-denied");
118: }
119: } catch (Exception e) {
120: mLogger.error("ERROR in action", e);
121: throw new ServletException(e);
122: }
123: return forward;
124: }
125:
126: //-----------------------------------------------------------------------
127: /**
128: * Process POST of edited user data, may cause delete of user.
129: */
130: public ActionForward update(ActionMapping mapping,
131: ActionForm actionForm, HttpServletRequest request,
132: HttpServletResponse response) throws IOException,
133: ServletException {
134: ActionForward forward = mapping.findForward("adminUser.page");
135: ActionMessages msgs = new ActionMessages();
136: try {
137: RollerRequest rreq = RollerRequest
138: .getRollerRequest(request);
139: RollerSession rollerSession = RollerSession
140: .getRollerSession(request);
141: if (rollerSession.isGlobalAdminUser()) {
142: UserManager mgr = RollerFactory.getRoller()
143: .getUserManager();
144: UserAdminForm userForm = (UserAdminForm) actionForm;
145:
146: if (userForm.isNewUser()) {
147: UserData user = new UserData();
148: userForm.copyTo(user, request.getLocale());
149: user.setId(null);
150: user.setDateCreated(new java.util.Date());
151: user.setEnabled(Boolean.TRUE);
152:
153: // Check username and email addresses
154: msgs = validate(userForm, msgs);
155:
156: // Must have matching passwords and confirm passwords
157: if (!StringUtils
158: .isEmpty(userForm.getPasswordText())
159: && !StringUtils.isEmpty(userForm
160: .getPasswordConfirm())) {
161: try {
162: user.resetPassword(RollerFactory
163: .getRoller(), userForm
164: .getPasswordText(), userForm
165: .getPasswordConfirm());
166: } catch (RollerException e) {
167: msgs
168: .add(
169: ActionErrors.GLOBAL_ERROR,
170: new ActionError(
171: "userSettings.passwordResetError"));
172: }
173: } else {
174: msgs
175: .add(
176: ActionErrors.GLOBAL_ERROR,
177: new ActionError(
178: "userSettings.needPasswordTwice"));
179: }
180:
181: // If no error messages, then add user
182: if (msgs.isEmpty()) {
183: try {
184: // Save new user to database
185: mgr.addUser(user);
186: RollerFactory.getRoller().flush();
187:
188: msgs.add(ActionMessages.GLOBAL_MESSAGE,
189: new ActionMessage(
190: "userSettings.saved"));
191: saveMessages(request, msgs);
192:
193: // Operation complete, return to edit action
194: userForm.setUserName(null);
195: userForm.setNewUser((false));
196:
197: } catch (RollerException e) {
198: // Add and commit failed, so show nice error message
199: msgs.add(ActionErrors.GLOBAL_ERROR,
200: new ActionError(e.getMessage()));
201: saveErrors(request, msgs);
202: }
203: } else {
204: saveErrors(request, msgs);
205: }
206: return edit(mapping, actionForm, request, response);
207:
208: } else {
209:
210: UserData user = mgr.getUser(userForm.getId());
211: userForm.copyTo(user, request.getLocale());
212:
213: // Check username and email addresses
214: msgs = validate(userForm, msgs);
215:
216: // If user set both password and passwordConfirm then reset
217: if (!StringUtils
218: .isEmpty(userForm.getPasswordText())
219: && !StringUtils.isEmpty(userForm
220: .getPasswordConfirm())) {
221: try {
222: user.resetPassword(RollerFactory
223: .getRoller(), userForm
224: .getPasswordText(), userForm
225: .getPasswordConfirm());
226: } catch (RollerException e) {
227: msgs
228: .add(
229: ActionErrors.GLOBAL_ERROR,
230: new ActionMessage(
231: "userSettings.passwordResetError"));
232: }
233: } else if (!StringUtils.isEmpty(userForm
234: .getPasswordText())
235: || !StringUtils.isEmpty(userForm
236: .getPasswordConfirm())) {
237: // But it's an error to specify only one of the two
238: msgs
239: .add(
240: ActionErrors.GLOBAL_ERROR,
241: new ActionMessage(
242: "userSettings.needPasswordTwice"));
243: }
244:
245: if (msgs.isEmpty()) {
246: try {
247: // Persist changes to user
248: mgr.saveUser(user);
249: RollerFactory.getRoller().flush();
250:
251: msgs.add(ActionMessages.GLOBAL_MESSAGE,
252: new ActionMessage(
253: "userSettings.saved"));
254: saveMessages(request, msgs);
255:
256: // Operation complete, return to edit action
257: userForm.setUserName(null);
258:
259: } catch (RollerException e) {
260: msgs.add(ActionErrors.GLOBAL_ERROR,
261: new ActionError(
262: "error.untranslated",
263: e.getRootCauseMessage()));
264: saveErrors(request, msgs);
265: }
266: } else {
267: saveErrors(request, msgs);
268: }
269: }
270:
271: return edit(mapping, actionForm, request, response);
272: } else {
273: forward = mapping.findForward("access-denied");
274: }
275: } catch (Exception e) {
276: mLogger.error("ERROR in action", e);
277: throw new ServletException(e);
278: }
279: return forward;
280: }
281:
282: //-----------------------------------------------------------------------
283: /**
284: * Cancel from edit user.
285: */
286: public ActionForward cancel(ActionMapping mapping,
287: ActionForm actionForm, HttpServletRequest request,
288: HttpServletResponse response) throws IOException,
289: ServletException {
290: UserAdminForm userForm = (UserAdminForm) actionForm;
291: userForm.setUserName(null);
292: userForm.setNewUser(false);
293: return edit(mapping, actionForm, request, response);
294: }
295:
296: //-----------------------------------------------------------------------
297: /**
298: * Create new user.
299: */
300: public ActionForward newUser(ActionMapping mapping,
301: ActionForm actionForm, HttpServletRequest request,
302: HttpServletResponse response) throws IOException,
303: ServletException {
304: UserAdminForm userForm = (UserAdminForm) actionForm;
305: userForm.setNewUser(true);
306: userForm.setEnabled(Boolean.TRUE);
307: return edit(mapping, actionForm, request, response);
308: }
309:
310: //-----------------------------------------------------------------------
311: /**
312: * Rebuild a user's search index.
313: */
314: public ActionForward index(ActionMapping mapping,
315: ActionForm actionForm, HttpServletRequest request,
316: HttpServletResponse response) throws IOException,
317: ServletException {
318: try {
319: RollerRequest rreq = RollerRequest
320: .getRollerRequest(request);
321: RollerSession rollerSession = RollerSession
322: .getRollerSession(request);
323: if (rollerSession.isGlobalAdminUser()) {
324: UserAdminForm uaf = (UserAdminForm) actionForm;
325:
326: // if admin requests an index be re-built, do it
327: IndexManager manager = RollerFactory.getRoller()
328: .getIndexManager();
329: manager.rebuildWebsiteIndex();
330: request.getSession().setAttribute(
331: RollerSession.STATUS_MESSAGE,
332: "Successfully scheduled rebuild of index for");
333: }
334: } catch (Exception e) {
335: mLogger.error("ERROR in action", e);
336: throw new ServletException(e);
337: }
338: return edit(mapping, actionForm, request, response);
339: }
340:
341: public class UserAdminPageModel extends BasePageModel {
342: private UserAdminForm userAdminForm = null;
343: private List permissions = new ArrayList();
344:
345: public UserAdminPageModel(HttpServletRequest request,
346: HttpServletResponse response, ActionMapping mapping,
347: UserAdminForm form, UserData user)
348: throws RollerException {
349: super ("dummy", request, response, mapping);
350: userAdminForm = form;
351:
352: if (user != null) {
353: Roller roller = RollerFactory.getRoller();
354: permissions = roller.getUserManager()
355: .getAllPermissions(user);
356: }
357: }
358:
359: public String getTitle() {
360: if (StringUtils.isEmpty(userAdminForm.getUserName())
361: && userAdminForm.isNewUser()) {
362: return bundle
363: .getString("userAdmin.title.createNewUser");
364: } else if (StringUtils.isEmpty(userAdminForm.getUserName())) {
365: return bundle.getString("userAdmin.title.searchUser");
366: }
367: return MessageFormat.format(bundle
368: .getString("userAdmin.title.editUser"),
369: new Object[] { userAdminForm.getUserName() });
370: }
371:
372: public List getPermissions() {
373: return permissions;
374: }
375:
376: public void setPermissions(List permissions) {
377: this.permissions = permissions;
378: }
379: }
380: }
|