/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.spring.mvc.controllers.security;
import org.internna.iwebmvc.core.context.ContextHolder;
import org.internna.iwebmvc.core.dao.DAO;
import org.internna.iwebmvc.core.dao.SecurityDAO;
import org.internna.iwebmvc.model.core.Locale;
import org.internna.iwebmvc.model.security.UserImpl;
import org.internna.iwebmvc.spring.binding.LocaleBinder;
import org.internna.iwebmvc.spring.services.dwr.AJAXValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
/**
* Administers User entity
*
* @author Jose Noheda
* @since 1.0
*/
@Controller
@SessionAttributes("registereduser")
@RequestMapping("/registration.iwebmvc")
public class RegistrationController {
@Autowired @Qualifier("baseDAO") protected DAO dao;
@Autowired protected SecurityDAO securityDao;
@Autowired protected ContextHolder holder;
@Autowired protected AJAXValidator validator;
@RequestMapping(method = RequestMethod.GET)
public String showForm(@RequestParam(required = false, value = "username") String username, ModelMap model) {
model.addAttribute("registereduser", new UserImpl());
model.addAttribute("availableLocales", holder.getAvailableLocales());
return "security/registration";
}
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Locale.class, new LocaleBinder(dao));
}
@RequestMapping(method = RequestMethod.POST)
public String onSubmit(@ModelAttribute("registereduser") UserImpl user, @RequestParam("captcha") String captcha, SessionStatus status) throws Exception {
if (!validator.validateCaptcha(captcha, true) || validator.validateUserName(user.getUsername())) return "security/registration";
user.addRole(securityDao.findAuthority("ROLE_USER"));
securityDao.createUser(user);
status.setComplete();
return "redirect:/security/filter.iwebmvc";
}
}