Search in sources :

Example 11 with Role

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

the class AppController method findAppsByOwner.

@GetMapping("/by-owner")
public List<App> findAppsByOwner(@RequestParam("owner") String owner, Pageable page) {
    Set<String> appIds = Sets.newHashSet();
    List<Role> userRoles = rolePermissionService.findUserRoles(owner);
    for (Role role : userRoles) {
        String appId = RoleUtils.extractAppIdFromRoleName(role.getRoleName());
        if (appId != null) {
            appIds.add(appId);
        }
    }
    return appService.findByAppIds(appIds, page);
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 12 with Role

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

the class ConsumerService method assignAppRoleToConsumer.

@Transactional
public ConsumerRole assignAppRoleToConsumer(String token, String appId) {
    Long consumerId = getConsumerIdByToken(token);
    if (consumerId == null) {
        throw new BadRequestException("Token is Illegal");
    }
    Role masterRole = rolePermissionService.findRoleByRoleName(RoleUtils.buildAppMasterRoleName(appId));
    if (masterRole == null) {
        throw new BadRequestException("App's role does not exist. Please check whether app has created.");
    }
    long roleId = masterRole.getId();
    ConsumerRole managedModifyRole = consumerRoleRepository.findByConsumerIdAndRoleId(consumerId, roleId);
    if (managedModifyRole != null) {
        return managedModifyRole;
    }
    String operator = userInfoHolder.getUser().getUserId();
    ConsumerRole consumerRole = createConsumerRole(consumerId, roleId, operator);
    return consumerRoleRepository.save(consumerRole);
}
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 13 with Role

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

the class DefaultRoleInitializationService method initCreateAppRole.

@Transactional
public void initCreateAppRole() {
    if (rolePermissionService.findRoleByRoleName(SystemRoleManagerService.CREATE_APPLICATION_ROLE_NAME) != null) {
        return;
    }
    Permission createAppPermission = permissionRepository.findTopByPermissionTypeAndTargetId(PermissionType.CREATE_APPLICATION, SystemRoleManagerService.SYSTEM_PERMISSION_TARGET_ID);
    if (createAppPermission == null) {
        // create application permission init
        createAppPermission = createPermission(SystemRoleManagerService.SYSTEM_PERMISSION_TARGET_ID, PermissionType.CREATE_APPLICATION, "apollo");
        rolePermissionService.createPermission(createAppPermission);
    }
    // create application role init
    Role createAppRole = createRole(SystemRoleManagerService.CREATE_APPLICATION_ROLE_NAME, "apollo");
    rolePermissionService.createRoleWithPermissions(createAppRole, Sets.newHashSet(createAppPermission.getId()));
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with Role

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

the class DefaultRoleInitializationService method createAppMasterRole.

private void createAppMasterRole(String appId, String operator) {
    Set<Permission> appPermissions = Stream.of(PermissionType.CREATE_CLUSTER, PermissionType.CREATE_NAMESPACE, PermissionType.ASSIGN_ROLE).map(permissionType -> createPermission(appId, permissionType, operator)).collect(Collectors.toSet());
    Set<Permission> createdAppPermissions = rolePermissionService.createPermissions(appPermissions);
    Set<Long> appPermissionIds = createdAppPermissions.stream().map(BaseEntity::getId).collect(Collectors.toSet());
    // create app master role
    Role appMasterRole = createRole(RoleUtils.buildAppMasterRoleName(appId), operator);
    rolePermissionService.createRoleWithPermissions(appMasterRole, appPermissionIds);
}
Also used : ConfigConsts(com.ctrip.framework.apollo.core.ConfigConsts) PortalConfig(com.ctrip.framework.apollo.portal.component.config.PortalConfig) PermissionType(com.ctrip.framework.apollo.portal.constant.PermissionType) Env(com.ctrip.framework.apollo.portal.environment.Env) SystemRoleManagerService(com.ctrip.framework.apollo.portal.service.SystemRoleManagerService) Role(com.ctrip.framework.apollo.portal.entity.po.Role) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) RoleType(com.ctrip.framework.apollo.portal.constant.RoleType) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) Collectors(java.util.stream.Collectors) RoleInitializationService(com.ctrip.framework.apollo.portal.service.RoleInitializationService) Sets(com.google.common.collect.Sets) BaseEntity(com.ctrip.framework.apollo.common.entity.BaseEntity) RoleUtils(com.ctrip.framework.apollo.portal.util.RoleUtils) HashSet(java.util.HashSet) List(java.util.List) Stream(java.util.stream.Stream) PermissionRepository(com.ctrip.framework.apollo.portal.repository.PermissionRepository) App(com.ctrip.framework.apollo.common.entity.App) RolePermissionService(com.ctrip.framework.apollo.portal.service.RolePermissionService) Transactional(org.springframework.transaction.annotation.Transactional) Role(com.ctrip.framework.apollo.portal.entity.po.Role) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission)

Example 15 with Role

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

the class RolePermissionServiceTest method assembleRole.

private Role assembleRole(String roleName) {
    Role role = new Role();
    role.setRoleName(roleName);
    role.setDataChangeCreatedBy(someCreatedBy);
    role.setDataChangeLastModifiedBy(someLastModifiedBy);
    return role;
}
Also used : UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) Role(com.ctrip.framework.apollo.portal.entity.po.Role)

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