I have 2 buttons which I would like to show depending on whether a user is admin or not.
<tbody id="table-container">
<tr th:each="user : ${allUsers}">
<th scope="row" th:text="*{user.id}">1</th>
<td th:text="*{user.firstName}">x</td>
<td>
<th:block th:if="${!#sets.contains(user.getRoles(), 'ADMIN')}">
<form class="form-inline"
th:action="@{/admin/manage-users/add-admin(email=*{user.email})}"
th:method="PATCH">
<button class="btn btn-sm" type="submit" data-toggle="tooltip"
data-placement="top" title="Make Admin"><i
class="bx bxs-file-plus bx-md" style="color: #5ca3b4" __^<="" i="">
</button>
</form>
</th:block>
<th:block th:if="${#sets.contains(user.getRoles(), 'ADMIN')}">
<form th:action="@{/admin/manage-users/remove-admin(email=*{user.email})}"
th:method="PATCH">
<button class="btn btn-sm" type="submit" data-toggle="tooltip"
data-placement="top" title="Remove Admin Role">
^__i class='bx bxs-folder-minus bx-md'
style="color: #dc3545">
</button>
</form>
</th:block>
</td>
</tr>
However #sets.contains does not work, what I get as an output is always Make Admin button, the Remove Admin button does not show at all.
The Controller:
@GetMapping("/admin/manage-users")
public String allUsers(Model model) {
List <AllUsersServiceModel> allUsersServiceModels = this.userService.findAll();
List <AllUsersViewModel> allUsersViewModels = allUsersServiceModels
.stream()
.map(a -> this.mapper.map(a, AllUsersViewModel.class))
.collect(Collectors.toList());
model.addAttribute("allUsers", allUsersViewModels);
return "manage-users";
}
UserRoleEntity:
@Entity
@Table(name = "roles")
public class UserRoleEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private UserRoleEnum role;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserRoleEntity that = (UserRoleEntity) o;
return this.role == that.role;
}
@Override
public int hashCode() {
return Objects.hash(this.role);
}
UserEntity:
@Entity
@Table(name = "users")
public class UserEntity extends BaseEntity {
@ManyToMany(fetch = FetchType.EAGER)
private Set<UserRoleEntity> roles = new HashSet<>();
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserEntity user = (UserEntity) o;
return Objects.equals(roles, user.roles);
}
@Override
public int hashCode() {
return roles.hashCode();
}
What I have tried:
I implemented equals and hashcode in my UrseRoleEntity and UserEntity but the result is the same.