Search in sources :

Example 1 with Role

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

the class ConsumerService method assignNamespaceRoleToConsumer.

@Transactional
public List<ConsumerRole> assignNamespaceRoleToConsumer(String token, String appId, String namespaceName, String env) {
    Long consumerId = getConsumerIdByToken(token);
    if (consumerId == null) {
        throw new BadRequestException("Token is Illegal");
    }
    Role namespaceModifyRole = rolePermissionService.findRoleByRoleName(RoleUtils.buildModifyNamespaceRoleName(appId, namespaceName, env));
    Role namespaceReleaseRole = rolePermissionService.findRoleByRoleName(RoleUtils.buildReleaseNamespaceRoleName(appId, namespaceName, env));
    if (namespaceModifyRole == null || namespaceReleaseRole == null) {
        throw new BadRequestException("Namespace's role does not exist. Please check whether namespace has created.");
    }
    long namespaceModifyRoleId = namespaceModifyRole.getId();
    long namespaceReleaseRoleId = namespaceReleaseRole.getId();
    ConsumerRole managedModifyRole = consumerRoleRepository.findByConsumerIdAndRoleId(consumerId, namespaceModifyRoleId);
    ConsumerRole managedReleaseRole = consumerRoleRepository.findByConsumerIdAndRoleId(consumerId, namespaceReleaseRoleId);
    if (managedModifyRole != null && managedReleaseRole != null) {
        return Arrays.asList(managedModifyRole, managedReleaseRole);
    }
    String operator = userInfoHolder.getUser().getUserId();
    ConsumerRole namespaceModifyConsumerRole = createConsumerRole(consumerId, namespaceModifyRoleId, operator);
    ConsumerRole namespaceReleaseConsumerRole = createConsumerRole(consumerId, namespaceReleaseRoleId, operator);
    ConsumerRole createdModifyConsumerRole = consumerRoleRepository.save(namespaceModifyConsumerRole);
    ConsumerRole createdReleaseConsumerRole = consumerRoleRepository.save(namespaceReleaseConsumerRole);
    return Arrays.asList(createdModifyConsumerRole, createdReleaseConsumerRole);
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) ConsumerRole(com.ctrip.framework.apollo.openapi.entity.ConsumerRole) ConsumerRole(com.ctrip.framework.apollo.openapi.entity.ConsumerRole) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Role

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

the class RoleInitializationServiceTest method mockRole.

private Role mockRole(String roleName) {
    Role role = new Role();
    role.setRoleName(roleName);
    return role;
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role)

Example 3 with Role

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

the class RolePermissionServiceTest method testCreateRoleWithPermissions.

@Test
@Sql(scripts = "/sql/permission/insert-test-permissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreateRoleWithPermissions() throws Exception {
    String someRoleName = "someRoleName";
    Role role = assembleRole(someRoleName);
    Set<Long> permissionIds = Sets.newHashSet(990L, 991L);
    Role created = rolePermissionService.createRoleWithPermissions(role, permissionIds);
    Role createdFromDB = roleRepository.findById(created.getId()).orElse(null);
    List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleIdIn(Sets.newHashSet(createdFromDB.getId()));
    Set<Long> rolePermissionIds = rolePermissions.stream().map(RolePermission::getPermissionId).collect(Collectors.toSet());
    assertEquals(someRoleName, createdFromDB.getRoleName());
    assertEquals(2, rolePermissionIds.size());
    assertTrue(rolePermissionIds.containsAll(permissionIds));
}
Also used : UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) Role(com.ctrip.framework.apollo.portal.entity.po.Role) RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) AbstractIntegrationTest(com.ctrip.framework.apollo.portal.AbstractIntegrationTest) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 4 with Role

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

the class ConsumerServiceTest method testAssignNamespaceRoleToConsumer.

@Test
public void testAssignNamespaceRoleToConsumer() {
    Long consumerId = 1L;
    String token = "token";
    doReturn(consumerId).when(consumerService).getConsumerIdByToken(token);
    String testNamespace = "namespace";
    String modifyRoleName = RoleUtils.buildModifyNamespaceRoleName(testAppId, testNamespace);
    String releaseRoleName = RoleUtils.buildReleaseNamespaceRoleName(testAppId, testNamespace);
    String envModifyRoleName = RoleUtils.buildModifyNamespaceRoleName(testAppId, testNamespace, Env.DEV.toString());
    String envReleaseRoleName = RoleUtils.buildReleaseNamespaceRoleName(testAppId, testNamespace, Env.DEV.toString());
    long modifyRoleId = 1;
    long releaseRoleId = 2;
    long envModifyRoleId = 3;
    long envReleaseRoleId = 4;
    Role modifyRole = createRole(modifyRoleId, modifyRoleName);
    Role releaseRole = createRole(releaseRoleId, releaseRoleName);
    Role envModifyRole = createRole(envModifyRoleId, modifyRoleName);
    Role envReleaseRole = createRole(envReleaseRoleId, releaseRoleName);
    when(rolePermissionService.findRoleByRoleName(modifyRoleName)).thenReturn(modifyRole);
    when(rolePermissionService.findRoleByRoleName(releaseRoleName)).thenReturn(releaseRole);
    when(rolePermissionService.findRoleByRoleName(envModifyRoleName)).thenReturn(envModifyRole);
    when(rolePermissionService.findRoleByRoleName(envReleaseRoleName)).thenReturn(envReleaseRole);
    when(consumerRoleRepository.findByConsumerIdAndRoleId(consumerId, modifyRoleId)).thenReturn(null);
    UserInfo owner = createUser(testOwner);
    when(userInfoHolder.getUser()).thenReturn(owner);
    ConsumerRole namespaceModifyConsumerRole = createConsumerRole(consumerId, modifyRoleId);
    ConsumerRole namespaceEnvModifyConsumerRole = createConsumerRole(consumerId, envModifyRoleId);
    ConsumerRole namespaceReleaseConsumerRole = createConsumerRole(consumerId, releaseRoleId);
    ConsumerRole namespaceEnvReleaseConsumerRole = createConsumerRole(consumerId, envReleaseRoleId);
    doReturn(namespaceModifyConsumerRole).when(consumerService).createConsumerRole(consumerId, modifyRoleId, testOwner);
    doReturn(namespaceEnvModifyConsumerRole).when(consumerService).createConsumerRole(consumerId, envModifyRoleId, testOwner);
    doReturn(namespaceReleaseConsumerRole).when(consumerService).createConsumerRole(consumerId, releaseRoleId, testOwner);
    doReturn(namespaceEnvReleaseConsumerRole).when(consumerService).createConsumerRole(consumerId, envReleaseRoleId, testOwner);
    consumerService.assignNamespaceRoleToConsumer(token, testAppId, testNamespace);
    consumerService.assignNamespaceRoleToConsumer(token, testAppId, testNamespace, Env.DEV.toString());
    verify(consumerRoleRepository).save(namespaceModifyConsumerRole);
    verify(consumerRoleRepository).save(namespaceEnvModifyConsumerRole);
    verify(consumerRoleRepository).save(namespaceReleaseConsumerRole);
    verify(consumerRoleRepository).save(namespaceEnvReleaseConsumerRole);
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) ConsumerRole(com.ctrip.framework.apollo.openapi.entity.ConsumerRole) ConsumerRole(com.ctrip.framework.apollo.openapi.entity.ConsumerRole) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Example 5 with Role

use of com.ctrip.framework.apollo.portal.entity.po.Role 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);
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

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