1 /* vim:set ts=4 sts=4 sw=4 noet fenc=utf-8:
3 Copyright 2009 senju@users.sourceforge.jp
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
18 package jp.sourceforge.rabbitBTS.controllers;
20 import java.util.Date;
22 import javax.servlet.http.HttpServletRequest;
24 import jp.sourceforge.rabbitBTS.Sht;
25 import jp.sourceforge.rabbitBTS.exceptions.HasNotValidIdException;
26 import jp.sourceforge.rabbitBTS.exceptions.RabbitBTSException;
27 import jp.sourceforge.rabbitBTS.models.Account;
28 import jp.sourceforge.rabbitBTS.services.AccountService;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Controller;
32 import org.springframework.ui.ModelMap;
33 import org.springframework.validation.BindingResult;
34 import org.springframework.validation.Validator;
35 import org.springframework.web.bind.annotation.RequestMapping;
36 import org.springframework.web.bind.annotation.RequestMethod;
39 public final class RegisterController extends BaseController implements
41 private static final String REDIRECT_HOME_HTML = "redirect:/home/";
44 private AccountService accountService;
47 private Validator validator;
52 * ユーザーがgoogleにログインしていない場合、googleのログイン画面にリダイレクトする。
53 * ユーザーがアカウントを持っている場合、ユーザーホームにリダイレクトする。
54 * ユーザーがgoogleにログイン済みでかつアカウントを持っていない時だけ、画面を表示する。
60 @RequestMapping(method = RequestMethod.GET)
61 public String index(HttpServletRequest req, ModelMap map) {
64 final Account account = this.accountService.fetchAccount();
65 if (account == null) {
67 map.addAttribute(new Account());
70 // すでに登録済みの場合ホームへリダイレクト
71 Sht.log(this).trace("既に登録済み");
72 ret = REDIRECT_HOME_HTML;
74 } catch (final HasNotValidIdException e) {
76 Sht.log(this).trace("ログインが必要");
77 ret = "redirect:" + Sht.loginUrl(req);
95 * <li>バリデートに失敗した場合、エラーメッセージ
96 * <li>Account登録に失敗した場合(二重登録等)、ロールバック、エラーメッセージ
103 @RequestMapping(method = RequestMethod.POST)
104 public String index(Account account, BindingResult result, ModelMap map) {
105 if (!this.isCsrfSafe(result)) {
109 account.setLastAccess(new Date());
111 account.setEmail(Sht.user().getEmail());
112 this.validator.validate(account, result);
113 if (result.hasErrors()) {
114 Sht.log(this).trace("ユーザ登録画面にてバリデートに失敗");
120 if (this.accountService.getAccountByNickName(account.getNickName()) != null) {
121 result.rejectValue("nickName", "Account.nickName[duplicate]");
122 Sht.log(this).trace("nickName重複チェックエラー");
126 this.accountService.registAccount(account);
127 } catch (final RabbitBTSException e) {
128 Sht.log(this).warn("ユーザー登録で例外発生", e);
130 map.addAttribute("errorMessage", "登録に失敗しました。再度やりなおしてください。");
133 return REDIRECT_HOME_HTML;