diff --git a/src/main/java/ru/redrise/marinesco/User.java b/src/main/java/ru/redrise/marinesco/User.java index 969bdf1..957fc05 100644 --- a/src/main/java/ru/redrise/marinesco/User.java +++ b/src/main/java/ru/redrise/marinesco/User.java @@ -5,6 +5,7 @@ import java.util.List; import org.springframework.security.core.userdetails.UserDetails; import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; @@ -29,6 +30,8 @@ public class User implements UserDetails{ @GeneratedValue(strategy = GenerationType.AUTO) private Long id; + @Column(unique=true) + //@LoginOccupiedConstraint private final String username; private String password; private String displayname; @@ -63,7 +66,7 @@ public class User implements UserDetails{ return true; } - public void setRole(UserRole role){ + public void setRole(UserRole role){ // TODO this.authorities.add(role); } } diff --git a/src/main/java/ru/redrise/marinesco/UserGenerified.java b/src/main/java/ru/redrise/marinesco/UserGenerified.java new file mode 100644 index 0000000..2f70581 --- /dev/null +++ b/src/main/java/ru/redrise/marinesco/UserGenerified.java @@ -0,0 +1,27 @@ +package ru.redrise.marinesco; + +import java.util.List; + +import lombok.Data; +import lombok.NoArgsConstructor; +import ru.redrise.marinesco.security.UserRole; + +@Data +@NoArgsConstructor +public class UserGenerified { + + private static final long serialVersionUID = 1L; + + private Long id; + + private String name; + private String displayName; + private List role; + + public UserGenerified(User user){ + this.id = user.getId(); + this.name = user.getUsername(); + this.displayName = user.getDisplayname(); + this.role = user.getAuthorities(); + } +} \ No newline at end of file diff --git a/src/main/java/ru/redrise/marinesco/Validators/LoginOccupiedConstraint.java b/src/main/java/ru/redrise/marinesco/Validators/LoginOccupiedConstraint.java new file mode 100644 index 0000000..e06157b --- /dev/null +++ b/src/main/java/ru/redrise/marinesco/Validators/LoginOccupiedConstraint.java @@ -0,0 +1,20 @@ +package ru.redrise.marinesco.Validators; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import jakarta.validation.Constraint; +import jakarta.validation.Payload; + +@Documented +@Constraint(validatedBy = LoginOccupiedValidator.class) +@Target({ElementType.METHOD, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface LoginOccupiedConstraint { + String message() default "Login already taken. Please use anohter one."; + Class[] groups() default {}; + Class[] payload() default {}; +} diff --git a/src/main/java/ru/redrise/marinesco/Validators/LoginOccupiedValidator.java b/src/main/java/ru/redrise/marinesco/Validators/LoginOccupiedValidator.java new file mode 100644 index 0000000..547dbf9 --- /dev/null +++ b/src/main/java/ru/redrise/marinesco/Validators/LoginOccupiedValidator.java @@ -0,0 +1,22 @@ +package ru.redrise.marinesco.Validators; + + +import org.springframework.beans.factory.annotation.Autowired; + +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import ru.redrise.marinesco.data.UserRepository; + +public class LoginOccupiedValidator implements ConstraintValidator{ + + @Autowired + private UserRepository userRepo; + + @Override + public void initialize(LoginOccupiedConstraint constraintAnnotation) {} + + @Override + public boolean isValid(String login, ConstraintValidatorContext context) { + return userRepo.findByUsername(login) == null; + } +} diff --git a/src/main/java/ru/redrise/marinesco/security/ManageUsersController.java b/src/main/java/ru/redrise/marinesco/security/ManageUsersController.java new file mode 100644 index 0000000..474e994 --- /dev/null +++ b/src/main/java/ru/redrise/marinesco/security/ManageUsersController.java @@ -0,0 +1,61 @@ +package ru.redrise.marinesco.security; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +import lombok.extern.slf4j.Slf4j; +import ru.redrise.marinesco.User; +import ru.redrise.marinesco.UserGenerified; +import ru.redrise.marinesco.data.UserRepository; + + +//TODO +@Slf4j +@Controller +@RequestMapping("/manage_users") +@PreAuthorize("hasRole('ADMIN')") +public class ManageUsersController { + + private UserRepository userRepository; + + public ManageUsersController(UserRepository userRepository){ + this.userRepository = userRepository; + } + + @ModelAttribute(name = "userGenerified") + public UserGenerified taco() { + return new UserGenerified(); + } + + @ModelAttribute + public void addUsers(Model model){ + Iterable users = userRepository.findAll(); + List usersGen = new ArrayList<>(); + for (User user : users){ + usersGen.add(new UserGenerified(user)); // TODO: ADD ARRAY INSTEAD OF ONE! + } + model.addAttribute("USR", usersGen); // TODO: ADD ARRAY INSTEAD OF ONE! + } + + @GetMapping + public String getPage() { + return "manage_users"; + } + + @PostMapping("/delete") + public String processDelete(UserGenerified userGenerified){ + log.info(userGenerified.toString()); + + userRepository.deleteById(userGenerified.getId()); + + return "redirect:/manage_users"; + } +} diff --git a/src/main/java/ru/redrise/marinesco/security/RegistrationController.java b/src/main/java/ru/redrise/marinesco/security/RegistrationController.java index 5a27943..9bd2899 100644 --- a/src/main/java/ru/redrise/marinesco/security/RegistrationController.java +++ b/src/main/java/ru/redrise/marinesco/security/RegistrationController.java @@ -10,9 +10,12 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import jakarta.validation.Valid; +import lombok.extern.slf4j.Slf4j; import ru.redrise.marinesco.data.RolesRepository; import ru.redrise.marinesco.data.UserRepository; +import ru.redrise.marinesco.User; +@Slf4j @Controller @RequestMapping("/register") public class RegistrationController { @@ -46,7 +49,8 @@ public class RegistrationController { return "registration"; } - userRepo.save(registerForm.toUser(passwordEncoder, rolesRepo)); + User user = userRepo.save(registerForm.toUser(passwordEncoder, rolesRepo)); + log.info("Added user {} {} {}", user.getId(), user.getUsername(), user.getDisplayname()); return "redirect:/login"; } } diff --git a/src/main/java/ru/redrise/marinesco/security/RegistrationForm.java b/src/main/java/ru/redrise/marinesco/security/RegistrationForm.java index 4878494..bfc6b8f 100644 --- a/src/main/java/ru/redrise/marinesco/security/RegistrationForm.java +++ b/src/main/java/ru/redrise/marinesco/security/RegistrationForm.java @@ -11,7 +11,7 @@ import ru.redrise.marinesco.data.RolesRepository; @Data public class RegistrationForm { - + @NotNull @Size(min=3, max=32, message="Username must be at least 3 characters long. Should not exceed 32 characters.") private String username; diff --git a/src/main/java/ru/redrise/marinesco/security/SecurityConfig.java b/src/main/java/ru/redrise/marinesco/security/SecurityConfig.java index b45111a..1ecba52 100644 --- a/src/main/java/ru/redrise/marinesco/security/SecurityConfig.java +++ b/src/main/java/ru/redrise/marinesco/security/SecurityConfig.java @@ -52,11 +52,11 @@ public class SecurityConfig { .requestMatchers(mvc.pattern("/register")).permitAll() .requestMatchers(mvc.pattern("/login")).permitAll() .requestMatchers(mvc.pattern("/error")).permitAll() + .requestMatchers(PathRequest.toH2Console()).permitAll() .requestMatchers(mvc.pattern("/")).hasAnyRole("ADMIN", "USER") .requestMatchers(mvc.pattern("/profile/**")).hasAnyRole("ADMIN", "USER") - .requestMatchers(PathRequest.toH2Console()).permitAll() //.requestMatchers(mvc.pattern("/design/**")).hasRole("USER") - .anyRequest().denyAll()) + .anyRequest().authenticated()) //.anyRequest().permitAll()) .formLogin(formLoginConfigurer -> formLoginConfigurer .loginPage("/login") diff --git a/src/main/java/ru/redrise/marinesco/security/UserSettingsController.java b/src/main/java/ru/redrise/marinesco/security/UserSettingsController.java index cb03a4d..b0065f4 100644 --- a/src/main/java/ru/redrise/marinesco/security/UserSettingsController.java +++ b/src/main/java/ru/redrise/marinesco/security/UserSettingsController.java @@ -48,18 +48,16 @@ public class UserSettingsController { return "redirect:/profile/settings"; } - @GetMapping("/settings") public String getSettingsFirstPage(){ return "user_settings"; } - @PostMapping("/settings") public String getSettingsPage(@Valid UserSettingsForm userSettingsForm, - Errors errors, - @AuthenticationPrincipal User user, - Model model){ + Errors errors, + @AuthenticationPrincipal User user, + Model model){ if (errors.hasErrors()) return "user_settings"; if (! user.getDisplayname().equals(userSettingsForm.getDisplayname())) diff --git a/src/main/java/ru/redrise/marinesco/web/HandyErrorController.java b/src/main/java/ru/redrise/marinesco/web/HandyErrorController.java index ea7c21f..ef732ee 100644 --- a/src/main/java/ru/redrise/marinesco/web/HandyErrorController.java +++ b/src/main/java/ru/redrise/marinesco/web/HandyErrorController.java @@ -8,10 +8,12 @@ import org.springframework.web.bind.annotation.RequestMapping; import jakarta.servlet.RequestDispatcher; import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +@Slf4j @Controller public class HandyErrorController implements ErrorController{ - + @ModelAttribute(name = "code") public String addMisc(HttpServletRequest request){ return request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE).toString(); diff --git a/src/main/java/ru/redrise/marinesco/web/RoleByIdConverter.java b/src/main/java/ru/redrise/marinesco/web/RoleByIdConverter.java new file mode 100644 index 0000000..ad05d56 --- /dev/null +++ b/src/main/java/ru/redrise/marinesco/web/RoleByIdConverter.java @@ -0,0 +1,23 @@ +package ru.redrise.marinesco.web; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.stereotype.Component; + +import ru.redrise.marinesco.data.RolesRepository; +import ru.redrise.marinesco.security.UserRole; + +@Component +public class RoleByIdConverter implements Converter{ + + private RolesRepository rolesRepository; + + public RoleByIdConverter(RolesRepository rolesRepository){ + this.rolesRepository = rolesRepository; + } + + @Override + public UserRole convert(Long id) { + return rolesRepository.findById(id).orElse(null); + } + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f9babf2..15da86f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,9 +1,8 @@ - spring: thymeleaf: cache: false datasource: - driverClassName: org.h2.Driver + driver-class-name: org.h2.Driver generate-unique-name: false name: marinesco url: jdbc:h2:mem:marinesco diff --git a/src/main/resources/static/images/logo.svg b/src/main/resources/static/images/logo.svg index 7b90bd4..f6a4bcd 100644 --- a/src/main/resources/static/images/logo.svg +++ b/src/main/resources/static/images/logo.svg @@ -2,12 +2,12 @@ + ry="1.226432" /> - - - - - - - + style="color:#000000;fill:#15bade;-inkscape-stroke:none" + d="M 27.730628,1.5763661 C 25.036825,1.5596634 22.673126,2.2437308 20.494299,3.0548817 15.52534,4.9047658 11.509661,7.3067267 4.20719,6.2443347 l -0.2382811,-0.03516 -0.2402344,0.01563 c 0,0 -0.571584,0.03161 -1.09375,0.355469 -0.522166,0.323861 -1.0854728,1.181111 -1.1308594,1.90039 -0.031147,0.493776 0.1131821,1.217964 0.5546875,1.7480473 0.4415054,0.530084 1.3378907,0.798828 1.3378907,0.798828 l 0.056641,0.01172 0.056641,0.0078 C 12.15843,12.305276 17.748772,9.2542897 22.18766,7.6017457 c 2.225096,-0.828377 4.129469,-1.332733 6.328125,-1.154297 2.198656,0.178436 4.822312,1.053458 8.382812,3.423828 l 1.878907,1.2500003 0.002,-0.0039 0.002,0.002 1.875,-1.2500003 c 3.560451,-2.370338 6.184021,-3.243543 8.382812,-3.421875 2.198792,-0.178332 4.10494,0.325904 6.330078,1.154297 4.439025,1.652595 10.029323,4.7035173 18.677735,3.4453133 l 0.05078,-0.0078 0.04883,-0.0098 c 0,0 0.902708,-0.265223 1.347656,-0.796875 0.444949,-0.5316513 0.589732,-1.2583183 0.558594,-1.7519533 -0.04553,-0.721911 -0.612608,-1.579806 -1.134765,-1.902343 -0.522158,-0.322538 -1.091797,-0.353516 -1.091797,-0.353516 l -0.240235,-0.01563 -0.238281,0.03516 C 66.045442,7.3067127 62.031754,4.904815 57.062659,3.0548817 54.883831,2.2437293 52.518178,1.5596634 49.824378,1.5763661 c -0.385437,0.00239 -0.775891,0.018721 -1.171875,0.050781 -2.945866,0.2385075 -6.201654,1.4666842 -9.875,3.7734376 -3.672889,-2.3061606 -6.92781,-3.5330278 -9.873047,-3.7714845 -0.396002,-0.032061 -0.788428,-0.050345 -1.173828,-0.052734 z" + id="path1154" /> + style="opacity:1;fill:#00b185;fill-opacity:1;stroke:#ffffff;stroke-width:0.818;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1;paint-order:stroke fill markers" + d="M 40.69873,4.1910257 47.085,1.9080267 c 0.0479,-0.017123 0.08973,0.04381 0.08973,0.09821 v 8.4513743 c 0,0.0544 -0.04003,0.09821 -0.08973,0.09821 0,0 -3.24318,-2.0452513 -3.24318,-3.8408703 0,-0.06402 0,-0.06402 0,0 0,1.794641 -3.14309,3.8409423 -3.14309,3.8409423 -0.03835,0.03461 -0.08973,-0.04381 -0.08973,-0.09821 V 4.2892377 c 0,-0.05441 0.08973,-0.097032 0.08973,-0.09821 z" + sodipodi:nodetypes="sssssssssscs" /> diff --git a/src/main/resources/templates/manage_users.html b/src/main/resources/templates/manage_users.html new file mode 100644 index 0000000..f63e49b --- /dev/null +++ b/src/main/resources/templates/manage_users.html @@ -0,0 +1,47 @@ + + + + + Marinesco - Manage users + + + + + +

Manage users

+
go back +

+

+ user + + user + +
+ + + + + + + + +
+
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/registration.html b/src/main/resources/templates/registration.html index 4b7aa14..1da765e 100644 --- a/src/main/resources/templates/registration.html +++ b/src/main/resources/templates/registration.html @@ -5,7 +5,7 @@ Marinesco - registration form - + diff --git a/src/main/resources/templates/root.html b/src/main/resources/templates/root.html index df62e2f..3346843 100644 --- a/src/main/resources/templates/root.html +++ b/src/main/resources/templates/root.html @@ -11,6 +11,10 @@

Welcome to Marinesco


Login +
/profile +
/manage_users +
+
Log out \ No newline at end of file diff --git a/target/classes/application.yml b/target/classes/application.yml index f9babf2..15da86f 100644 --- a/target/classes/application.yml +++ b/target/classes/application.yml @@ -1,9 +1,8 @@ - spring: thymeleaf: cache: false datasource: - driverClassName: org.h2.Driver + driver-class-name: org.h2.Driver generate-unique-name: false name: marinesco url: jdbc:h2:mem:marinesco diff --git a/target/classes/ru/redrise/marinesco/User.class b/target/classes/ru/redrise/marinesco/User.class index ba7444b..b935bd4 100644 Binary files a/target/classes/ru/redrise/marinesco/User.class and b/target/classes/ru/redrise/marinesco/User.class differ diff --git a/target/classes/ru/redrise/marinesco/UserGenerified.class b/target/classes/ru/redrise/marinesco/UserGenerified.class new file mode 100644 index 0000000..c6a77fd Binary files /dev/null and b/target/classes/ru/redrise/marinesco/UserGenerified.class differ diff --git a/target/classes/ru/redrise/marinesco/Validators/LoginOccupiedConstraint.class b/target/classes/ru/redrise/marinesco/Validators/LoginOccupiedConstraint.class new file mode 100644 index 0000000..ebaf188 Binary files /dev/null and b/target/classes/ru/redrise/marinesco/Validators/LoginOccupiedConstraint.class differ diff --git a/target/classes/ru/redrise/marinesco/Validators/LoginOccupiedValidator.class b/target/classes/ru/redrise/marinesco/Validators/LoginOccupiedValidator.class new file mode 100644 index 0000000..9e744d0 Binary files /dev/null and b/target/classes/ru/redrise/marinesco/Validators/LoginOccupiedValidator.class differ diff --git a/target/classes/ru/redrise/marinesco/security/ManageUsersController.class b/target/classes/ru/redrise/marinesco/security/ManageUsersController.class new file mode 100644 index 0000000..7593a7a Binary files /dev/null and b/target/classes/ru/redrise/marinesco/security/ManageUsersController.class differ diff --git a/target/classes/ru/redrise/marinesco/security/RegistrationController.class b/target/classes/ru/redrise/marinesco/security/RegistrationController.class index 4961882..80b2c75 100644 Binary files a/target/classes/ru/redrise/marinesco/security/RegistrationController.class and b/target/classes/ru/redrise/marinesco/security/RegistrationController.class differ diff --git a/target/classes/ru/redrise/marinesco/security/SecurityConfig.class b/target/classes/ru/redrise/marinesco/security/SecurityConfig.class index f7a0cd9..c7269f9 100644 Binary files a/target/classes/ru/redrise/marinesco/security/SecurityConfig.class and b/target/classes/ru/redrise/marinesco/security/SecurityConfig.class differ diff --git a/target/classes/ru/redrise/marinesco/security/UserSettingsController.class b/target/classes/ru/redrise/marinesco/security/UserSettingsController.class index 9424704..dba5b20 100644 Binary files a/target/classes/ru/redrise/marinesco/security/UserSettingsController.class and b/target/classes/ru/redrise/marinesco/security/UserSettingsController.class differ diff --git a/target/classes/ru/redrise/marinesco/web/HandyErrorController.class b/target/classes/ru/redrise/marinesco/web/HandyErrorController.class index c1e31b8..28fb56d 100644 Binary files a/target/classes/ru/redrise/marinesco/web/HandyErrorController.class and b/target/classes/ru/redrise/marinesco/web/HandyErrorController.class differ diff --git a/target/classes/ru/redrise/marinesco/web/RoleByIdConverter.class b/target/classes/ru/redrise/marinesco/web/RoleByIdConverter.class new file mode 100644 index 0000000..02064ce Binary files /dev/null and b/target/classes/ru/redrise/marinesco/web/RoleByIdConverter.class differ diff --git a/target/classes/static/images/logo.svg b/target/classes/static/images/logo.svg index 7b90bd4..f6a4bcd 100644 --- a/target/classes/static/images/logo.svg +++ b/target/classes/static/images/logo.svg @@ -2,12 +2,12 @@ + ry="1.226432" /> - - - - - - - + style="color:#000000;fill:#15bade;-inkscape-stroke:none" + d="M 27.730628,1.5763661 C 25.036825,1.5596634 22.673126,2.2437308 20.494299,3.0548817 15.52534,4.9047658 11.509661,7.3067267 4.20719,6.2443347 l -0.2382811,-0.03516 -0.2402344,0.01563 c 0,0 -0.571584,0.03161 -1.09375,0.355469 -0.522166,0.323861 -1.0854728,1.181111 -1.1308594,1.90039 -0.031147,0.493776 0.1131821,1.217964 0.5546875,1.7480473 0.4415054,0.530084 1.3378907,0.798828 1.3378907,0.798828 l 0.056641,0.01172 0.056641,0.0078 C 12.15843,12.305276 17.748772,9.2542897 22.18766,7.6017457 c 2.225096,-0.828377 4.129469,-1.332733 6.328125,-1.154297 2.198656,0.178436 4.822312,1.053458 8.382812,3.423828 l 1.878907,1.2500003 0.002,-0.0039 0.002,0.002 1.875,-1.2500003 c 3.560451,-2.370338 6.184021,-3.243543 8.382812,-3.421875 2.198792,-0.178332 4.10494,0.325904 6.330078,1.154297 4.439025,1.652595 10.029323,4.7035173 18.677735,3.4453133 l 0.05078,-0.0078 0.04883,-0.0098 c 0,0 0.902708,-0.265223 1.347656,-0.796875 0.444949,-0.5316513 0.589732,-1.2583183 0.558594,-1.7519533 -0.04553,-0.721911 -0.612608,-1.579806 -1.134765,-1.902343 -0.522158,-0.322538 -1.091797,-0.353516 -1.091797,-0.353516 l -0.240235,-0.01563 -0.238281,0.03516 C 66.045442,7.3067127 62.031754,4.904815 57.062659,3.0548817 54.883831,2.2437293 52.518178,1.5596634 49.824378,1.5763661 c -0.385437,0.00239 -0.775891,0.018721 -1.171875,0.050781 -2.945866,0.2385075 -6.201654,1.4666842 -9.875,3.7734376 -3.672889,-2.3061606 -6.92781,-3.5330278 -9.873047,-3.7714845 -0.396002,-0.032061 -0.788428,-0.050345 -1.173828,-0.052734 z" + id="path1154" /> + style="opacity:1;fill:#00b185;fill-opacity:1;stroke:#ffffff;stroke-width:0.818;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1;paint-order:stroke fill markers" + d="M 40.69873,4.1910257 47.085,1.9080267 c 0.0479,-0.017123 0.08973,0.04381 0.08973,0.09821 v 8.4513743 c 0,0.0544 -0.04003,0.09821 -0.08973,0.09821 0,0 -3.24318,-2.0452513 -3.24318,-3.8408703 0,-0.06402 0,-0.06402 0,0 0,1.794641 -3.14309,3.8409423 -3.14309,3.8409423 -0.03835,0.03461 -0.08973,-0.04381 -0.08973,-0.09821 V 4.2892377 c 0,-0.05441 0.08973,-0.097032 0.08973,-0.09821 z" + sodipodi:nodetypes="sssssssssscs" /> diff --git a/target/classes/templates/manage_users.html b/target/classes/templates/manage_users.html new file mode 100644 index 0000000..f63e49b --- /dev/null +++ b/target/classes/templates/manage_users.html @@ -0,0 +1,47 @@ + + + + + Marinesco - Manage users + + + + + +

Manage users

+
go back +

+

+ user + + user + +
+ + + + + + + + +
+
+
+ + + \ No newline at end of file diff --git a/target/classes/templates/registration.html b/target/classes/templates/registration.html index 4b7aa14..1da765e 100644 --- a/target/classes/templates/registration.html +++ b/target/classes/templates/registration.html @@ -5,7 +5,7 @@ Marinesco - registration form - + diff --git a/target/classes/templates/root.html b/target/classes/templates/root.html index df62e2f..3346843 100644 --- a/target/classes/templates/root.html +++ b/target/classes/templates/root.html @@ -11,6 +11,10 @@

Welcome to Marinesco


Login +
/profile +
/manage_users +
+
Log out \ No newline at end of file