From 5d3664d1480e2afaf14df1798ba93e4a63003389 Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Tue, 2 Jan 2024 20:02:25 +0300 Subject: [PATCH] Admin is able to set role and change display name to anyone --- .../marinesco/AdministatorAddUserForm.java | 9 ++-- src/main/java/ru/redrise/marinesco/User.java | 15 +++++-- .../ru/redrise/marinesco/UserGenerified.java | 11 +++-- .../security/ManageUsersController.java | 41 ++++++++++++++----- .../resources/templates/manage_users.html | 40 +++++++++++------- 5 files changed, 80 insertions(+), 36 deletions(-) diff --git a/src/main/java/ru/redrise/marinesco/AdministatorAddUserForm.java b/src/main/java/ru/redrise/marinesco/AdministatorAddUserForm.java index 68929bb..ea17e2c 100644 --- a/src/main/java/ru/redrise/marinesco/AdministatorAddUserForm.java +++ b/src/main/java/ru/redrise/marinesco/AdministatorAddUserForm.java @@ -1,6 +1,6 @@ package ru.redrise.marinesco; -import java.util.Collections; +import java.util.List; import org.springframework.security.crypto.password.PasswordEncoder; @@ -8,7 +8,6 @@ import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; import lombok.Data; -import ru.redrise.marinesco.data.RolesRepository; import ru.redrise.marinesco.security.UserRole; //TODO: refactor along with RegistrationForm.java @@ -27,13 +26,15 @@ public class AdministatorAddUserForm { @NotEmpty(message = "Display name could not be blank") private String displayname; - private UserRole role; + @NotNull + @Size(min=1, message="You must choose at least 1 role") + private List athorities; public User toUser(PasswordEncoder passwordEncoder){ return new User( username, passwordEncoder.encode(password), displayname, - Collections.singletonList(role)); + athorities); } } diff --git a/src/main/java/ru/redrise/marinesco/User.java b/src/main/java/ru/redrise/marinesco/User.java index e61966b..704e50f 100644 --- a/src/main/java/ru/redrise/marinesco/User.java +++ b/src/main/java/ru/redrise/marinesco/User.java @@ -2,7 +2,6 @@ package ru.redrise.marinesco; import java.util.List; -import org.hibernate.annotations.ManyToAny; import org.springframework.security.core.userdetails.UserDetails; import jakarta.persistence.Column; @@ -37,7 +36,7 @@ public class User implements UserDetails{ private String displayname; @ManyToMany(fetch = FetchType.EAGER) - private final List authorities; + private List authorities; public User(String username, String password, String displayname, List authorities){ this.username = username; @@ -66,11 +65,19 @@ public class User implements UserDetails{ return true; } - public void setRole(UserRole role){ // TODO + public void addRole(UserRole role){ this.authorities.add(role); } + public void removeRole(UserRole role){ + this.authorities.remove(role); + } + public boolean isAdmin(){ - return authorities.get(0).getAuthority().equals("ROLE_ADMIN"); + for (UserRole athority : authorities){ + if (athority.getAuthority().equals("ROLE_ADMIN")) + return true; + } + return false; } } diff --git a/src/main/java/ru/redrise/marinesco/UserGenerified.java b/src/main/java/ru/redrise/marinesco/UserGenerified.java index 2f70581..c7745d5 100644 --- a/src/main/java/ru/redrise/marinesco/UserGenerified.java +++ b/src/main/java/ru/redrise/marinesco/UserGenerified.java @@ -1,6 +1,7 @@ package ru.redrise.marinesco; import java.util.List; +import java.util.stream.Collectors; import lombok.Data; import lombok.NoArgsConstructor; @@ -16,12 +17,16 @@ public class UserGenerified { private String name; private String displayName; - private List role; + private List athorities; + private List athoritiesLost; - public UserGenerified(User user){ + public UserGenerified(User user, List allRolesList){ this.id = user.getId(); this.name = user.getUsername(); this.displayName = user.getDisplayname(); - this.role = user.getAuthorities(); + this.athorities = user.getAuthorities(); + athoritiesLost = allRolesList.stream() + .filter(element -> !athorities.contains(element)) + .collect(Collectors.toList()); } } \ No newline at end of file diff --git a/src/main/java/ru/redrise/marinesco/security/ManageUsersController.java b/src/main/java/ru/redrise/marinesco/security/ManageUsersController.java index 4bb49f0..30deeb1 100644 --- a/src/main/java/ru/redrise/marinesco/security/ManageUsersController.java +++ b/src/main/java/ru/redrise/marinesco/security/ManageUsersController.java @@ -10,6 +10,7 @@ import org.springframework.ui.Model; import org.springframework.validation.Errors; 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 org.springframework.web.bind.annotation.RequestMapping; @@ -21,7 +22,6 @@ import ru.redrise.marinesco.UserGenerified; import ru.redrise.marinesco.data.RolesRepository; import ru.redrise.marinesco.data.UserRepository; -//TODO @Slf4j @Controller @RequestMapping("/manage_users") @@ -55,15 +55,19 @@ public class ManageUsersController { public void addUsers(Model model) { Iterable users = userRepository.findAll(); List usersGen = new ArrayList<>(); + List allRolesList = new ArrayList<>(); + rolesRepository.findAll().forEach(allRolesList::add); + for (User user : users) { - usersGen.add(new UserGenerified(user)); + usersGen.add(new UserGenerified(user, allRolesList)); } - model.addAttribute("USR", usersGen); + model.addAttribute("users", usersGen); } + @ModelAttribute public void addRoles(Model model) { Iterable roles = rolesRepository.findAll(); - model.addAttribute("roles", roles); + model.addAttribute("rolesSet", roles); } @GetMapping @@ -71,15 +75,31 @@ public class ManageUsersController { return "manage_users"; } - @PostMapping("/delete") - public String processDelete(UserGenerified userGenerified) { - log.info(userGenerified.toString()); - - userRepository.deleteById(userGenerified.getId()); + @GetMapping("/delete/{id}") + public String delete(@PathVariable("id") String id) { + try { + long userId = Long.parseLong(id); + userRepository.deleteById(userId); + } catch (Exception e) { + log.error(id, e); + } return "redirect:/manage_users"; } + @PostMapping("/update") + public String updateRoles(UserGenerified userGenerified) { + User user = userRepository.findById(userGenerified.getId()).get(); + if (user == null) + return "redirect:/manage_users"; + + user.setAuthorities(userGenerified.getAthorities()); + user.setDisplayname(userGenerified.getDisplayName()); + userRepository.save(user); + + return "redirect:/manage_users"; + } + @PostMapping public String processNewUser(@Valid AdministatorAddUserForm form, Errors errors, Model model) { if (userRepository.findByUsername(form.getUsername()) != null) { @@ -92,7 +112,8 @@ public class ManageUsersController { } User user = userRepository.save(form.toUser(passwordEncoder)); - log.info("Added user {} {} {}", user.getId(), user.getUsername(), user.getDisplayname(), user.getAuthorities().get(0)); + log.info("Added user {} {} {}", user.getId(), user.getUsername(), user.getDisplayname(), + user.getAuthorities().get(0)); // Reloads page therefore new records appears return "redirect:/manage_users"; } diff --git a/src/main/resources/templates/manage_users.html b/src/main/resources/templates/manage_users.html index 3c75d9f..e8212c4 100644 --- a/src/main/resources/templates/manage_users.html +++ b/src/main/resources/templates/manage_users.html @@ -14,22 +14,29 @@

-

- user - - user - -
+
+ + + - - - - +
+ Display name: + +

Roles: +
+ +
+
+
+ +
+
- + Delete
+

Add user @@ -53,10 +60,13 @@

- - + Roles: +
+ +
+ +
+