2023-11-01 12:32:54 +01:00
|
|
|
package catering.users;
|
|
|
|
|
|
|
|
import org.springframework.ui.Model;
|
2023-11-11 17:56:01 +01:00
|
|
|
import org.springframework.validation.Errors;
|
|
|
|
|
|
|
|
import org.salespointframework.useraccount.Role;
|
|
|
|
import org.salespointframework.useraccount.UserAccount;
|
|
|
|
import org.salespointframework.useraccount.web.LoggedIn;
|
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
2023-11-01 12:32:54 +01:00
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
2023-11-11 17:56:01 +01:00
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
2023-11-01 12:32:54 +01:00
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
2023-11-11 17:56:01 +01:00
|
|
|
import jakarta.validation.Valid;
|
|
|
|
|
2023-11-01 12:32:54 +01:00
|
|
|
@Controller
|
|
|
|
public class UserController {
|
|
|
|
|
|
|
|
private final UserManagement userManagement;
|
|
|
|
|
|
|
|
UserController(UserManagement userManagerment){
|
|
|
|
this.userManagement = userManagerment;
|
|
|
|
};
|
|
|
|
|
2023-11-11 17:56:01 +01:00
|
|
|
@GetMapping("/unauthorized")
|
|
|
|
String unauthorized(){
|
|
|
|
return "unauthorized";
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/register")
|
|
|
|
String register() {
|
|
|
|
return "register";
|
|
|
|
}
|
2023-11-01 12:32:54 +01:00
|
|
|
|
2023-11-11 17:56:01 +01:00
|
|
|
@PostMapping("/register")
|
|
|
|
String register(@Valid RegistrationForm form, Errors result) {
|
|
|
|
if (result.hasErrors()){
|
|
|
|
return "register";
|
|
|
|
}
|
|
|
|
userManagement.createCustomer(form.getUsername(),form.getAddress(),form.getPassword());
|
|
|
|
return "redirect:/login";
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/login")
|
|
|
|
public String loginPage(){
|
|
|
|
return "login";
|
|
|
|
}
|
2023-11-01 12:32:54 +01:00
|
|
|
|
2023-11-11 17:56:01 +01:00
|
|
|
@GetMapping("/profile")
|
|
|
|
@PreAuthorize("isAuthenticated()")
|
|
|
|
public String viewProfile(Model model, @LoggedIn UserAccount userAccount) {
|
|
|
|
if (userAccount == null) {
|
|
|
|
return "redirect:/";
|
|
|
|
}
|
|
|
|
User user = userManagement.getUserByAccount(userAccount).get();
|
|
|
|
model.addAttribute("user", user);
|
2023-11-01 12:32:54 +01:00
|
|
|
return "profile";
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/profile")
|
2023-11-11 17:56:01 +01:00
|
|
|
@PreAuthorize("isAuthenticated()")
|
|
|
|
public String editProfile(@LoggedIn UserAccount userAccount, @RequestParam String password, @RequestParam String address, @RequestParam String username) {
|
|
|
|
User user = userManagement.getUserByAccount(userAccount).get();
|
2023-11-01 12:32:54 +01:00
|
|
|
|
2023-11-11 17:56:01 +01:00
|
|
|
if (!username.isBlank()) {
|
|
|
|
user.setUsername(username);
|
2023-11-01 12:32:54 +01:00
|
|
|
}
|
2023-11-11 17:56:01 +01:00
|
|
|
if (!address.isBlank()) {
|
|
|
|
user.setAddress(address);
|
2023-11-01 12:32:54 +01:00
|
|
|
}
|
2023-11-11 17:56:01 +01:00
|
|
|
if (!password.isBlank()) {
|
|
|
|
userManagement.setPassword(password, user.getUserAccount());
|
2023-11-01 12:32:54 +01:00
|
|
|
}
|
|
|
|
|
2023-11-11 17:56:01 +01:00
|
|
|
userManagement.save(user);
|
2023-11-01 12:32:54 +01:00
|
|
|
|
|
|
|
return "redirect:/profile";
|
|
|
|
}
|
|
|
|
|
2023-11-11 17:56:01 +01:00
|
|
|
@GetMapping("/profile/disable")
|
|
|
|
@PreAuthorize("hasRole('CUSTOMER')")
|
|
|
|
public String disableUser(@LoggedIn UserAccount userAccount) {
|
|
|
|
userManagement.disableUserAccount(userAccount);
|
2023-11-01 12:32:54 +01:00
|
|
|
return "redirect:/";
|
|
|
|
}
|
2023-11-11 17:56:01 +01:00
|
|
|
|
|
|
|
@GetMapping("/customers")
|
|
|
|
@PreAuthorize("hasRole('ADMIN')")
|
|
|
|
public String getCustomer(Model model) {
|
|
|
|
model.addAttribute("title", "Kundenverwaltung");
|
|
|
|
model.addAttribute("customers", userManagement.getUsers().findAll().stream().filter(customer -> customer.getUserAccount().hasRole(Role.of("CUSTOMER"))).toList());
|
|
|
|
return "customers";
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/customers/remove/{id}")
|
|
|
|
@PreAuthorize("hasRole('ADMIN')")
|
|
|
|
public String removeCustomer(@PathVariable("id") User user, Model model) {
|
|
|
|
userManagement.disableUserAccount(user.getUserAccount());
|
|
|
|
return "redirect:/customers";
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/customers/edit/{id}")
|
|
|
|
@PreAuthorize("hasRole('ADMIN')")
|
|
|
|
public String editCustomer(@PathVariable("id") User user, Model model) {
|
|
|
|
model.addAttribute("customer", user);
|
|
|
|
return "edit-customer";
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/customers/edit/{id}")
|
|
|
|
@PreAuthorize("hasRole('ADMIN')")
|
|
|
|
public String updateCustomer(@PathVariable("id") User user, @RequestParam String username, @RequestParam String address, Model model) {
|
|
|
|
user.setUsername(username);
|
|
|
|
user.setAddress(address);
|
|
|
|
userManagement.save(user);
|
|
|
|
return "redirect:/customers";
|
|
|
|
}
|
2023-11-01 12:32:54 +01:00
|
|
|
}
|