Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 buttons which I would like to show depending on whether a user is admin or not.

HTML
<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" __^&lt;="" 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:
Java
@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:
Java
@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:
Java
@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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900