Commit a80d1174 by 张乐 Committed by GitHub

Merge pull request #264 from nobodyiam/query-role

expose find role by role name
parents 59ad9e90 0c2a628c
...@@ -49,7 +49,7 @@ public class RolePermissionService { ...@@ -49,7 +49,7 @@ public class RolePermissionService {
*/ */
@Transactional @Transactional
public Role createRoleWithPermissions(Role role, Set<Long> permissionIds) { public Role createRoleWithPermissions(Role role, Set<Long> permissionIds) {
Role current = roleRepository.findTopByRoleName(role.getRoleName()); Role current = findRoleByRoleName(role.getRoleName());
Preconditions.checkState(current == null, "Role %s already exists!", role.getRoleName()); Preconditions.checkState(current == null, "Role %s already exists!", role.getRoleName());
Role createdRole = roleRepository.save(role); Role createdRole = roleRepository.save(role);
...@@ -75,7 +75,7 @@ public class RolePermissionService { ...@@ -75,7 +75,7 @@ public class RolePermissionService {
*/ */
@Transactional @Transactional
public void assignRoleToUsers(String roleName, Set<String> userIds, String operatorUserId) { public void assignRoleToUsers(String roleName, Set<String> userIds, String operatorUserId) {
Role role = roleRepository.findTopByRoleName(roleName); Role role = findRoleByRoleName(roleName);
Preconditions.checkState(role != null, "Role %s doesn't exist!", roleName); Preconditions.checkState(role != null, "Role %s doesn't exist!", roleName);
List<UserRole> existedUserRoles = List<UserRole> existedUserRoles =
...@@ -102,7 +102,7 @@ public class RolePermissionService { ...@@ -102,7 +102,7 @@ public class RolePermissionService {
*/ */
@Transactional @Transactional
public void removeRoleFromUsers(String roleName, Set<String> userIds, String operatorUserId) { public void removeRoleFromUsers(String roleName, Set<String> userIds, String operatorUserId) {
Role role = roleRepository.findTopByRoleName(roleName); Role role = findRoleByRoleName(roleName);
Preconditions.checkState(role != null, "Role %s doesn't exist!", roleName); Preconditions.checkState(role != null, "Role %s doesn't exist!", roleName);
List<UserRole> existedUserRoles = List<UserRole> existedUserRoles =
...@@ -121,7 +121,7 @@ public class RolePermissionService { ...@@ -121,7 +121,7 @@ public class RolePermissionService {
* Query users with role * Query users with role
*/ */
public Set<UserInfo> queryUsersWithRole(String roleName) { public Set<UserInfo> queryUsersWithRole(String roleName) {
Role role = roleRepository.findTopByRoleName(roleName); Role role = findRoleByRoleName(roleName);
if (role == null) { if (role == null) {
return Collections.emptySet(); return Collections.emptySet();
...@@ -139,6 +139,13 @@ public class RolePermissionService { ...@@ -139,6 +139,13 @@ public class RolePermissionService {
} }
/** /**
* Find role by role name, note that roleName should be unique
*/
public Role findRoleByRoleName(String roleName) {
return roleRepository.findTopByRoleName(roleName);
}
/**
* Check whether user has the permission * Check whether user has the permission
*/ */
public boolean userHasPermission(String userId, String permissionType, String targetId) { public boolean userHasPermission(String userId, String permissionType, String targetId) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment