MainController.java 3.21 KB
package com.PFE.ServerManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashSet;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.Authentication;

@Controller
public class MainController {

    @Autowired
    CustomerRepository customerRepository;

    @Autowired
    RoleRepository roleRepository;

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @GetMapping(value="/")
    public String homeRedirection(){
        return "redirect:home";
    }

    @GetMapping(value="/home")
    public ModelAndView home() {
        ModelAndView modelAndView = new ModelAndView();
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        Customer customer = customerRepository.findByPseudo(auth.getName());
        modelAndView.addObject("customerName", customer.getPseudo());
        modelAndView.setViewName("home");
        return modelAndView;
    }

    @GetMapping(path="/registration")
    public String registration() {
        return "registration";//fait le lien automatiquement avec le page html du même nom  //return "redirect:/....";
    }

    @GetMapping(path="/denied")
    public String denied() {
        return "denied";
    }

    @PostMapping(path="/registration")
    public ModelAndView addNewUser(@RequestParam String pseudo, @RequestParam String password) {
        //Model map, ModelAndView ou l'utilisation direct comme dans la méthode précédente sont 3 méthodes qui permettent d'envoyer des informations et donc de changer l'apparence d'une page
        ModelAndView modelAndView = new ModelAndView(); // il n'est peut être pas utile d'utiliser ModelAndView
        Customer n = new Customer();
        n.setPseudo(pseudo);
        n.setPassword(bCryptPasswordEncoder.encode(password));
        n.setActive(1);
        Customer temp = customerRepository.findByPseudo(pseudo);

        Role role = new Role(); // l'utilisation d'un role au lieu d'un tableau semble valide, ormis la première ligne de la table qui n'est pas utilisé
        role.setRole("ADMIN");

        HashSet<Role> hset = new HashSet<Role>();
        hset.add(role);

        n.setRoles(hset);

        if(temp != null) {
            modelAndView.addObject("ok", "l'utilisateur existe déjà");
            //return "login?fail";
        }
        else {
            modelAndView.addObject("ok", "l'utilisateur a bien été ajouté");
            customerRepository.save(n);
        }
        modelAndView.setViewName("registration");
        return modelAndView;
    }

    @GetMapping(path="/login")
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @GetMapping(path="/all")
    public @ResponseBody Iterable<Customer> getAllUsers() {
        return customerRepository.findAll();
    }

    @GetMapping(value="/success")
    public String success(){
        return "success";
    }
}