0

How to append a class to an element with th:classappend only if the user has a specific role?

Something like this:

<div th:classappend="${hasRole('EDITOR')?'glow':''}"></div>

I'm using Spring Boot, Spring Security and Thymeleaf.

Mahozad
  • 18,032
  • 13
  • 118
  • 133

2 Answers2

1

I found the solution on Thymeleaf "extras" Spring Security GitHub page:

<div th:classappend="${#authorization.expression('hasRole(''EDITOR'')')?'glow':''}"></div>
Mahozad
  • 18,032
  • 13
  • 118
  • 133
1

You can use the request variable, which points to the current HttpServletRequest:

<div th:classappend="${#request.isUserInRole('EDITOR')?'glow':''}"></div>
holmis83
  • 15,922
  • 5
  • 82
  • 83