Skip to content

Commit 7777c25

Browse files
committed
[DURACOM-447] Cris security from
- 98939d6 - 865f8c6 - CST-3981 - DSC-643 (check)
1 parent f55e734 commit 7777c25

File tree

42 files changed

+2081
-57
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2081
-57
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* The contents of this file are subject to the license and copyright
3+
* detailed in the LICENSE and NOTICE files at the root of the source
4+
* tree and available online at
5+
*
6+
* http://www.dspace.org/license/
7+
*/
8+
package org.dspace.authorize;
9+
10+
import java.util.UUID;
11+
12+
/**
13+
* VO that model an owner of a resource policy (eperson or group).
14+
*
15+
* @author Luca Giamminonni (luca.giamminonni at 4science.it)
16+
*
17+
*/
18+
public class ResourcePolicyOwnerVO {
19+
20+
private final UUID ePersonId;
21+
22+
private final UUID groupId;
23+
24+
public ResourcePolicyOwnerVO(UUID ePersonId, UUID groupId) {
25+
this.ePersonId = ePersonId;
26+
this.groupId = groupId;
27+
}
28+
29+
public UUID getEPersonId() {
30+
return ePersonId;
31+
}
32+
33+
public UUID getGroupId() {
34+
return groupId;
35+
}
36+
37+
}

dspace-api/src/main/java/org/dspace/authorize/ResourcePolicyServiceImpl.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class ResourcePolicyServiceImpl implements ResourcePolicyService {
4141
/**
4242
* log4j logger
4343
*/
44-
private static Logger log = org.apache.logging.log4j.LogManager.getLogger(ResourcePolicyServiceImpl.class);
44+
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(ResourcePolicyServiceImpl.class);
4545

4646
@Autowired(required = true)
4747
protected ContentServiceFactory contentServiceFactory;
@@ -115,6 +115,11 @@ public List<ResourcePolicy> find(Context c, DSpaceObject o, int actionId) throws
115115
return resourcePolicyDAO.findByDSoAndAction(c, o, actionId);
116116
}
117117

118+
@Override
119+
public List<ResourcePolicy> find(Context c, DSpaceObject o, int actionId, String type) throws SQLException {
120+
return resourcePolicyDAO.findByDSoAndActionAndType(c, o, actionId, type);
121+
}
122+
118123
@Override
119124
public List<ResourcePolicy> find(Context c, DSpaceObject dso, Group group, int action) throws SQLException {
120125
return resourcePolicyDAO.findByTypeGroupAction(c, dso, group, action);
@@ -205,13 +210,11 @@ public boolean isDateValid(ResourcePolicy resourcePolicy) {
205210
}
206211

207212
// now expiration date
208-
if (ed != null && now.isAfter(ed)) {
209-
// end date is set, return false if we're after it
210-
return false;
211-
}
213+
// end date is set, return false if we're after it
214+
return ed == null || !now.isAfter(ed);
212215

213216
// if we made it this far, start < now < end
214-
return true; // date must be okay
217+
// date must be okay
215218
}
216219

217220
@Override
@@ -361,7 +364,7 @@ public int countByEPerson(Context context, EPerson eperson) throws SQLException
361364

362365
@Override
363366
public List<ResourcePolicy> findByEPersonAndResourceUuid(Context context, EPerson eperson, UUID resourceUuid,
364-
int offset, int limit) throws SQLException {
367+
int offset, int limit) throws SQLException {
365368
return resourcePolicyDAO.findByEPersonAndResourceUuid(context, eperson, resourceUuid, offset, limit);
366369
}
367370

@@ -373,7 +376,7 @@ public int countResourcePoliciesByEPersonAndResourceUuid(Context context, EPerso
373376

374377
@Override
375378
public List<ResourcePolicy> findByResouceUuidAndActionId(Context context, UUID resourceUuid, int actionId,
376-
int offset, int limit) throws SQLException {
379+
int offset, int limit) throws SQLException {
377380
return resourcePolicyDAO.findByResouceUuidAndActionId(context, resourceUuid, actionId, offset, limit);
378381
}
379382

@@ -405,7 +408,7 @@ public int countResourcePolicyByGroup(Context context, Group group) throws SQLEx
405408

406409
@Override
407410
public List<ResourcePolicy> findByGroupAndResourceUuid(Context context, Group group, UUID resourceUuid,
408-
int offset, int limit) throws SQLException {
411+
int offset, int limit) throws SQLException {
409412
return resourcePolicyDAO.findByGroupAndResourceUuid(context, group, resourceUuid, offset, limit);
410413
}
411414

@@ -428,4 +431,10 @@ public boolean isMyResourcePolicy(Context context, EPerson eperson, Integer id)
428431
}
429432
return isMy || authorizeService.isAdmin(context, eperson, resourcePolicy.getdSpaceObject());
430433
}
434+
435+
@Override
436+
public List<ResourcePolicyOwnerVO> findValidPolicyOwners(Context c, List<UUID> dsoIds, int actionID)
437+
throws SQLException {
438+
return resourcePolicyDAO.findValidPolicyOwners(c, dsoIds, actionID);
439+
}
431440
}

dspace-api/src/main/java/org/dspace/authorize/dao/ResourcePolicyDAO.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.UUID;
1313

1414
import org.dspace.authorize.ResourcePolicy;
15+
import org.dspace.authorize.ResourcePolicyOwnerVO;
1516
import org.dspace.content.DSpaceObject;
1617
import org.dspace.core.Context;
1718
import org.dspace.core.GenericDAO;
@@ -42,6 +43,9 @@ public List<ResourcePolicy> findByDsoAndType(Context context, DSpaceObject dSpac
4243
public void deleteByDsoAndTypeAndAction(Context context, DSpaceObject dSpaceObject, String type, int action)
4344
throws SQLException;
4445

46+
public List<ResourcePolicy> findByDSoAndActionAndType(Context c, DSpaceObject o, int actionId, String type)
47+
throws SQLException;
48+
4549
public List<ResourcePolicy> findByTypeGroupAction(Context context, DSpaceObject dso, Group group, int action)
4650
throws SQLException;
4751

@@ -242,5 +246,16 @@ public List<ResourcePolicy> findByGroupAndResourceUuid(Context context, Group gr
242246

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

249+
/**
250+
* Return a list of date valid policy owners for a list of object that match the
251+
* action.
252+
*
253+
* @param c context
254+
* @param dsoIds DSpaceObject ids policies relate to
255+
* @param actionID action (defined in class Constants)
256+
* @return list of resource policies
257+
* @throws SQLException if there's a database problem
258+
*/
259+
List<ResourcePolicyOwnerVO> findValidPolicyOwners(Context c, List<UUID> dsoIds, int actionID) throws SQLException;
245260

246261
}

dspace-api/src/main/java/org/dspace/authorize/dao/impl/ResourcePolicyDAOImpl.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
*/
88
package org.dspace.authorize.dao.impl;
99

10+
import static java.util.Collections.emptyList;
11+
1012
import java.sql.SQLException;
13+
import java.time.LocalDate;
1114
import java.util.Collections;
1215
import java.util.LinkedList;
1316
import java.util.List;
@@ -22,6 +25,7 @@
2225
import jakarta.persistence.criteria.Root;
2326
import org.apache.commons.collections.CollectionUtils;
2427
import org.dspace.authorize.ResourcePolicy;
28+
import org.dspace.authorize.ResourcePolicyOwnerVO;
2529
import org.dspace.authorize.ResourcePolicy_;
2630
import org.dspace.authorize.dao.ResourcePolicyDAO;
2731
import org.dspace.content.DSpaceObject;
@@ -106,6 +110,23 @@ public List<ResourcePolicy> findByDSoAndAction(Context context, DSpaceObject dso
106110
return list(context, criteriaQuery, false, ResourcePolicy.class, -1, -1);
107111
}
108112

113+
@Override
114+
public List<ResourcePolicy> findByDSoAndActionAndType(Context context, DSpaceObject dso, int actionId, String type)
115+
throws SQLException {
116+
117+
CriteriaBuilder builder = getCriteriaBuilder(context);
118+
CriteriaQuery<ResourcePolicy> criteriaQuery = getCriteriaQuery(builder, ResourcePolicy.class);
119+
Root<ResourcePolicy> resourcePolicyRoot = criteriaQuery.from(ResourcePolicy.class);
120+
121+
criteriaQuery.select(resourcePolicyRoot);
122+
123+
criteriaQuery.where(builder.and(builder.equal(resourcePolicyRoot.get(ResourcePolicy_.dSpaceObject), dso),
124+
builder.and(builder.equal(resourcePolicyRoot.get(ResourcePolicy_.actionId), actionId),
125+
builder.equal(resourcePolicyRoot.get(ResourcePolicy_.rptype), type))));
126+
127+
return list(context, criteriaQuery, false, ResourcePolicy.class, -1, -1);
128+
}
129+
109130
@Override
110131
public void deleteByDsoAndTypeAndAction(Context context, DSpaceObject dso, String type, int actionId)
111132
throws SQLException {
@@ -420,4 +441,29 @@ public ResourcePolicy findOneById(Context context, Integer id) throws SQLExcepti
420441
criteriaQuery.where(criteriaBuilder.equal(resourcePolicyRoot.get(ResourcePolicy_.id), id));
421442
return singleResult(context, criteriaQuery);
422443
}
444+
445+
@Override
446+
@SuppressWarnings("unchecked")
447+
public List<ResourcePolicyOwnerVO> findValidPolicyOwners(Context context, List<UUID> dsoIds, int actionID)
448+
throws SQLException {
449+
450+
if (CollectionUtils.isEmpty(dsoIds)) {
451+
return emptyList();
452+
}
453+
454+
String sqlQuery = ""
455+
+ " SELECT new org.dspace.authorize.ResourcePolicyOwnerVO(policy.eperson.id, policy.epersonGroup.id)"
456+
+ " FROM ResourcePolicy policy "
457+
+ " WHERE policy.dSpaceObject.id in (:dsoIds) "
458+
+ " AND policy.actionId = :actionId "
459+
+ " AND (policy.startDate is NULL OR policy.startDate <= :date)"
460+
+ " AND (policy.endDate is NULL OR policy.endDate >= :date)";
461+
462+
Query query = createQuery(context, sqlQuery);
463+
query.setParameter("dsoIds", dsoIds);
464+
query.setParameter("actionId", actionID);
465+
query.setParameter("date", LocalDate.now());
466+
return query.getResultList();
467+
468+
}
423469
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* The contents of this file are subject to the license and copyright
3+
* detailed in the LICENSE and NOTICE files at the root of the source
4+
* tree and available online at
5+
*
6+
* http://www.dspace.org/license/
7+
*/
8+
package org.dspace.authorize.relationship;
9+
10+
import org.dspace.content.Item;
11+
import org.dspace.content.RelationshipType;
12+
import org.dspace.core.Context;
13+
14+
/**
15+
* Interface for classes that check if a relationship of a specific type can be
16+
* created between two items.
17+
*
18+
* @author Luca Giamminonni (luca.giamminonni at 4science.it)
19+
*
20+
*/
21+
public interface RelationshipAuthorizer {
22+
23+
/**
24+
* Check if the current user is authorized to create/edit/delete a relationship
25+
* of the given type between the leftItem and the rigthItem.
26+
*
27+
* @param context The DSpace context
28+
* @param relationshipType the type of the relationship to be checked
29+
* @param leftItem the left item of the relationship
30+
* @param rightItem the right item of the relationship
31+
* @return true if the current user can create/edit or delete
32+
* the relationship, false otherwise
33+
*/
34+
boolean canHandleRelationship(Context context, RelationshipType relationshipType, Item leftItem, Item rightItem);
35+
36+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* The contents of this file are subject to the license and copyright
3+
* detailed in the LICENSE and NOTICE files at the root of the source
4+
* tree and available online at
5+
*
6+
* http://www.dspace.org/license/
7+
*/
8+
package org.dspace.authorize.relationship;
9+
10+
import static java.util.Objects.requireNonNull;
11+
import static org.springframework.util.Assert.notNull;
12+
13+
import org.dspace.content.EntityType;
14+
import org.dspace.content.Item;
15+
import org.dspace.content.RelationshipType;
16+
import org.dspace.core.Context;
17+
18+
/**
19+
* Default implementation of {@link RelationshipAuthorizer}.
20+
*
21+
* @author Luca Giamminonni (luca.giamminonni at 4science.it)
22+
*
23+
*/
24+
public class RelationshipAuthorizerImpl implements RelationshipAuthorizer {
25+
26+
private String leftEntityType;
27+
28+
private String leftwardType;
29+
30+
private String rightEntityType;
31+
32+
private String rightwardType;
33+
34+
private RelationshipItemAuthorizer leftItemAuthorizer;
35+
36+
private RelationshipItemAuthorizer rightItemAuthorizer;
37+
38+
private boolean andCondition;
39+
40+
public RelationshipAuthorizerImpl(RelationshipItemAuthorizer leftItemAuthorizer,
41+
RelationshipItemAuthorizer rightItemAuthorizer) {
42+
this.leftItemAuthorizer = requireNonNull(leftItemAuthorizer, "Left item authorizer required");
43+
this.rightItemAuthorizer = requireNonNull(rightItemAuthorizer, "Right item authorizer required");
44+
}
45+
46+
@Override
47+
public boolean canHandleRelationship(Context context,
48+
RelationshipType relationshipType, Item leftItem, Item rightItem) {
49+
50+
notNull(relationshipType, "The relationship type is required to handle a relationship");
51+
notNull(leftItem, "The left item is required to handle a relationship");
52+
notNull(rightItem, "The right item is required to handle a relationship");
53+
54+
if (notMatchesRelationshipType(relationshipType)) {
55+
return false;
56+
}
57+
58+
if (andCondition) {
59+
return leftItemAuthorizer.canHandleRelationshipOnItem(context, leftItem)
60+
&& rightItemAuthorizer.canHandleRelationshipOnItem(context, rightItem);
61+
} else {
62+
return leftItemAuthorizer.canHandleRelationshipOnItem(context, leftItem)
63+
|| rightItemAuthorizer.canHandleRelationshipOnItem(context, rightItem);
64+
}
65+
66+
}
67+
68+
private boolean notMatchesRelationshipType(RelationshipType relationshipType) {
69+
70+
if (leftEntityType != null && !leftEntityType.equals(getEntityTypeLabel(relationshipType.getLeftType()))) {
71+
return true;
72+
}
73+
74+
if (rightEntityType != null && !rightEntityType.equals(getEntityTypeLabel(relationshipType.getRightType()))) {
75+
return true;
76+
}
77+
78+
if (leftwardType != null && !leftwardType.equals(relationshipType.getLeftwardType())) {
79+
return true;
80+
}
81+
82+
if (rightwardType != null && !rightwardType.equals(relationshipType.getRightwardType())) {
83+
return true;
84+
}
85+
86+
return false;
87+
88+
}
89+
90+
private String getEntityTypeLabel(EntityType entityType) {
91+
return entityType != null ? entityType.getLabel() : null;
92+
}
93+
94+
public void setLeftEntityType(String leftEntityType) {
95+
this.leftEntityType = leftEntityType;
96+
}
97+
98+
public void setLeftwardType(String leftwardType) {
99+
this.leftwardType = leftwardType;
100+
}
101+
102+
public void setRightEntityType(String rightEntityType) {
103+
this.rightEntityType = rightEntityType;
104+
}
105+
106+
public void setRightwardType(String rightwardType) {
107+
this.rightwardType = rightwardType;
108+
}
109+
110+
public void setAndCondition(boolean andCondition) {
111+
this.andCondition = andCondition;
112+
}
113+
114+
115+
}

0 commit comments

Comments
 (0)