View Javadoc

1   package org.unitedfront2.web.controller.account;
2   
3   import javax.annotation.Resource;
4   
5   import org.springframework.beans.factory.annotation.Autowired;
6   import org.springframework.stereotype.Repository;
7   import org.springframework.validation.Errors;
8   import org.springframework.validation.Validator;
9   import org.springframework.webflow.execution.Event;
10  import org.springframework.webflow.execution.RequestContext;
11  import org.unitedfront2.domain.Account;
12  import org.unitedfront2.domain.AccountTable;
13  import org.unitedfront2.domain.MailAddrAndPassword;
14  import org.unitedfront2.domain.MailAddrUsedByOtherException;
15  import org.unitedfront2.web.WebUtils;
16  import org.unitedfront2.web.controller.FormAction;
17  
18  /**
19   * パスワード変更のアクションクラスです。
20   *
21   * @author kurokkie
22   *
23   */
24  @Repository(value = "account.mailAddrChangeAction")
25  public class MailAddrChangeAction extends FormAction {
26  
27      /** 発行するパスワードの最小文字列長 (4) */
28      public static final int PASSWORD_MIN_LENGTH = 4;
29  
30      /** 発行するパスワードの最大文字列長 (8) */
31      public static final int PASSWORD_MAX_LENGTH = 8;
32  
33      /** メールテンプレート名 (account.MailAddrChange-mail) */
34      public static final String MAIL_ADDR_CHANGE_MAIL_TEMPLATE_NAME
35          = "account.MailAddrChange-mail";
36  
37      /** アカウントテーブル */
38      @Autowired private AccountTable accountTable;
39  
40      @Override
41      protected void initAction() {
42          super.initAction();
43          setMailTemplateName(MAIL_ADDR_CHANGE_MAIL_TEMPLATE_NAME);
44          setFormObjectClass(Account.class);
45      }
46  
47      /**
48       * メールアドレスの変更とパスワードの再発行を行います。
49       *
50       * @param context リクエストコンテキスト
51       * @return イベント
52       * @throws MailAddrUsedByOtherException メールアドレスが既に使われている
53       */
54      public Event changeMailAddrAndReissuePassword(RequestContext context)
55          throws MailAddrUsedByOtherException {
56  
57          int id = WebUtils.getAccount(context).getId();
58          String newMailAddr = ((Account) getFormObject(context)).getMailAddr();
59          String newPassword = Account.createRandomPassword(PASSWORD_MIN_LENGTH,
60                  PASSWORD_MAX_LENGTH);
61  
62          Account newAccount = accountTable.get(id);
63          newAccount.setMailAddr(newMailAddr);
64          newAccount.setPlainPassword(newPassword);
65          newAccount.encrypt();
66          try {
67              newAccount.store();
68          } catch (MailAddrUsedByOtherException e) {
69              logger.warn(e.getMessage());
70              Errors errors = getBindingErrors(context, newAccount);
71              errors.rejectValue("mailAddr",
72                      "account.validation.mailAddrUserdByOther",
73                      new Object[] {e.getMailAddr()}, e.getMessage());
74              throw e;
75          }
76  
77          context.getRequestScope().put("mailAddrAndPassword",
78                  new MailAddrAndPassword(newAccount.getMailAddr(), newPassword));
79  
80          doSendMail(context);
81  
82          WebUtils.setWidgets(WebUtils.getSession(context), null);
83  
84          return success();
85      }
86  
87      /**
88       * メールアドレスの変更を行います。
89       *
90       * @param context リクエストコンテキスト
91       * @return イベント
92       * @throws MailAddrUsedByOtherException メールアドレスが既に使われている
93       */
94      public Event changeMailAddr(RequestContext context)
95          throws MailAddrUsedByOtherException {
96  
97          Account account = (Account) getFormObject(context);
98          int id = WebUtils.getAccount(context).getId();
99          Account newAccount = accountTable.find(id);
100         newAccount.setMailAddr(account.getMailAddr());
101         try {
102             newAccount.store();
103         } catch (MailAddrUsedByOtherException e) {
104             logger.warn(e.getMessage());
105             Errors errors = getBindingErrors(context, account);
106             errors.rejectValue("mailAddr",
107                 "account.validation.mailAddrUserdByOther",
108                 new Object[] {e.getMailAddr()}, e.getMessage());
109             throw e;
110         }
111 
112         WebUtils.setWidgets(WebUtils.getSession(context), null);
113 
114         return success();
115     }
116 
117     @Override
118     @Resource(name = "account.mailAddrFormValidator")
119     public void setValidator(Validator validator) {
120         super.setValidator(validator);
121     }
122 }