简体   繁体   English

jhipster,Java端的管理员角色标识

[英]jhipster, admin role identification on Java side

recently I started working on jhipster angular application (employee-targets app). 最近,我开始研究jhipster角度应用程序(employee-targets应用程序)。 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") EmployeeTargetsResource.java获取@GetMapping("/emp-targets")

I have changed 我变了

empTargetsRepository.findAll() to empTargetsRepository.findByUserIsCurrentUser() empTargetsRepository.findAll()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() 如果登录的用户是admin ,那么我想拥有empTargetsRepository.findAll()

otherwise empTargetsRepository.findByUserIsCurrentUser() 否则为empTargetsRepository.findByUserIsCurrentUser()

Is there is any global variable available to check whether the current logged in user is admin in Java code? 是否有任何全局变量可用于检查当前登录用户是否为Java代码中的admin?

For angular code I could able to get using *jhiHasAnyAuthority="'ROLE_ADMIN'" for html. 对于角度代码,我可以使用*jhiHasAnyAuthority="'ROLE_ADMIN'" for html。 Is there any similar option available in Java code side. Java代码端是否有任何类似的选项可用。

You can use SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN) which will return a boolean if the user has the ROLE_ADMIN authority. 您可以使用SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN) ,如果用户具有ROLE_ADMIN权限,则它将返回一个布尔值。

So your code would look similar to below: 因此,您的代码将类似于以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM