Search in sources :

Example 6 with Permission

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

the class DefaultRolePermissionService method createPermissions.

/**
 * Create permissions, note that permissionType + targetId should be unique
 */
@Transactional
public Set<Permission> createPermissions(Set<Permission> permissions) {
    Multimap<String, String> targetIdPermissionTypes = HashMultimap.create();
    for (Permission permission : permissions) {
        targetIdPermissionTypes.put(permission.getTargetId(), permission.getPermissionType());
    }
    for (String targetId : targetIdPermissionTypes.keySet()) {
        Collection<String> permissionTypes = targetIdPermissionTypes.get(targetId);
        List<Permission> current = permissionRepository.findByPermissionTypeInAndTargetId(permissionTypes, targetId);
        Preconditions.checkState(CollectionUtils.isEmpty(current), "Permission with permissionType %s targetId %s already exists!", permissionTypes, targetId);
    }
    Iterable<Permission> results = permissionRepository.saveAll(permissions);
    return StreamSupport.stream(results.spliterator(), false).collect(Collectors.toSet());
}
Also used : Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with Permission

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

the class DefaultRolePermissionService method createPermission.

/**
 * Create permission, note that permissionType + targetId should be unique
 */
@Transactional
public Permission createPermission(Permission permission) {
    String permissionType = permission.getPermissionType();
    String targetId = permission.getTargetId();
    Permission current = permissionRepository.findTopByPermissionTypeAndTargetId(permissionType, targetId);
    Preconditions.checkState(current == null, "Permission with permissionType %s targetId %s already exists!", permissionType, targetId);
    return permissionRepository.save(permission);
}
Also used : Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with Permission

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

the class DefaultRoleInitializationService method createNamespaceRole.

private void createNamespaceRole(String appId, String namespaceName, String permissionType, String roleName, String operator) {
    Permission permission = createPermission(RoleUtils.buildNamespaceTargetId(appId, namespaceName), permissionType, operator);
    Permission createdPermission = rolePermissionService.createPermission(permission);
    Role role = createRole(roleName, operator);
    rolePermissionService.createRoleWithPermissions(role, Sets.newHashSet(createdPermission.getId()));
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission)

Example 9 with Permission

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

the class DefaultRoleInitializationService method createManageAppMasterRole.

@Transactional
private void createManageAppMasterRole(String appId, String operator) {
    Permission permission = createPermission(appId, PermissionType.MANAGE_APP_MASTER, operator);
    rolePermissionService.createPermission(permission);
    Role role = createRole(RoleUtils.buildAppRoleName(appId, PermissionType.MANAGE_APP_MASTER), operator);
    Set<Long> permissionIds = new HashSet<>();
    permissionIds.add(permission.getId());
    rolePermissionService.createRoleWithPermissions(role, permissionIds);
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with Permission

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

the class DefaultRoleInitializationService method createNamespaceEnvRole.

private void createNamespaceEnvRole(String appId, String namespaceName, String permissionType, String env, String roleName, String operator) {
    Permission permission = createPermission(RoleUtils.buildNamespaceTargetId(appId, namespaceName, env), permissionType, operator);
    Permission createdPermission = rolePermissionService.createPermission(permission);
    Role role = createRole(roleName, operator);
    rolePermissionService.createRoleWithPermissions(role, Sets.newHashSet(createdPermission.getId()));
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission)

Aggregations

Permission (com.ctrip.framework.apollo.portal.entity.po.Permission)16 RolePermission (com.ctrip.framework.apollo.portal.entity.po.RolePermission)9 Role (com.ctrip.framework.apollo.portal.entity.po.Role)5 Transactional (org.springframework.transaction.annotation.Transactional)5 AbstractIntegrationTest (com.ctrip.framework.apollo.portal.AbstractIntegrationTest)4 Test (org.junit.Test)4 Sql (org.springframework.test.context.jdbc.Sql)4 HashSet (java.util.HashSet)2 App (com.ctrip.framework.apollo.common.entity.App)1 BaseEntity (com.ctrip.framework.apollo.common.entity.BaseEntity)1 ConfigConsts (com.ctrip.framework.apollo.core.ConfigConsts)1 ConsumerRole (com.ctrip.framework.apollo.openapi.entity.ConsumerRole)1 PortalConfig (com.ctrip.framework.apollo.portal.component.config.PortalConfig)1 PermissionType (com.ctrip.framework.apollo.portal.constant.PermissionType)1 RoleType (com.ctrip.framework.apollo.portal.constant.RoleType)1 UserRole (com.ctrip.framework.apollo.portal.entity.po.UserRole)1 Env (com.ctrip.framework.apollo.portal.environment.Env)1 PermissionRepository (com.ctrip.framework.apollo.portal.repository.PermissionRepository)1 RoleInitializationService (com.ctrip.framework.apollo.portal.service.RoleInitializationService)1 RolePermissionService (com.ctrip.framework.apollo.portal.service.RolePermissionService)1