View Javadoc

1   package org.appfuse.webapp.pages;
2   
3   import de.agilecoders.wicket.core.markup.html.bootstrap.common.NotificationMessage;
4   import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
5   import org.apache.wicket.model.Model;
6   import org.apache.wicket.model.StringResourceModel;
7   import org.appfuse.model.User;
8   import org.appfuse.service.UserExistsException;
9   import org.wicketstuff.annotation.mount.MountPath;
10  
11  import javax.servlet.http.HttpServletRequest;
12  
13  /**
14   * Page for editing current user.
15   *
16   * @author Marcin ZajÄ…czkowski, 2011-03-13
17   */
18  @MountPath("userEdit")
19  @AuthorizeInstantiation({"ROLE_ADMIN", "ROLE_USER"})
20  public class CurrentUserEdit extends AbstractUserEdit {
21  
22      private static final String USER_PROFILE_PROPERTY_PREFIX = "userProfile";
23  
24      public CurrentUserEdit() {
25          super(NO_RESPONSE_PAGE, USER_PROFILE_PROPERTY_PREFIX, new Model<User>());
26      }
27  
28      @Override
29      protected void onInitialize() {
30  
31          //TODO: MZA: Here or in constructor?
32          //TODO: Quite odd - before super
33          User user = getUserManager().getUserByUsername(getCurrentUserUsername());
34          //TODO: An ugly hack required to not force user to enter his password on each edition. Will be fixed in APF-1370
35          user.setConfirmPassword(user.getPassword());
36          setUser(user);
37  
38          super.onInitialize();
39  
40      }
41  
42      private String getCurrentUserUsername() {
43          String username = ((HttpServletRequest)getRequest().getContainerRequest()).getRemoteUser();
44          if (username == null) {
45              throw new IllegalStateException("Unable to get login of current user");
46          }
47          return username;
48      }
49  
50      //TODO: MZA: Duplication with FromListUserEdit
51      @Override
52      protected void onSaveButtonSubmit() {
53          User user = getUser();
54  
55          log.info("(current) onSubmit: {}", user);
56          log.info("(current) onSubmit (address): {}", user.getAddress());
57  
58          try {
59              getUserManager().saveUser(user);
60              getSession().info(createDefaultInfoNotificationMessage(
61                      new StringResourceModel("user.added", this, null, new Object[]{user.getFullName()})));
62              resolveAndSetResponsePage();
63          } catch (UserExistsException e) {
64              log.warn("User already exists", e);
65              error(new NotificationMessage(new StringResourceModel("errors.existing.user", this, null, new Object[] {
66                      user.getUsername(), user.getEmail()})
67              ));
68          }
69      }
70  
71      @Override
72      protected void onDeleteButtonSubmit() {
73          throw new IllegalStateException("Delete button should not be able to use on edit current user page");
74      }
75  
76      @Override
77      protected boolean getDisplayRolesGroupVisibility() {
78          return true;
79      }
80  
81      @Override
82      protected boolean getAccountSettingsGroupVisibility() {
83          return false;
84      }
85  
86      @Override
87      protected boolean getDeleteButtonVisibility() {
88          return false;
89      }
90  }