Search in sources :

Example 1 with UserRole

use of com.hortonworks.streamline.streams.security.catalog.UserRole in project streamline by hortonworks.

the class SecurityCatalogResource method addOrUpdateRoleUsers.

private Response addOrUpdateRoleUsers(Long roleId, Set<Long> userIds) {
    List<UserRole> userRoles = new ArrayList<>();
    Role roleToQuery = catalogService.getRole(roleId);
    Set<Long> currentUserIds = catalogService.listUsers(roleToQuery).stream().map(User::getId).collect(Collectors.toSet());
    Set<Long> userIdsToAdd = Sets.difference(userIds, currentUserIds);
    Set<Long> userIdsToRemove = Sets.difference(currentUserIds, userIds);
    Sets.intersection(currentUserIds, userIds).forEach(userId -> {
        userRoles.add(new UserRole(userId, roleId));
    });
    userIdsToRemove.forEach(userId -> catalogService.removeUserRole(userId, roleId));
    userIdsToAdd.forEach(userId -> {
        userRoles.add(catalogService.addUserRole(userId, roleId));
    });
    return WSUtils.respondEntities(userRoles, OK);
}
Also used : UserRole(com.hortonworks.streamline.streams.security.catalog.UserRole) Role(com.hortonworks.streamline.streams.security.catalog.Role) UserRole(com.hortonworks.streamline.streams.security.catalog.UserRole) ArrayList(java.util.ArrayList)

Example 2 with UserRole

use of com.hortonworks.streamline.streams.security.catalog.UserRole in project streamline by hortonworks.

the class SecurityCatalogService method addUserRole.

public UserRole addUserRole(Long userId, Long roleId) {
    UserRole userRole = new UserRole();
    userRole.setUserId(userId);
    userRole.setRoleId(roleId);
    dao.add(userRole);
    return userRole;
}
Also used : UserRole(com.hortonworks.streamline.streams.security.catalog.UserRole)

Example 3 with UserRole

use of com.hortonworks.streamline.streams.security.catalog.UserRole in project streamline by hortonworks.

the class SecurityCatalogService method removeUserRole.

public UserRole removeUserRole(Long userId, Long roleId) {
    UserRole userRole = new UserRole();
    userRole.setUserId(userId);
    userRole.setRoleId(roleId);
    return dao.remove(new StorableKey(UserRole.NAMESPACE, userRole.getPrimaryKey()));
}
Also used : UserRole(com.hortonworks.streamline.streams.security.catalog.UserRole) StorableKey(com.hortonworks.registries.storage.StorableKey)

Example 4 with UserRole

use of com.hortonworks.streamline.streams.security.catalog.UserRole in project streamline by hortonworks.

the class SecurityCatalogService method removeRole.

public Role removeRole(Long roleId) {
    // check if role is part of any parent roles, if so parent role should be deleted first.
    Set<Role> parentRoles = getParentRoles(roleId);
    if (!parentRoles.isEmpty()) {
        throw new IllegalStateException("Role is a child role of the following parent role(s): " + parentRoles + ". Parent roles must be deleted first.");
    }
    // check if role has any users
    List<QueryParam> qps = QueryParam.params(UserRole.ROLE_ID, String.valueOf(roleId));
    Collection<UserRole> userRoles = listUserRoles(qps);
    if (!userRoles.isEmpty()) {
        throw new IllegalStateException("Role has users");
    }
    // remove child role associations
    qps = QueryParam.params(RoleHierarchy.PARENT_ID, String.valueOf(roleId));
    Collection<RoleHierarchy> roleHierarchies = dao.find(RoleHierarchy.NAMESPACE, qps);
    LOG.info("Removing child role association for role id {}", roleId);
    roleHierarchies.forEach(rh -> removeChildRole(roleId, rh.getChildId()));
    // remove permissions assigned to role
    qps = QueryParam.params(AclEntry.SID_ID, String.valueOf(roleId), AclEntry.SID_TYPE, AclEntry.SidType.ROLE.toString());
    LOG.info("Removing ACL entries for role id {}", roleId);
    listAcls(qps).forEach(aclEntry -> removeAcl(aclEntry.getId()));
    Role role = new Role();
    role.setId(roleId);
    return dao.remove(new StorableKey(Role.NAMESPACE, role.getPrimaryKey()));
}
Also used : UserRole(com.hortonworks.streamline.streams.security.catalog.UserRole) Role(com.hortonworks.streamline.streams.security.catalog.Role) QueryParam(com.hortonworks.registries.common.QueryParam) UserRole(com.hortonworks.streamline.streams.security.catalog.UserRole) StorableKey(com.hortonworks.registries.storage.StorableKey) RoleHierarchy(com.hortonworks.streamline.streams.security.catalog.RoleHierarchy)

Aggregations

UserRole (com.hortonworks.streamline.streams.security.catalog.UserRole)4 StorableKey (com.hortonworks.registries.storage.StorableKey)2 Role (com.hortonworks.streamline.streams.security.catalog.Role)2 QueryParam (com.hortonworks.registries.common.QueryParam)1 RoleHierarchy (com.hortonworks.streamline.streams.security.catalog.RoleHierarchy)1 ArrayList (java.util.ArrayList)1