Madasu K
Madasu K

Reputation: 1863

jhipster, admin role identification on Java side

recently I started working on jhipster angular application (employee-targets app). By default I am getting entries for all users, so to restrict it to current user, I have modified the code to :

In EmployeeTargetsResource.java for @GetMapping("/emp-targets")

I have changed

empTargetsRepository.findAll() to empTargetsRepository.findByUserIsCurrentUser()

But now I want to change this based on role:

if the logged in user is admin, I want to have empTargetsRepository.findAll()

otherwise empTargetsRepository.findByUserIsCurrentUser()

Is there is any global variable available to check whether the current logged in user is admin in Java code?

For angular code I could able to get using *jhiHasAnyAuthority="'ROLE_ADMIN'" for html. Is there any similar option available in Java code side.

Upvotes: 0

Views: 425

Answers (1)

Jon Ruddell
Jon Ruddell

Reputation: 6352

You can use SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN) which will return a boolean if the user has the ROLE_ADMIN authority.

So your code would look similar to below:

if (SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)) {
    empTargetsRepository.findAll()
} else {
    empTargetsRepository.findByUserIsCurrentUser()
}

Upvotes: 3

Related Questions