use of com.ctrip.framework.apollo.portal.entity.po.UserRole in project apollo by ctripcorp.
the class DefaultRolePermissionService method removeRoleFromUsers.
/**
* Remove role from users
*/
@Transactional
public void removeRoleFromUsers(String roleName, Set<String> userIds, String operatorUserId) {
Role role = findRoleByRoleName(roleName);
Preconditions.checkState(role != null, "Role %s doesn't exist!", roleName);
List<UserRole> existedUserRoles = userRoleRepository.findByUserIdInAndRoleId(userIds, role.getId());
for (UserRole userRole : existedUserRoles) {
userRole.setDeleted(true);
userRole.setDataChangeLastModifiedTime(new Date());
userRole.setDataChangeLastModifiedBy(operatorUserId);
}
userRoleRepository.saveAll(existedUserRoles);
}
use of com.ctrip.framework.apollo.portal.entity.po.UserRole in project apollo by ctripcorp.
the class RolePermissionServiceTest method testAssignRoleToUsersWithUserRolesExisted.
@Test
@Sql(scripts = "/sql/permission/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-userroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testAssignRoleToUsersWithUserRolesExisted() throws Exception {
String someRoleName = "someRoleName";
String someUser = "someUser";
String anotherUser = "anotherUser";
String operator = "operator";
Set<String> users = Sets.newHashSet(someUser, anotherUser);
rolePermissionService.assignRoleToUsers(someRoleName, users, operator);
List<UserRole> userRoles = userRoleRepository.findByRoleId(990);
Set<String> usersWithRole = Sets.newHashSet();
for (UserRole userRole : userRoles) {
assertEquals("someOperator", userRole.getDataChangeCreatedBy());
assertEquals("someOperator", userRole.getDataChangeLastModifiedBy());
usersWithRole.add(userRole.getUserId());
}
assertEquals(2, usersWithRole.size());
assertTrue(usersWithRole.containsAll(users));
}
use of com.ctrip.framework.apollo.portal.entity.po.UserRole in project apollo by ctripcorp.
the class RolePermissionServiceTest method testAssignRoleToUsers.
@Test
@Sql(scripts = "/sql/permission/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testAssignRoleToUsers() throws Exception {
String someRoleName = "someRoleName";
String someUser = "someUser";
String anotherUser = "anotherUser";
String operator = "operator";
Set<String> users = Sets.newHashSet(someUser, anotherUser);
rolePermissionService.assignRoleToUsers(someRoleName, users, operator);
List<UserRole> userRoles = userRoleRepository.findByRoleId(990);
Set<String> usersWithRole = Sets.newHashSet();
for (UserRole userRole : userRoles) {
assertEquals(operator, userRole.getDataChangeCreatedBy());
assertEquals(operator, userRole.getDataChangeLastModifiedBy());
usersWithRole.add(userRole.getUserId());
}
assertEquals(2, usersWithRole.size());
assertTrue(usersWithRole.containsAll(users));
}
use of com.ctrip.framework.apollo.portal.entity.po.UserRole in project apollo by ctripcorp.
the class DefaultRolePermissionService method assignRoleToUsers.
/**
* Assign role to users
*
* @return the users assigned roles
*/
@Transactional
public Set<String> assignRoleToUsers(String roleName, Set<String> userIds, String operatorUserId) {
Role role = findRoleByRoleName(roleName);
Preconditions.checkState(role != null, "Role %s doesn't exist!", roleName);
List<UserRole> existedUserRoles = userRoleRepository.findByUserIdInAndRoleId(userIds, role.getId());
Set<String> existedUserIds = existedUserRoles.stream().map(UserRole::getUserId).collect(Collectors.toSet());
Set<String> toAssignUserIds = Sets.difference(userIds, existedUserIds);
Iterable<UserRole> toCreate = toAssignUserIds.stream().map(userId -> {
UserRole userRole = new UserRole();
userRole.setRoleId(role.getId());
userRole.setUserId(userId);
userRole.setDataChangeCreatedBy(operatorUserId);
userRole.setDataChangeLastModifiedBy(operatorUserId);
return userRole;
}).collect(Collectors.toList());
userRoleRepository.saveAll(toCreate);
return toAssignUserIds;
}
use of com.ctrip.framework.apollo.portal.entity.po.UserRole in project apollo by ctripcorp.
the class DefaultRolePermissionService method userHasPermission.
/**
* Check whether user has the permission
*/
public boolean userHasPermission(String userId, String permissionType, String targetId) {
Permission permission = permissionRepository.findTopByPermissionTypeAndTargetId(permissionType, targetId);
if (permission == null) {
return false;
}
if (isSuperAdmin(userId)) {
return true;
}
List<UserRole> userRoles = userRoleRepository.findByUserId(userId);
if (CollectionUtils.isEmpty(userRoles)) {
return false;
}
Set<Long> roleIds = userRoles.stream().map(UserRole::getRoleId).collect(Collectors.toSet());
List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleIdIn(roleIds);
if (CollectionUtils.isEmpty(rolePermissions)) {
return false;
}
for (RolePermission rolePermission : rolePermissions) {
if (rolePermission.getPermissionId() == permission.getId()) {
return true;
}
}
return false;
}
Aggregations