feat: user roles improvements
This commit is contained in:
parent
0b862f52fb
commit
32cbfd6079
3 changed files with 64 additions and 8 deletions
20
src/main/java/com/application/munera/data/Role.java
Normal file
20
src/main/java/com/application/munera/data/Role.java
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package com.application.munera.data;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public enum Role {
|
||||||
|
ROLE_ADMIN("ROLE_ADMIN"),
|
||||||
|
ROLE_USER("ROLE_USER");
|
||||||
|
|
||||||
|
private final String roleName;
|
||||||
|
|
||||||
|
Role(String roleName) {
|
||||||
|
this.roleName = roleName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return roleName;
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,11 @@ import jakarta.validation.constraints.Email;
|
||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Data
|
@Data
|
||||||
@Table(name = "users")
|
@Table(name = "users")
|
||||||
|
@ -38,4 +43,18 @@ public class User {
|
||||||
@Size(max = 100)
|
@Size(max = 100)
|
||||||
@Column(name = "email")
|
@Column(name = "email")
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
|
// Helper methods to handle roles as a list of enum values
|
||||||
|
public List<Role> getRoleList() {
|
||||||
|
if (roles == null || roles.isEmpty()) return new ArrayList<>(); // Return an empty list if roles are null or empty
|
||||||
|
return Arrays.stream(roles.split(","))
|
||||||
|
.map(Role::valueOf)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoleList(List<Role> roleList) {
|
||||||
|
this.roles = roleList.stream()
|
||||||
|
.map(Role::name)
|
||||||
|
.collect(Collectors.joining(","));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package com.application.munera.views.users;
|
package com.application.munera.views.users;
|
||||||
|
|
||||||
|
import com.application.munera.data.Role;
|
||||||
import com.application.munera.data.User;
|
import com.application.munera.data.User;
|
||||||
import com.application.munera.services.UserService;
|
import com.application.munera.services.UserService;
|
||||||
import com.application.munera.views.MainLayout;
|
import com.application.munera.views.MainLayout;
|
||||||
import com.vaadin.flow.component.UI;
|
import com.vaadin.flow.component.UI;
|
||||||
import com.vaadin.flow.component.button.Button;
|
import com.vaadin.flow.component.button.Button;
|
||||||
import com.vaadin.flow.component.button.ButtonVariant;
|
import com.vaadin.flow.component.button.ButtonVariant;
|
||||||
|
import com.vaadin.flow.component.combobox.MultiSelectComboBox;
|
||||||
import com.vaadin.flow.component.dependency.Uses;
|
import com.vaadin.flow.component.dependency.Uses;
|
||||||
import com.vaadin.flow.component.formlayout.FormLayout;
|
import com.vaadin.flow.component.formlayout.FormLayout;
|
||||||
import com.vaadin.flow.component.grid.Grid;
|
import com.vaadin.flow.component.grid.Grid;
|
||||||
|
@ -21,6 +23,7 @@ import com.vaadin.flow.component.textfield.PasswordField;
|
||||||
import com.vaadin.flow.component.textfield.TextField;
|
import com.vaadin.flow.component.textfield.TextField;
|
||||||
import com.vaadin.flow.data.binder.BeanValidationBinder;
|
import com.vaadin.flow.data.binder.BeanValidationBinder;
|
||||||
import com.vaadin.flow.data.binder.ValidationException;
|
import com.vaadin.flow.data.binder.ValidationException;
|
||||||
|
import com.vaadin.flow.data.validator.EmailValidator;
|
||||||
import com.vaadin.flow.router.BeforeEnterEvent;
|
import com.vaadin.flow.router.BeforeEnterEvent;
|
||||||
import com.vaadin.flow.router.BeforeEnterObserver;
|
import com.vaadin.flow.router.BeforeEnterObserver;
|
||||||
import com.vaadin.flow.router.PageTitle;
|
import com.vaadin.flow.router.PageTitle;
|
||||||
|
@ -28,6 +31,8 @@ import com.vaadin.flow.router.Route;
|
||||||
import jakarta.annotation.security.RolesAllowed;
|
import jakarta.annotation.security.RolesAllowed;
|
||||||
import org.springframework.orm.ObjectOptimisticLockingFailureException;
|
import org.springframework.orm.ObjectOptimisticLockingFailureException;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
|
||||||
|
@ -53,9 +58,9 @@ public class UsersView extends Div implements BeforeEnterObserver {
|
||||||
private TextField firstName;
|
private TextField firstName;
|
||||||
private TextField lastName;
|
private TextField lastName;
|
||||||
private TextField username;
|
private TextField username;
|
||||||
private TextField roles;
|
|
||||||
private PasswordField password;
|
private PasswordField password;
|
||||||
private EmailField email;
|
private EmailField email;
|
||||||
|
private MultiSelectComboBox<Role> roles; // Updated to MultiSelectComboBox
|
||||||
|
|
||||||
public UsersView(UserService userService) {
|
public UsersView(UserService userService) {
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
|
@ -88,10 +93,8 @@ public class UsersView extends Div implements BeforeEnterObserver {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Configure Form
|
|
||||||
binder = new BeanValidationBinder<>(User.class);
|
binder = new BeanValidationBinder<>(User.class);
|
||||||
// Bind fields. This is where you'd define e.g. validation rules
|
// Bind fields with validation rules
|
||||||
binder.bindInstanceFields(this);
|
|
||||||
binder.forField(firstName)
|
binder.forField(firstName)
|
||||||
.asRequired("First name is required")
|
.asRequired("First name is required")
|
||||||
.bind(User::getFirstName, User::setFirstName);
|
.bind(User::getFirstName, User::setFirstName);
|
||||||
|
@ -104,11 +107,25 @@ public class UsersView extends Div implements BeforeEnterObserver {
|
||||||
.asRequired("Username is required")
|
.asRequired("Username is required")
|
||||||
.bind(User::getUsername, User::setUsername);
|
.bind(User::getUsername, User::setUsername);
|
||||||
|
|
||||||
|
binder.forField(password)
|
||||||
|
.asRequired("Password is required")
|
||||||
|
.bind(User::getPassword, User::setPassword);
|
||||||
|
|
||||||
|
binder.forField(email)
|
||||||
|
.withValidator(
|
||||||
|
new EmailValidator("Please enter a valid email address"))
|
||||||
|
.bind(User::getEmail, User::setEmail);
|
||||||
|
|
||||||
|
binder.forField(roles)
|
||||||
|
.bind(user1 -> new HashSet<>(user.getRoleList()), // Getter to convert roles string to list
|
||||||
|
(user1, roles) -> user.setRoleList(new ArrayList<>(roles))); // Setter to convert list back to string
|
||||||
|
|
||||||
|
|
||||||
|
// Button listeners
|
||||||
cancel.addClickListener(e -> {
|
cancel.addClickListener(e -> {
|
||||||
clearForm();
|
clearForm();
|
||||||
refreshGrid();
|
refreshGrid();
|
||||||
});
|
});
|
||||||
|
|
||||||
save.addClickListener(e -> {
|
save.addClickListener(e -> {
|
||||||
try {
|
try {
|
||||||
if (this.user == null) this.user = new User();
|
if (this.user == null) this.user = new User();
|
||||||
|
@ -127,7 +144,6 @@ public class UsersView extends Div implements BeforeEnterObserver {
|
||||||
Notification.show("Failed to update the user. Check again that all values are valid");
|
Notification.show("Failed to update the user. Check again that all values are valid");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
delete.addClickListener(e -> {
|
delete.addClickListener(e -> {
|
||||||
try {
|
try {
|
||||||
if (this.user == null) throw new IllegalStateException("The user is null!");
|
if (this.user == null) throw new IllegalStateException("The user is null!");
|
||||||
|
@ -174,8 +190,10 @@ public class UsersView extends Div implements BeforeEnterObserver {
|
||||||
lastName = new TextField("Last Name");
|
lastName = new TextField("Last Name");
|
||||||
username = new TextField("Username");
|
username = new TextField("Username");
|
||||||
password = new PasswordField("Password");
|
password = new PasswordField("Password");
|
||||||
roles = new TextField("Roles");
|
|
||||||
email = new EmailField("Email");
|
email = new EmailField("Email");
|
||||||
|
roles = new MultiSelectComboBox<>("Roles");
|
||||||
|
roles.setItems(Role.values()); // Set the enum values as options
|
||||||
|
roles.setItemLabelGenerator(Role::getRoleName); // Define how to display roles
|
||||||
|
|
||||||
// We set the maximum parallel columns to 1
|
// We set the maximum parallel columns to 1
|
||||||
formLayout.setResponsiveSteps(new FormLayout.ResponsiveStep("0", 1));
|
formLayout.setResponsiveSteps(new FormLayout.ResponsiveStep("0", 1));
|
||||||
|
@ -216,6 +234,5 @@ public class UsersView extends Div implements BeforeEnterObserver {
|
||||||
private void populateForm(User value) {
|
private void populateForm(User value) {
|
||||||
this.user = value;
|
this.user = value;
|
||||||
binder.readBean(this.user);
|
binder.readBean(this.user);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue