Search in sources :

Example 1 with RolePermissionVO

use of org.apache.cloudstack.acl.RolePermissionVO in project cloudstack by apache.

the class RolePermissionsDaoImpl method persist.

@Override
public RolePermissionVO persist(final RolePermissionVO item) {
    item.setSortOrder(0);
    final List<RolePermissionVO> permissionsList = findAllByRoleIdSorted(item.getRoleId());
    if (permissionsList != null && permissionsList.size() > 0) {
        RolePermission lastRule = permissionsList.get(permissionsList.size() - 1);
        item.setSortOrder(lastRule.getSortOrder() + 1);
    }
    return super.persist(item);
}
Also used : RolePermissionVO(org.apache.cloudstack.acl.RolePermissionVO) RolePermission(org.apache.cloudstack.acl.RolePermission)

Example 2 with RolePermissionVO

use of org.apache.cloudstack.acl.RolePermissionVO in project cloudstack by apache.

the class RolePermissionsDaoImpl method updateSortOrder.

private boolean updateSortOrder(final RolePermissionVO permissionBeingMoved, final RolePermissionVO parentPermission) {
    if (parentPermission != null && permissionBeingMoved.getId() == parentPermission.getId()) {
        return true;
    }
    final List<RolePermissionVO> newOrderedPermissionsList = new ArrayList<>();
    // Null parent implies item needs to move to the top
    if (parentPermission == null) {
        newOrderedPermissionsList.add(permissionBeingMoved);
    }
    for (final RolePermissionVO permission : findAllByRoleIdSorted(permissionBeingMoved.getRoleId())) {
        if (permission.getId() == permissionBeingMoved.getId()) {
            continue;
        }
        newOrderedPermissionsList.add(permission);
        if (parentPermission != null && permission.getId() == parentPermission.getId()) {
            newOrderedPermissionsList.add(permissionBeingMoved);
        }
    }
    long sortOrder = 0L;
    for (final RolePermissionVO permission : newOrderedPermissionsList) {
        permission.setSortOrder(sortOrder++);
        if (!update(permission.getId(), permission)) {
            LOGGER.warn("Failed to update item's sort order with id:" + permission.getId() + " while moving permission with id:" + permissionBeingMoved.getId() + " to a new position");
            return false;
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) RolePermissionVO(org.apache.cloudstack.acl.RolePermissionVO)

Example 3 with RolePermissionVO

use of org.apache.cloudstack.acl.RolePermissionVO in project cloudstack by apache.

the class RolePermissionsDaoImpl method findAllByRoleIdSorted.

@Override
public List<RolePermissionVO> findAllByRoleIdSorted(final Long roleId) {
    final SearchCriteria<RolePermissionVO> sc = RolePermissionsSearch.create();
    if (roleId != null && roleId > 0L) {
        sc.setParameters("roleId", roleId);
    }
    final Filter searchBySorted = new Filter(RolePermissionVO.class, "sortOrder", true, null, null);
    final List<RolePermissionVO> rolePermissionList = listBy(sc, searchBySorted);
    if (rolePermissionList == null) {
        return Collections.emptyList();
    }
    return rolePermissionList;
}
Also used : Filter(com.cloud.utils.db.Filter) RolePermissionVO(org.apache.cloudstack.acl.RolePermissionVO)

Aggregations

RolePermissionVO (org.apache.cloudstack.acl.RolePermissionVO)3 Filter (com.cloud.utils.db.Filter)1 ArrayList (java.util.ArrayList)1 RolePermission (org.apache.cloudstack.acl.RolePermission)1