org.homiefund.web.controllers.SignUpController.java Source code

Java tutorial

Introduction

Here is the source code for org.homiefund.web.controllers.SignUpController.java

Source

/**
 * Copyright  2016 REPLACE ME OWNER (REPLACE ME YEAR)
 *
 * 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.homiefund.web.controllers;

import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.dozer.Mapper;
import org.homiefund.api.dto.InvitationDTO;
import org.homiefund.api.dto.UserDTO;
import org.homiefund.api.service.RegistrationService;
import org.homiefund.exceptions.FieldException;
import org.homiefund.web.forms.UserRegistrationForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.validation.Valid;
import java.util.Optional;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 21.9.2016.
 */
@Controller("SUC")
@Log4j2
public class SignUpController {
    private static final String EMPTY_PASSWORD = StringUtils.EMPTY;
    @Autowired
    private RegistrationService registrationService;
    @Autowired
    private Mapper mapper;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @GetMapping(value = { "/signup/", "/signup/{inviteToken}/" })
    public String signup(@PathVariable Optional<String> inviteToken, Model model) {
        UserRegistrationForm form = new UserRegistrationForm();
        if (inviteToken.isPresent()) {
            form.setInviteToken(inviteToken.get());
        }

        model.addAttribute("userRegistrationForm", form);

        return "signup";
    }

    @PostMapping("/signup/")
    public String submit(@Valid @ModelAttribute UserRegistrationForm userRegistrationForm, BindingResult result,
            Model model) {
        log.info(userRegistrationForm.getName());
        if (result.hasErrors()) {
            clearPasswords(userRegistrationForm);

            return "signup";
        }

        try {
            Optional<InvitationDTO> invitation;
            if (StringUtils.isEmpty(userRegistrationForm.getInviteToken())) {
                invitation = Optional.empty();
            } else {
                InvitationDTO invite = new InvitationDTO();
                invite.setHash(userRegistrationForm.getInviteToken());

                invitation = Optional.of(invite);
            }

            userRegistrationForm.setPassword(bCryptPasswordEncoder.encode(userRegistrationForm.getPassword()));
            registrationService.register(mapper.map(userRegistrationForm, UserDTO.class), invitation);
        } catch (FieldException fe) {
            result.rejectValue(fe.getField(), fe.getMessage());

            clearPasswords(userRegistrationForm);

            return "signup";
        }

        return "redirect:/";
    }

    private void clearPasswords(UserRegistrationForm userRegistrationForm) {
        userRegistrationForm.setPassword(EMPTY_PASSWORD);
        userRegistrationForm.setPasswordAgain(EMPTY_PASSWORD);
    }
}