Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 246 additions & 0 deletions src/main/java/org/dspace/authorize/dao/ResourcePolicyDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.authorize.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import org.dspace.authorize.ResourcePolicy;
import org.dspace.content.DSpaceObject;
import org.dspace.core.Context;
import org.dspace.core.GenericDAO;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.Group;

/**
* Database Access Object interface class for the ResourcePolicy object.
* The implementation of this class is responsible for all database calls for the ResourcePolicy object and is
* autowired by spring
* This class should only be accessed from a single service and should never be exposed outside of the API
*
* @author kevinvandevelde at atmire.com
*/
public interface ResourcePolicyDAO extends GenericDAO<ResourcePolicy> {

public List<ResourcePolicy> findByDso(Context context, DSpaceObject dso) throws SQLException;

public List<ResourcePolicy> findByDsoAndType(Context context, DSpaceObject dSpaceObject, String type)
throws SQLException;

public List<ResourcePolicy> findByEPerson(Context context, EPerson ePerson) throws SQLException;

public List<ResourcePolicy> findByGroup(Context context, Group group) throws SQLException;

public List<ResourcePolicy> findByDSoAndAction(Context context, DSpaceObject dso, int actionId) throws SQLException;

public void deleteByDsoAndTypeAndAction(Context context, DSpaceObject dSpaceObject, String type, int action)
throws SQLException;

public List<ResourcePolicy> findByTypeGroupAction(Context context, DSpaceObject dso, Group group, int action)
throws SQLException;

/**
* Look for ResourcePolicies by DSpaceObject, Group, and action, ignoring IDs with a specific PolicyID.
* This method can be used to detect duplicate ResourcePolicies.
*
* @param notPolicyID ResourcePolicies with this ID will be ignored while looking out for equal ResourcePolicies.
* @return List of resource policies for the same DSpaceObject, group and action but other policyID.
* @throws SQLException
*/
public List<ResourcePolicy> findByTypeGroupActionExceptId(Context context, DSpaceObject dso, Group group,
int action, int notPolicyID) throws SQLException;

public List<ResourcePolicy> findByEPersonGroupTypeIdAction(Context context, EPerson e, List<Group> groups,
int action, int type_id) throws SQLException;

public void deleteByDso(Context context, DSpaceObject dso) throws SQLException;

public void deleteByDsoAndAction(Context context, DSpaceObject dso, int actionId) throws SQLException;

public void deleteByDsoAndType(Context context, DSpaceObject dSpaceObject, String type) throws SQLException;

public void deleteByGroup(Context context, Group group) throws SQLException;

public void deleteByDsoGroupPolicies(Context context, DSpaceObject dso, Group group) throws SQLException;

public void deleteByDsoEPersonPolicies(Context context, DSpaceObject dso, EPerson ePerson) throws SQLException;

/**
* Deletes all policies that belong to an EPerson
*
* @param context DSpace context object
* @param ePerson ePerson whose policies to delete
* @throws SQLException if database error
*/
public void deleteByEPerson(Context context, EPerson ePerson) throws SQLException;

public void deleteByDsoAndTypeNotEqualsTo(Context c, DSpaceObject o, String type) throws SQLException;

/**
* Return a list of policies for an object that match the action except the record labeled with the rpType
*
* @param c context
* @param o DSpaceObject policies relate to
* @param actionID action (defined in class Constants)
* @param rpType the resource policy type
* @return list of resource policies
* @throws SQLException if there's a database problem
*/
public List<ResourcePolicy> findByDSoAndActionExceptRpType(Context c, DSpaceObject o, int actionID,
String rpType) throws SQLException;

/**
* Return a paginated list of policies that belong to an EPerson
*
* @param context DSpace context object
* @param ePerson ePerson whose policies want to find
* @param offset the position of the first result to return
* @param limit paging limit
* @throws SQLException if database error
*/
public List<ResourcePolicy> findByEPerson(Context context, EPerson ePerson, int offset, int limit)
throws SQLException;

/**
* Count all the resource policies of the ePerson
*
* @param context DSpace context object
* @param ePerson ePerson whose policies want to count
* @return total resource policies of the ePerson
* @throws SQLException if database error
*/
public int countByEPerson(Context context, EPerson ePerson) throws SQLException;

/**
* Return a paginated list of policies related to a resourceUuid belong to an ePerson
*
* @param context DSpace context object
* @param ePerson ePerson whose policies want to find
* @param resourceUuid the uuid of an DSpace resource
* @param offset the position of the first result to return
* @param limit paging limit
* @return list of resource policies
* @throws SQLException if database error
*/
public List<ResourcePolicy> findByEPersonAndResourceUuid(Context context, EPerson ePerson, UUID resourceUuid,
int offset, int limit) throws SQLException;

/**
* Count all the policies related to a resourceUuid belong to an ePerson
*
* @param context DSpace context object
* @param resourceUuid the uuid of an DSpace resource
* @param ePerson ePerson whose policies want to find
* @return total policies
* @throws SQLException if database error
*/
public int countByEPersonAndResourceUuid(Context context, EPerson ePerson, UUID resourceUuid)
throws SQLException;

/**
* Return a paginated list of policies related to a DSpace resource filter by actionId
*
* @param context DSpace context object
* @param resourceUuid the uuid of an DSpace resource
* @param actionId id relative to action as READ, WRITE, DELITE etc.
* @param offset the position of the first result to return
* @param limit paging limit
* @return list of resource policies
* @throws SQLException if database error
*/
public List<ResourcePolicy> findByResouceUuidAndActionId(Context context, UUID resourceUuid, int actionId,
int offset, int limit) throws SQLException;

/**
* Count all the policies related to a resourceUuid and actionId
*
* @param context DSpace context object
* @param resourceUuid the uuid of an DSpace resource
* @param actionId id relative to action as READ, WRITE, DELITE etc.
* @return total policies
* @throws SQLException if database error
*/
public int countByResouceUuidAndActionId(Context context, UUID resourceUuid, int actionId)
throws SQLException;

/**
* Return a paginated list of policies related to a DSpace resource
*
* @param context DSpace context object
* @param resourceUuid the uuid of an DSpace resource
* @param offset the position of the first result to return
* @param limit paging limit
* @return list of resource policies
* @throws SQLException if database error
*/
public List<ResourcePolicy> findByResouceUuid(Context context, UUID resourceUuid, int offset, int limit)
throws SQLException;

/**
* Count all the policies by resourceUuid
*
* @param context DSpace context object
* @param resourceUuid the uuid of an DSpace resource
* @return total policies
* @throws SQLException if database error
*/
public int countByResourceUuid(Context context, UUID resourceUuid) throws SQLException;

/**
* Return a paginated list of policies related to a group
*
* @param context DSpace context object
* @param group DSpace group
* @param offset the position of the first result to return
* @param limit paging limit
* @return list of resource policies
* @throws SQLException if database error
*/
public List<ResourcePolicy> findByGroup(Context context, Group group, int offset, int limit)
throws SQLException;

/**
* Count all the resource policies of the group
*
* @param context DSpace context object
* @param group DSpace group
* @return total policies
* @throws SQLException if database error
*/
public int countResourcePolicyByGroup(Context context, Group group) throws SQLException;

/**
* Return a paginated list of policies related to a group and related to a resourceUuid
*
* @param context DSpace context object
* @param group DSpace group
* @param resourceUuid the uuid of an DSpace resource
* @param offset the position of the first result to return
* @param limit paging limit
* @return list of resource policies
* @throws SQLException if database error
*/
public List<ResourcePolicy> findByGroupAndResourceUuid(Context context, Group group, UUID resourceUuid,
int offset, int limit) throws SQLException;

/**
* Count all the resource policies of the group and of the resourceUuid
*
* @param context DSpace context object
* @param group DSpace group
* @param resourceUuid the uuid of an DSpace resource
* @return total policies
* @throws SQLException if database error
*/
public int countByGroupAndResourceUuid(Context context, Group group, UUID resourceUuid) throws SQLException;

public ResourcePolicy findOneById(Context context, Integer id) throws SQLException;


}
90 changes: 87 additions & 3 deletions src/main/java/org/dspace/content/CollectionServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -835,8 +839,88 @@ public List<Collection> findAuthorized(Context context, Community community, int
}
return myResults;
}

@Override
public List<Collection> findAuthorized(Context context, Community community, List<Integer> actions)
throws SQLException {

@Override
List<Collection> myCollections = new ArrayList<>();
EPerson eperson = context.getCurrentUser();

//If eperson is Administrator return all colls or if a community is not null only the community's collections
if (authorizeService.isAdmin(context, eperson)) {
if (community != null) {
return community.getCollections();
}
myCollections = this.findAll(context);
return myCollections;
}

//Get the collections of the eperson where is is admin of a community
List<Group> directGroups = new ArrayList<>(eperson.getGroups()); // direct membership
Queue<Group> queue = new LinkedList<>(directGroups);
while (!queue.isEmpty()) {
Group current = queue.poll();
List<Group> parents = current.getParentGroups();

for (Group parent : parents) {
if (directGroups.add(parent)) {
queue.add(parent);
}
}
}

List<ResourcePolicy> resourcePolicies = resourcePolicyService
.find(context, eperson, directGroups, Constants.ADMIN, Constants.COMMUNITY);
List<UUID> uuids = resourcePolicies.stream()
.map(policy -> policy.getdSpaceObject().getID())
.collect(Collectors.toList());

List<Community> communities = uuids.stream()
.map(uuid -> {
try {
return communityService.find(context, uuid);
} catch (SQLException e) {
return null; //ignore that uuid
}
})
.filter(Objects::nonNull)
.collect(Collectors.toList());

Set<Community> allCommunities = new HashSet<>(communities);
Set<Collection> allCommAdminCollections = communities.stream()
.flatMap(cm -> cm.getCollections().stream())
.collect(Collectors.toSet());
Queue<Community> queueComm = new LinkedList<>(communities);

while (!queueComm.isEmpty()) {
Community com = queueComm.poll();
List<Community> childrenComms = com.getSubcommunities();
for (Community childComm : childrenComms) {
if (allCommunities.add(childComm)) {
queueComm.add(childComm);
allCommAdminCollections.addAll(childComm.getCollections());
}
}
}

//Now get the collection when the eperson can deposit or is admin or is in a group with those privileges
myCollections = collectionDAO.findAuthorizedByEPerson(context, eperson, actions);
Set<Collection> allCollections = new HashSet<>(myCollections);
//Join EPerson Community Admin Collections with Collection Admins
allCollections.addAll(allCommAdminCollections);

List<Collection> collsAllowed = new ArrayList<>(allCollections);

//A community is passed, only the community's collections will be used and existing in eperson Authorizations
if (community != null) {
collsAllowed.retainAll(community.getCollections());
}

return collsAllowed;
}

@Override
public Collection findByGroup(Context context, Group group) throws SQLException {
return collectionDAO.findByGroup(context, group);
}
Expand Down Expand Up @@ -1095,8 +1179,8 @@ public List<Collection> findAllCollectionsByEntityType(Context context, String e

return collectionList;
}

/**
/**
* Returns total collection archived items
*
* @param collection Collection
Expand Down
Loading