Search in sources :

Example 16 with Role

use of com.ctrip.framework.apollo.portal.entity.po.Role in project apollo by ctripcorp.

the class RolePermissionServiceTest method testCreateRoleWithPermissionsWithRoleExisted.

@Test(expected = IllegalStateException.class)
@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 testCreateRoleWithPermissionsWithRoleExisted() throws Exception {
    String someRoleName = "someRoleName";
    Role role = assembleRole(someRoleName);
    rolePermissionService.createRoleWithPermissions(role, null);
}
Also used : UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) Role(com.ctrip.framework.apollo.portal.entity.po.Role) AbstractIntegrationTest(com.ctrip.framework.apollo.portal.AbstractIntegrationTest) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 17 with Role

use of com.ctrip.framework.apollo.portal.entity.po.Role 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;
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) Date(java.util.Date) Role(com.ctrip.framework.apollo.portal.entity.po.Role) Autowired(org.springframework.beans.factory.annotation.Autowired) Multimap(com.google.common.collect.Multimap) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) StreamSupport(java.util.stream.StreamSupport) RolePermissionService(com.ctrip.framework.apollo.portal.service.RolePermissionService) UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) PortalConfig(com.ctrip.framework.apollo.portal.component.config.PortalConfig) RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) Collection(java.util.Collection) Set(java.util.Set) ConsumerRoleRepository(com.ctrip.framework.apollo.openapi.repository.ConsumerRoleRepository) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) UserRoleRepository(com.ctrip.framework.apollo.portal.repository.UserRoleRepository) RolePermissionRepository(com.ctrip.framework.apollo.portal.repository.RolePermissionRepository) PermissionRepository(com.ctrip.framework.apollo.portal.repository.PermissionRepository) CollectionUtils(org.springframework.util.CollectionUtils) Preconditions(com.google.common.base.Preconditions) RoleRepository(com.ctrip.framework.apollo.portal.repository.RoleRepository) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with Role

use of com.ctrip.framework.apollo.portal.entity.po.Role in project apollo by ctripcorp.

the class DefaultRolePermissionService method queryUsersWithRole.

/**
 * Query users with role
 */
public Set<UserInfo> queryUsersWithRole(String roleName) {
    Role role = findRoleByRoleName(roleName);
    if (role == null) {
        return Collections.emptySet();
    }
    List<UserRole> userRoles = userRoleRepository.findByRoleId(role.getId());
    return userRoles.stream().map(userRole -> {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userRole.getUserId());
        return userInfo;
    }).collect(Collectors.toSet());
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) Date(java.util.Date) Role(com.ctrip.framework.apollo.portal.entity.po.Role) Autowired(org.springframework.beans.factory.annotation.Autowired) Multimap(com.google.common.collect.Multimap) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) StreamSupport(java.util.stream.StreamSupport) RolePermissionService(com.ctrip.framework.apollo.portal.service.RolePermissionService) UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) PortalConfig(com.ctrip.framework.apollo.portal.component.config.PortalConfig) RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) Collection(java.util.Collection) Set(java.util.Set) ConsumerRoleRepository(com.ctrip.framework.apollo.openapi.repository.ConsumerRoleRepository) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) UserRoleRepository(com.ctrip.framework.apollo.portal.repository.UserRoleRepository) RolePermissionRepository(com.ctrip.framework.apollo.portal.repository.RolePermissionRepository) PermissionRepository(com.ctrip.framework.apollo.portal.repository.PermissionRepository) CollectionUtils(org.springframework.util.CollectionUtils) Preconditions(com.google.common.base.Preconditions) RoleRepository(com.ctrip.framework.apollo.portal.repository.RoleRepository) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo)

Example 19 with Role

use of com.ctrip.framework.apollo.portal.entity.po.Role in project apollo by ctripcorp.

the class ConsumerServiceTest method createRole.

private Role createRole(long roleId, String roleName) {
    Role role = new Role();
    role.setId(roleId);
    role.setRoleName(roleName);
    return role;
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) ConsumerRole(com.ctrip.framework.apollo.openapi.entity.ConsumerRole)

Example 20 with Role

use of com.ctrip.framework.apollo.portal.entity.po.Role in project apollo by ctripcorp.

the class AppControllerTest method generateRoleByIdAndRoleName.

private static Role generateRoleByIdAndRoleName(long id, String roleName) {
    Role role = new Role();
    role.setId(id);
    role.setRoleName(roleName);
    return role;
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) ConsumerRole(com.ctrip.framework.apollo.openapi.entity.ConsumerRole)

Aggregations

Role (com.ctrip.framework.apollo.portal.entity.po.Role)20 Transactional (org.springframework.transaction.annotation.Transactional)9 Permission (com.ctrip.framework.apollo.portal.entity.po.Permission)8 UserRole (com.ctrip.framework.apollo.portal.entity.po.UserRole)7 ConsumerRole (com.ctrip.framework.apollo.openapi.entity.ConsumerRole)5 PortalConfig (com.ctrip.framework.apollo.portal.component.config.PortalConfig)4 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)4 RolePermission (com.ctrip.framework.apollo.portal.entity.po.RolePermission)4 PermissionRepository (com.ctrip.framework.apollo.portal.repository.PermissionRepository)4 RolePermissionService (com.ctrip.framework.apollo.portal.service.RolePermissionService)4 Sets (com.google.common.collect.Sets)4 Date (java.util.Date)4 List (java.util.List)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 ConsumerRoleRepository (com.ctrip.framework.apollo.openapi.repository.ConsumerRoleRepository)3 RolePermissionRepository (com.ctrip.framework.apollo.portal.repository.RolePermissionRepository)3 RoleRepository (com.ctrip.framework.apollo.portal.repository.RoleRepository)3 UserRoleRepository (com.ctrip.framework.apollo.portal.repository.UserRoleRepository)3