Skip to content

Commit

Permalink
MID-8965 request access wizard improvements, now disabling add to car…
Browse files Browse the repository at this point in the history
…t button when every poi in cart already has assignment
  • Loading branch information
1azyman committed Oct 2, 2023
1 parent 51459d0 commit b166603
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public void addPersonOfInterest(List<ObjectReferenceType> refs, Map<ObjectRefere
}

/**
* Matching will be done only based on targetRef (oid and relation)
* Matching will be done only based on targetRef (oid, type and relation)
*/
private boolean matchAssignments(AssignmentType one, AssignmentType two) {
return matchAssignments(one, two, false);
Expand Down Expand Up @@ -1050,20 +1050,20 @@ public boolean canAddTemplateAssignment(ObjectReferenceType newTargetRef) {

private boolean canAddTemplateAssignment(ObjectReferenceType newTargetRef, AssignmentConstraintsType constraints) {
boolean allowSameTarget = isAllowSameTarget(constraints);
// boolean allowSameRelation = isAllowSameRelation(constraints);
//
// // if everyone in "shopping cart" has already this assignment, we can't add it again
// boolean allPoiHasMatching = true;
// for (List<ObjectReferenceType> memberships : existingPoiRoleMemberships.values()) {
// boolean found = memberships.stream().anyMatch(m -> referencesEqual(newTargetRef, m, allowSameTarget, allowSameRelation));
// if (!found) {
// allPoiHasMatching = false;
// }
// }
//
// if (allPoiHasMatching) {
// return false;
// }
boolean allowSameRelation = isAllowSameRelation(constraints);

// if everyone in "shopping cart" has already this assignment, we can't add it again
boolean allPoiHasMatching = true;
for (List<ObjectReferenceType> memberships : existingPoiRoleMemberships.values()) {
boolean found = memberships.stream().anyMatch(m -> referencesEqual(newTargetRef, m, allowSameTarget, allowSameRelation));
if (!found) {
allPoiHasMatching = false;
}
}

if (allPoiHasMatching) {
return false;
}

// if there's already "template" assignment (picked one by user in role catalog) we can't add it again
return getTemplateAssignments().stream()
Expand Down Expand Up @@ -1105,12 +1105,16 @@ private boolean referencesEqual(ObjectReferenceType one, ObjectReferenceType two
return false;
}

// oid & type already equal here
if (!ignoreTarget) {
// we'll compare whole reference
return true;
}

return ignoreRelation || Objects.equals(one.getRelation(), two.getRelation());
if (ignoreRelation) {
return !ignoreTarget;
}

return Objects.equals(one.getRelation(), two.getRelation());
}

public int getPoiCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ public void test200SameRelationNotAllowed() throws Exception {
access.addPersonOfInterest(POI_1, List.of(ROLE_EXISTING));

// try to add assignment with same oid and relation -> should fail
addAndAssertAssignment(access, ROLE_EXISTING_OID, SchemaConstants.ORG_DEFAULT, true, 1, 1);
addAndAssertAssignment(access, ROLE_EXISTING_OID, SchemaConstants.ORG_DEFAULT, false, 0, 0);

// try to add assignment with same oid but different relation -> should be ok
addAndAssertAssignment(access, ROLE_EXISTING_OID, SchemaConstants.ORG_APPROVER, true, 2, 1);
addAndAssertAssignment(access, ROLE_EXISTING_OID, SchemaConstants.ORG_APPROVER, true, 1, 1);

// try to add assignment with different oid and same relation -> should be ok
addAndAssertAssignment(access, ROLE_1_OID, SchemaConstants.ORG_DEFAULT, true, 3, 1);
addAndAssertAssignment(access, ROLE_1_OID, SchemaConstants.ORG_DEFAULT, true, 2, 1);

// try to add assignment with different oid and relation -> should be ok
addAndAssertAssignment(access, ROLE_2_OID, SchemaConstants.ORG_APPROVER, true, 4, 1);
addAndAssertAssignment(access, ROLE_2_OID, SchemaConstants.ORG_APPROVER, true, 3, 1);
}

private void addAndAssertAssignment(
Expand Down Expand Up @@ -135,16 +135,16 @@ public void test300SameTargetOidNotAllowed() throws Exception {
access.addPersonOfInterest(POI_1, List.of(ROLE_EXISTING));

// try to add assignment with same oid and relation -> should fail
addAndAssertAssignment(access, ROLE_EXISTING_OID, SchemaConstants.ORG_DEFAULT, true, 1, 1);
addAndAssertAssignment(access, ROLE_EXISTING_OID, SchemaConstants.ORG_DEFAULT, false, 0, 0);

// try to add assignment with same oid but different relation -> should be ok
addAndAssertAssignment(access, ROLE_EXISTING_OID, SchemaConstants.ORG_APPROVER, false, 1, 0);
addAndAssertAssignment(access, ROLE_EXISTING_OID, SchemaConstants.ORG_APPROVER, false, 0, 0);

// try to add assignment with different oid and same relation -> should fail
addAndAssertAssignment(access, ROLE_1_OID, SchemaConstants.ORG_DEFAULT, true, 2, 1);
addAndAssertAssignment(access, ROLE_1_OID, SchemaConstants.ORG_DEFAULT, true, 1, 1);

// try to add assignment with different oid and relation -> should be ok
addAndAssertAssignment(access, ROLE_2_OID, SchemaConstants.ORG_APPROVER, true, 3, 1);
addAndAssertAssignment(access, ROLE_2_OID, SchemaConstants.ORG_APPROVER, true, 2, 1);
}

@Test
Expand All @@ -156,18 +156,16 @@ public void test400SameRelationAndTargetOidNotAllowed() throws Exception {
access.addPersonOfInterest(POI_1, List.of(ROLE_EXISTING));

// try to add assignment with same oid and relation -> should fail
addAndAssertAssignment(access, ROLE_EXISTING_OID, SchemaConstants.ORG_DEFAULT, true, 1, 1);

addAndAssertAssignment(access, ROLE_EXISTING_OID, SchemaConstants.ORG_DEFAULT, false, 1, 1);
addAndAssertAssignment(access, ROLE_EXISTING_OID, SchemaConstants.ORG_DEFAULT, false, 0, 0);

// try to add assignment with same oid but different relation -> should fail
addAndAssertAssignment(access, ROLE_EXISTING_OID, SchemaConstants.ORG_APPROVER, false, 1, 0);
addAndAssertAssignment(access, ROLE_EXISTING_OID, SchemaConstants.ORG_APPROVER, false, 0, 0);

// try to add assignment with different oid and same relation -> should be ok
addAndAssertAssignment(access, ROLE_1_OID, SchemaConstants.ORG_DEFAULT, true, 2, 1);
addAndAssertAssignment(access, ROLE_1_OID, SchemaConstants.ORG_DEFAULT, true, 1, 1);

// try to add assignment with different oid and relation -> should be ok
addAndAssertAssignment(access, ROLE_2_OID, SchemaConstants.ORG_APPROVER, true, 3, 1);
addAndAssertAssignment(access, ROLE_2_OID, SchemaConstants.ORG_APPROVER, true, 2, 1);
}

private void updateSystemConfiguration(Boolean allowSameRelation, Boolean allowSameTarget) throws Exception {
Expand Down

0 comments on commit b166603

Please sign in to comment.