001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/user/tags/sakai_2-4-1/user-tool/tool/src/java/org/sakaiproject/user/tool/UsersAction.java $
003: * $Id: UsersAction.java 14594 2006-09-14 16:58:12Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.user.tool;
021:
022: import java.util.List;
023:
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.sakaiproject.authz.cover.SecurityService;
028: import org.sakaiproject.cheftool.Context;
029: import org.sakaiproject.cheftool.JetspeedRunData;
030: import org.sakaiproject.cheftool.PagedResourceActionII;
031: import org.sakaiproject.cheftool.PortletConfig;
032: import org.sakaiproject.cheftool.RunData;
033: import org.sakaiproject.cheftool.VelocityPortlet;
034: import org.sakaiproject.cheftool.api.Menu;
035: import org.sakaiproject.cheftool.api.MenuItem;
036: import org.sakaiproject.cheftool.menu.MenuEntry;
037: import org.sakaiproject.cheftool.menu.MenuImpl;
038: import org.sakaiproject.component.cover.ServerConfigurationService;
039: import org.sakaiproject.event.api.SessionState;
040: import org.sakaiproject.event.cover.UsageSessionService;
041: import org.sakaiproject.thread_local.cover.ThreadLocalManager;
042: import org.sakaiproject.tool.cover.SessionManager;
043: import org.sakaiproject.user.api.Authentication;
044: import org.sakaiproject.user.api.AuthenticationException;
045: import org.sakaiproject.user.api.Evidence;
046: import org.sakaiproject.user.api.User;
047: import org.sakaiproject.user.api.UserAlreadyDefinedException;
048: import org.sakaiproject.user.api.UserEdit;
049: import org.sakaiproject.user.api.UserIdInvalidException;
050: import org.sakaiproject.user.api.UserLockedException;
051: import org.sakaiproject.user.api.UserNotDefinedException;
052: import org.sakaiproject.user.api.UserPermissionException;
053: import org.sakaiproject.user.cover.AuthenticationManager;
054: import org.sakaiproject.user.cover.UserDirectoryService;
055: import org.sakaiproject.util.ExternalTrustedEvidence;
056: import org.sakaiproject.util.RequestFilter;
057: import org.sakaiproject.util.ResourceLoader;
058: import org.sakaiproject.util.StringUtil;
059:
060: /**
061: * <p>
062: * UsersAction is the Sakai users editor.
063: * </p>
064: */
065: public class UsersAction extends PagedResourceActionII {
066: private static ResourceLoader rb = new ResourceLoader("admin");
067:
068: /**
069: * {@inheritDoc}
070: */
071: protected List readResourcesPage(SessionState state, int first,
072: int last) {
073: // search?
074: String search = StringUtil.trimToNull((String) state
075: .getAttribute(STATE_SEARCH));
076:
077: if (search != null) {
078: return UserDirectoryService
079: .searchUsers(search, first, last);
080: }
081:
082: return UserDirectoryService.getUsers(first, last);
083: }
084:
085: /**
086: * {@inheritDoc}
087: */
088: protected int sizeResources(SessionState state) {
089: // search?
090: String search = StringUtil.trimToNull((String) state
091: .getAttribute(STATE_SEARCH));
092:
093: if (search != null) {
094: return UserDirectoryService.countSearchUsers(search);
095: }
096:
097: return UserDirectoryService.countUsers();
098: }
099:
100: /**
101: * Populate the state object, if needed.
102: */
103: protected void initState(SessionState state,
104: VelocityPortlet portlet, JetspeedRunData rundata) {
105: super .initState(state, portlet, rundata);
106:
107: PortletConfig config = portlet.getPortletConfig();
108:
109: if (state.getAttribute("single-user") == null) {
110: state.setAttribute("single-user", new Boolean(config
111: .getInitParameter("single-user", "false")));
112: state.setAttribute("include-password", new Boolean(config
113: .getInitParameter("include-password", "true")));
114: }
115:
116: if (state.getAttribute("create-user") == null) {
117: state.setAttribute("create-user", new Boolean(config
118: .getInitParameter("create-user", "false")));
119: state.setAttribute("create-login", new Boolean(config
120: .getInitParameter("create-login", "false")));
121: }
122:
123: if (state.getAttribute("create-type") == null) {
124: state.setAttribute("create-type", config.getInitParameter(
125: "create-type", ""));
126: }
127:
128: } // initState
129:
130: /**
131: * build the context
132: */
133: public String buildMainPanelContext(VelocityPortlet portlet,
134: Context context, RunData rundata, SessionState state) {
135: context.put("tlang", rb);
136: boolean singleUser = ((Boolean) state
137: .getAttribute("single-user")).booleanValue();
138: boolean createUser = ((Boolean) state
139: .getAttribute("create-user")).booleanValue();
140:
141: // if not logged in as the super user, we won't do anything
142: if ((!singleUser) && (!createUser)
143: && (!SecurityService.isSuperUser())) {
144: context.put("tlang", rb);
145: return (String) getContext(rundata).get("template")
146: + "_noaccess";
147: }
148:
149: String template = null;
150:
151: // for the create-user create-login case, we set this in the do so we can process the redirect here
152: if (state.getAttribute("redirect") != null) {
153: state.removeAttribute("redirect");
154: sendParentRedirect((HttpServletResponse) ThreadLocalManager
155: .get(RequestFilter.CURRENT_HTTP_RESPONSE),
156: ServerConfigurationService.getPortalUrl());
157: return template;
158: }
159:
160: // put $action into context for menus, forms and links
161: context.put(Menu.CONTEXT_ACTION, state
162: .getAttribute(STATE_ACTION));
163:
164: // check mode and dispatch
165: String mode = (String) state.getAttribute("mode");
166:
167: if ((singleUser) && (mode != null) && (mode.equals("edit"))) {
168: template = buildEditContext(state, context);
169: } else if (singleUser) {
170: String id = SessionManager.getCurrentSessionUserId();
171: state.setAttribute("user-id", id);
172: template = buildViewContext(state, context);
173: } else if (createUser) {
174: template = buildCreateContext(state, context);
175: } else if (mode == null) {
176: template = buildListContext(state, context);
177: } else if (mode.equals("new")) {
178: template = buildNewContext(state, context);
179: } else if (mode.equals("edit")) {
180: template = buildEditContext(state, context);
181: } else if (mode.equals("confirm")) {
182: template = buildConfirmRemoveContext(state, context);
183: } else {
184: Log.warn("chef", "UsersAction: mode: " + mode);
185: template = buildListContext(state, context);
186: }
187:
188: String prefix = (String) getContext(rundata).get("template");
189: return prefix + template;
190:
191: } // buildNormalContext
192:
193: /**
194: * Build the context for the main list mode.
195: */
196: private String buildListContext(SessionState state, Context context) {
197: // put the service in the context
198: context.put("service", UserDirectoryService.getInstance());
199:
200: // put all (internal) users into the context
201: context.put("users", prepPage(state));
202:
203: // build the menu
204: Menu bar = new MenuImpl();
205: if (UserDirectoryService.allowAddUser()) {
206: bar.add(new MenuEntry(rb.getString("useact.newuse"), null,
207: true, MenuItem.CHECKED_NA, "doNew"));
208: }
209:
210: // add the paging commands
211: addListPagingMenus(bar, state);
212:
213: // add the search commands
214: addSearchMenus(bar, state);
215:
216: // add the refresh commands
217: addRefreshMenus(bar, state);
218:
219: if (bar.size() > 0) {
220: context.put(Menu.CONTEXT_MENU, bar);
221: }
222:
223: return "_list";
224:
225: } // buildListContext
226:
227: /**
228: * Build the context for the new user mode.
229: */
230: private String buildNewContext(SessionState state, Context context) {
231: // put the service in the context
232: context.put("service", UserDirectoryService.getInstance());
233:
234: // include the password fields?
235: context.put("incPw", state.getAttribute("include-password"));
236:
237: context.put("incType", Boolean.valueOf(true));
238:
239: String value = (String) state.getAttribute("valueEid");
240: if (value != null)
241: context.put("valueEid", value);
242:
243: value = (String) state.getAttribute("valueFirstName");
244: if (value != null)
245: context.put("valueFirstName", value);
246:
247: value = (String) state.getAttribute("valueLastName");
248: if (value != null)
249: context.put("valueLastName", value);
250:
251: value = (String) state.getAttribute("valueEmail");
252: if (value != null)
253: context.put("valueEmail", value);
254:
255: value = (String) state.getAttribute("valueType");
256: if (value != null)
257: context.put("valueType", value);
258:
259: return "_edit";
260:
261: } // buildNewContext
262:
263: /**
264: * Build the context for the create user mode.
265: */
266: private String buildCreateContext(SessionState state,
267: Context context) {
268: // put the service in the context
269: context.put("service", UserDirectoryService.getInstance());
270:
271: // is the type to be pre-set
272: context.put("type", state.getAttribute("create-type"));
273:
274: // password is required when using Gateway New Account tool
275: // attribute "create-user" is true only for New Account tool
276: context.put("pwRequired", state.getAttribute("create-user"));
277:
278: String value = (String) state.getAttribute("valueEid");
279: if (value != null)
280: context.put("valueEid", value);
281:
282: value = (String) state.getAttribute("valueFirstName");
283: if (value != null)
284: context.put("valueFirstName", value);
285:
286: value = (String) state.getAttribute("valueLastName");
287: if (value != null)
288: context.put("valueLastName", value);
289:
290: value = (String) state.getAttribute("valueEmail");
291: if (value != null)
292: context.put("valueEmail", value);
293:
294: return "_create";
295:
296: } // buildCreateContext
297:
298: /**
299: * Build the context for the new user mode.
300: */
301: private String buildEditContext(SessionState state, Context context) {
302:
303: // put the service in the context
304: context.put("service", UserDirectoryService.getInstance());
305:
306: // name the html form for user edit fields
307: context.put("form-name", "user-form");
308:
309: // get the user to edit
310: UserEdit user = (UserEdit) state.getAttribute("user");
311: context.put("user", user);
312:
313: // is super user/admin user?
314: context.put("superUser", Boolean.valueOf(SecurityService
315: .isSuperUser()));
316:
317: // include the password fields?
318: context.put("incPw", state.getAttribute("include-password"));
319:
320: // include type fields (not if single user)
321: boolean singleUser = ((Boolean) state
322: .getAttribute("single-user")).booleanValue();
323: context.put("incType", Boolean.valueOf(!singleUser));
324:
325: // build the menu
326: // we need the form fields for the remove...
327: boolean menuPopulated = false;
328: Menu bar = new MenuImpl();
329: if ((!singleUser)
330: && (UserDirectoryService.allowRemoveUser(user.getId()))) {
331: bar
332: .add(new MenuEntry(rb.getString("useact.remuse"),
333: null, true, MenuItem.CHECKED_NA,
334: "doRemove", "user-form"));
335: menuPopulated = true;
336: }
337:
338: if (menuPopulated) {
339: context.put(Menu.CONTEXT_MENU, bar);
340: }
341:
342: String value = (String) state.getAttribute("valueEid");
343: if (value != null)
344: context.put("valueEid", value);
345:
346: value = (String) state.getAttribute("valueFirstName");
347: if (value != null)
348: context.put("valueFirstName", value);
349:
350: value = (String) state.getAttribute("valueLastName");
351: if (value != null)
352: context.put("valueLastName", value);
353:
354: value = (String) state.getAttribute("valueEmail");
355: if (value != null)
356: context.put("valueEmail", value);
357:
358: value = (String) state.getAttribute("valueType");
359: if (value != null)
360: context.put("valueType", value);
361:
362: return "_edit";
363:
364: } // buildEditContext
365:
366: /**
367: * Build the context for the view user mode.
368: */
369: private String buildViewContext(SessionState state, Context context) {
370: if (Log.getLogger("chef").isDebugEnabled()) {
371: Log.debug("chef", this + ".buildViewContext");
372: }
373:
374: // get current user's id
375: String id = (String) state.getAttribute("user-id");
376:
377: // get the user and put in state as "user"
378: try {
379: User user = UserDirectoryService.getUser(id);
380: context.put("user", user);
381:
382: // name the html form for user edit fields
383: context.put("form-name", "user-form");
384:
385: state.setAttribute("mode", "view");
386:
387: // make sure we can do an edit
388: try {
389: UserEdit edit = UserDirectoryService.editUser(id);
390: UserDirectoryService.cancelEdit(edit);
391: context.put("enableEdit", "true");
392: } catch (UserNotDefinedException e) {
393: } catch (UserPermissionException e) {
394: } catch (UserLockedException e) {
395: }
396:
397: // disable auto-updates while not in list mode
398: disableObservers(state);
399: } catch (UserNotDefinedException e) {
400: Log.warn("chef", "UsersAction.doEdit: user not found: "
401: + id);
402:
403: addAlert(state, rb.getString("useact.use") + " " + id + " "
404: + rb.getString("useact.notfou"));
405: state.removeAttribute("mode");
406:
407: // make sure auto-updates are enabled
408: enableObserver(state);
409: }
410:
411: return "_view";
412:
413: } // buildViewContext
414:
415: /**
416: * Build the context for the new user mode.
417: */
418: private String buildConfirmRemoveContext(SessionState state,
419: Context context) {
420: // get the user to edit
421: UserEdit user = (UserEdit) state.getAttribute("user");
422: context.put("user", user);
423:
424: return "_confirm_remove";
425:
426: } // buildConfirmRemoveContext
427:
428: /**
429: * doNew called when "eventSubmit_doNew" is in the request parameters to add a new user
430: */
431: public void doNew(RunData data, Context context) {
432: SessionState state = ((JetspeedRunData) data)
433: .getPortletSessionState(((JetspeedRunData) data)
434: .getJs_peid());
435: state.setAttribute("mode", "new");
436:
437: // mark the user as new, so on cancel it can be deleted
438: state.setAttribute("new", "true");
439:
440: // disable auto-updates while not in list mode
441: disableObservers(state);
442:
443: } // doNew
444:
445: /**
446: * doEdit called when "eventSubmit_doEdit" is in the request parameters to edit a user
447: */
448: public void doEdit(RunData data, Context context) {
449: SessionState state = ((JetspeedRunData) data)
450: .getPortletSessionState(((JetspeedRunData) data)
451: .getJs_peid());
452: String id = data.getParameters().getString("id");
453: state.removeAttribute("user");
454: state.removeAttribute("newuser");
455:
456: // get the user
457: try {
458: UserEdit user = UserDirectoryService.editUser(id);
459: state.setAttribute("user", user);
460: state.setAttribute("mode", "edit");
461:
462: // disable auto-updates while not in list mode
463: disableObservers(state);
464: } catch (UserNotDefinedException e) {
465: Log.warn("chef", "UsersAction.doEdit: user not found: "
466: + id);
467:
468: addAlert(state, rb.getString("useact.use") + " " + id + " "
469: + rb.getString("useact.notfou"));
470: state.removeAttribute("mode");
471:
472: // make sure auto-updates are enabled
473: enableObserver(state);
474: } catch (UserPermissionException e) {
475: addAlert(state, rb.getString("useact.youdonot1") + " " + id);
476: state.removeAttribute("mode");
477:
478: // make sure auto-updates are enabled
479: enableObserver(state);
480: } catch (UserLockedException e) {
481: addAlert(state, rb.getString("useact.somone") + " " + id);
482: state.removeAttribute("mode");
483:
484: // make sure auto-updates are enabled
485: enableObserver(state);
486: }
487:
488: } // doEdit
489:
490: /**
491: * doModify called when "eventSubmit_doModify" is in the request parameters to edit a user
492: */
493: public void doModify(RunData data, Context context) {
494: if (Log.getLogger("chef").isDebugEnabled()) {
495: Log.debug("chef", this + ".doModify");
496: }
497:
498: SessionState state = ((JetspeedRunData) data)
499: .getPortletSessionState(((JetspeedRunData) data)
500: .getJs_peid());
501: String id = data.getParameters().getString("id");
502: state.removeAttribute("user");
503: state.removeAttribute("newuser");
504:
505: // get the user
506: try {
507: UserEdit user = UserDirectoryService.editUser(id);
508: state.setAttribute("user", user);
509: state.setAttribute("mode", "edit");
510:
511: // disable auto-updates while not in list mode
512: disableObservers(state);
513: } catch (UserNotDefinedException e) {
514: Log.warn("chef", "UsersAction.doEdit: user not found: "
515: + id);
516:
517: addAlert(state, rb.getString("useact.use") + " " + id + " "
518: + rb.getString("useact.notfou"));
519: state.removeAttribute("mode");
520:
521: // make sure auto-updates are enabled
522: enableObserver(state);
523: } catch (UserPermissionException e) {
524: addAlert(state, rb.getString("useact.youdonot1") + " " + id);
525: state.removeAttribute("mode");
526:
527: // make sure auto-updates are enabled
528: enableObserver(state);
529: } catch (UserLockedException e) {
530: addAlert(state, rb.getString("useact.somone") + " " + id);
531: state.removeAttribute("mode");
532:
533: // make sure auto-updates are enabled
534: enableObserver(state);
535: }
536:
537: } // doModify
538:
539: /**
540: * doSave called when "eventSubmit_doSave" is in the request parameters to save user edits
541: */
542: public void doSave(RunData data, Context context) {
543: SessionState state = ((JetspeedRunData) data)
544: .getPortletSessionState(((JetspeedRunData) data)
545: .getJs_peid());
546:
547: // read the form - if rejected, leave things as they are
548: if (!readUserForm(data, state))
549: return;
550:
551: // commit the change
552: UserEdit edit = (UserEdit) state.getAttribute("user");
553: if (edit != null) {
554: try {
555: UserDirectoryService.commitEdit(edit);
556: } catch (UserAlreadyDefinedException e) {
557: // TODO: this means the EID value is not unique... when we implement EID fully, we need to check this and send it back to the user
558: Log.warn("chef", "UsersAction.doSave()" + e);
559: addAlert(state, rb.getString("useact.theuseid1"));
560: return;
561: }
562: }
563:
564: User user = edit;
565: if (user == null) {
566: user = (User) state.getAttribute("newuser");
567: }
568:
569: // cleanup
570: state.removeAttribute("user");
571: state.removeAttribute("newuser");
572: state.removeAttribute("new");
573: state.removeAttribute("valueEid");
574: state.removeAttribute("valueFirstName");
575: state.removeAttribute("valueLastName");
576: state.removeAttribute("valueEmail");
577: state.removeAttribute("valueType");
578:
579: // return to main mode
580: state.removeAttribute("mode");
581:
582: // make sure auto-updates are enabled
583: enableObserver(state);
584:
585: if ((user != null)
586: && ((Boolean) state.getAttribute("create-login"))
587: .booleanValue()) {
588: try {
589: // login - use the fact that we just created the account as external evidence
590: Evidence e = new ExternalTrustedEvidence(user.getEid());
591: Authentication a = AuthenticationManager
592: .authenticate(e);
593: if (!UsageSessionService
594: .login(
595: a,
596: (HttpServletRequest) ThreadLocalManager
597: .get(RequestFilter.CURRENT_HTTP_REQUEST))) {
598: addAlert(state, rb
599: .getString("useact.tryloginagain"));
600: }
601: } catch (AuthenticationException ex) {
602: Log.warn("chef",
603: "UsersAction.doSave: authentication failure: "
604: + ex);
605: }
606:
607: // redirect to home (on next build)
608: state.setAttribute("redirect", "");
609: }
610:
611: } // doSave
612:
613: /**
614: * doCancel called when "eventSubmit_doCancel" is in the request parameters to cancel user edits
615: */
616: public void doCancel(RunData data, Context context) {
617: SessionState state = ((JetspeedRunData) data)
618: .getPortletSessionState(((JetspeedRunData) data)
619: .getJs_peid());
620:
621: // get the user
622: UserEdit user = (UserEdit) state.getAttribute("user");
623: if (user != null) {
624: // if this was a new, delete the user
625: if ("true".equals(state.getAttribute("new"))) {
626: // remove
627: try {
628: UserDirectoryService.removeUser(user);
629: } catch (UserPermissionException e) {
630: addAlert(state, rb.getString("useact.youdonot2")
631: + " " + user.getId());
632: }
633: } else {
634: UserDirectoryService.cancelEdit(user);
635: }
636: }
637:
638: // cleanup
639: state.removeAttribute("user");
640: state.removeAttribute("newuser");
641: state.removeAttribute("new");
642: state.removeAttribute("valueEid");
643: state.removeAttribute("valueFirstName");
644: state.removeAttribute("valueLastName");
645: state.removeAttribute("valueEmail");
646: state.removeAttribute("valueType");
647:
648: // return to main mode
649: state.removeAttribute("mode");
650:
651: // make sure auto-updates are enabled
652: enableObserver(state);
653:
654: } // doCancel
655:
656: /**
657: * doRemove called when "eventSubmit_doRemove" is in the request par ameters to confirm removal of the user
658: */
659: public void doRemove(RunData data, Context context) {
660: SessionState state = ((JetspeedRunData) data)
661: .getPortletSessionState(((JetspeedRunData) data)
662: .getJs_peid());
663:
664: // read the form - if rejected, leave things as they are
665: if (!readUserForm(data, state))
666: return;
667:
668: // go to remove confirm mode
669: state.setAttribute("mode", "confirm");
670:
671: } // doRemove
672:
673: /**
674: * doRemove_confirmed called when "eventSubmit_doRemove_confirmed" is in the request parameters to remove the user
675: */
676: public void doRemove_confirmed(RunData data, Context context) {
677: SessionState state = ((JetspeedRunData) data)
678: .getPortletSessionState(((JetspeedRunData) data)
679: .getJs_peid());
680:
681: // get the user
682: UserEdit user = (UserEdit) state.getAttribute("user");
683:
684: // remove
685: try {
686: UserDirectoryService.removeUser(user);
687: } catch (UserPermissionException e) {
688: addAlert(state, rb.getString("useact.youdonot2") + " "
689: + user.getId());
690: }
691:
692: // cleanup
693: state.removeAttribute("user");
694: state.removeAttribute("newuser");
695: state.removeAttribute("new");
696:
697: // go to main mode
698: state.removeAttribute("mode");
699:
700: // make sure auto-updates are enabled
701: enableObserver(state);
702:
703: } // doRemove_confirmed
704:
705: /**
706: * doCancel_remove called when "eventSubmit_doCancel_remove" is in the request parameters to cancel user removal
707: */
708: public void doCancel_remove(RunData data, Context context) {
709: SessionState state = ((JetspeedRunData) data)
710: .getPortletSessionState(((JetspeedRunData) data)
711: .getJs_peid());
712:
713: // return to edit mode
714: state.setAttribute("mode", "edit");
715:
716: } // doCancel_remove
717:
718: /**
719: * Read the user form and update the user in state.
720: *
721: * @return true if the form is accepted, false if there's a validation error (an alertMessage will be set)
722: */
723: private boolean readUserForm(RunData data, SessionState state) {
724: // boolean parameters and values
725: // --------------Mode--singleUser-createUser-typeEnable
726: // Admin New-----new---false------false------true
727: // Admin Update--edit--false------false------true
728: // Gateway New---null---false------true-------false
729: // Account Edit--edit--true-------false------false
730:
731: // read the form
732: String id = StringUtil.trimToNull(data.getParameters()
733: .getString("id"));
734: String eid = StringUtil.trimToNull(data.getParameters()
735: .getString("eid"));
736: state.setAttribute("valueEid", eid);
737: String firstName = StringUtil.trimToNull(data.getParameters()
738: .getString("first-name"));
739: state.setAttribute("valueFirstName", firstName);
740: String lastName = StringUtil.trimToNull(data.getParameters()
741: .getString("last-name"));
742: state.setAttribute("valueLastName", lastName);
743: String email = StringUtil.trimToNull(data.getParameters()
744: .getString("email"));
745: state.setAttribute("valueEmail", email);
746: String pw = StringUtil.trimToNull(data.getParameters()
747: .getString("pw"));
748: String pwConfirm = StringUtil.trimToNull(data.getParameters()
749: .getString("pw0"));
750:
751: String mode = (String) state.getAttribute("mode");
752: boolean singleUser = ((Boolean) state
753: .getAttribute("single-user")).booleanValue();
754: boolean createUser = ((Boolean) state
755: .getAttribute("create-user")).booleanValue();
756:
757: boolean typeEnable = false;
758: String type = null;
759: if ((mode != null) && (mode.equalsIgnoreCase("new"))) {
760: typeEnable = true;
761: } else if ((mode != null) && (mode.equalsIgnoreCase("edit"))
762: && (!singleUser)) {
763: typeEnable = true;
764: }
765:
766: if (typeEnable) {
767: // for the case of Admin User tool creating new user
768: type = StringUtil.trimToNull(data.getParameters()
769: .getString("type"));
770: state.setAttribute("valueType", type);
771: } else {
772: if (createUser) {
773: // for the case of Gateway Account tool creating new user
774: type = (String) state.getAttribute("create-type");
775: }
776: }
777:
778: //insure valid email address
779: if (email != null && !email.matches(".+@.+\\..+")) {
780: addAlert(state, rb.getString("useact.invemail"));
781: return false;
782: }
783:
784: // get the user
785: UserEdit user = (UserEdit) state.getAttribute("user");
786:
787: // add if needed
788: if (user == null) {
789: // make sure we have eid
790: if (eid == null) {
791: addAlert(state, rb.getString("usecre.eidmis"));
792: return false;
793: }
794:
795: // if in create mode, make sure we have a password
796: if (createUser) {
797: if (pw == null) {
798: addAlert(state, rb.getString("usecre.pasismis"));
799: return false;
800: }
801: }
802:
803: // make sure we have matching password fields
804: if (StringUtil.different(pw, pwConfirm)) {
805: addAlert(state, rb.getString("usecre.pass"));
806: return false;
807: }
808:
809: try {
810: // add the user in one step so that all you need is add not update permission
811: // (the added might be "anon", and anon has add but not update permission)
812: User newUser = UserDirectoryService.addUser(id, eid,
813: firstName, lastName, email, pw, type, null);
814:
815: // put the user in the state
816: state.setAttribute("newuser", newUser);
817: } catch (UserAlreadyDefinedException e) {
818: addAlert(state, rb.getString("useact.theuseid1"));
819: return false;
820: } catch (UserIdInvalidException e) {
821: addAlert(state, rb.getString("useact.theuseid2"));
822: return false;
823: } catch (UserPermissionException e) {
824: addAlert(state, rb.getString("useact.youdonot3"));
825: return false;
826: }
827: }
828:
829: // update
830: else {
831: if (!user.isActiveEdit()) {
832: try {
833: // add the user in one step so that all you need is add not update permission
834: // (the added might be "anon", and anon has add but not update permission)
835: user = UserDirectoryService.editUser(user.getId());
836:
837: // put the user in the state
838: state.setAttribute("user", user);
839: } catch (UserLockedException e) {
840: addAlert(state, rb.getString("useact.somels"));
841: return false;
842: } catch (UserNotDefinedException e) {
843: addAlert(state, rb.getString("useact.use") + " "
844: + user.getId() + " "
845: + rb.getString("useact.notfou"));
846:
847: return false;
848: } catch (UserPermissionException e) {
849: addAlert(state, rb.getString("useact.youdonot3"));
850: return false;
851: }
852: }
853:
854: // eid, pw, type might not be editable
855: if (eid != null)
856: user.setEid(eid);
857: user.setFirstName(firstName);
858: user.setLastName(lastName);
859: user.setEmail(email);
860: if (type != null)
861: user.setType(type);
862:
863: // make sure we have matching password fields
864: if (StringUtil.different(pw, pwConfirm)) {
865: addAlert(state, rb.getString("usecre.pass"));
866: return false;
867: }
868:
869: if (pw != null)
870: user.setPassword(pw);
871: }
872:
873: return true;
874: }
875: }
|