com.pw.ism.controllers.AccountController.java Source code

Java tutorial

Introduction

Here is the source code for com.pw.ism.controllers.AccountController.java

Source

package com.pw.ism.controllers;

import com.pw.ism.users.User;
import com.pw.ism.users.UserRepository;

import java.security.Principal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author NRS
 */
@RequestMapping("/account")
@Controller
public class AccountController {

    private UserRepository userRepo;

    @Autowired
    private PasswordEncoder passEncoder;

    @Autowired
    public AccountController(UserRepository userRepo) {
        this.userRepo = userRepo;
    }

    @RequestMapping
    public ModelAndView getDetails(Principal principal) {
        String userName = principal.getName();
        User currentUser = userRepo.findBySsoId(userName);
        return new ModelAndView("account/details", "user", currentUser);
    }

    @RequestMapping(value = "/pass", params = "form", method = RequestMethod.POST)
    public ModelAndView changePass(User user, BindingResult bindingResult, Principal principal,
            RedirectAttributes redirectAttributes) {
        if (bindingResult.hasErrors()) {
            return new ModelAndView("account", "formErrors", bindingResult.getAllErrors());
        }
        User currentUser = userRepo.findBySsoId(principal.getName());
        currentUser.setPassword(passEncoder.encode(user.getPassword()));
        userRepo.save(currentUser);
        redirectAttributes.addFlashAttribute("globalMessage", "Password changed");
        return new ModelAndView("redirect:/account");
    }

    @RequestMapping(value = "/details", params = "form", method = RequestMethod.POST)
    public ModelAndView changeDetails(User user, BindingResult bindingResult, Principal principal,
            RedirectAttributes redirectAttributes) {
        if (bindingResult.hasErrors()) {
            return new ModelAndView("account", "formErrors", bindingResult.getAllErrors());
        }
        User currentUser = userRepo.findBySsoId(principal.getName());
        currentUser.setEmail(user.getEmail());
        currentUser.setFirstName(user.getFirstName());
        currentUser.setLastName(user.getLastName());
        userRepo.save(currentUser);
        redirectAttributes.addFlashAttribute("globalMessage", "Details changed");
        return new ModelAndView("redirect:/account");
    }
}