use of com.sun.identity.entitlement.OrSubject in project OpenAM by OpenRock.
the class PolicyConditionUpgraderTest method shouldMigratePolicyWithOrSubjectCondition.
@SuppressWarnings("unchecked")
@Test
public void shouldMigratePolicyWithOrSubjectCondition() throws EntitlementException, UpgradeException {
//Given
Privilege policy = mock(Privilege.class);
OrSubject orSubject = mock(OrSubject.class);
Set<EntitlementSubject> orSubjects = new HashSet<EntitlementSubject>();
PolicySubject subject1 = mock(PolicySubject.class);
PolicySubject subject2 = mock(PolicySubject.class);
orSubjects.add(subject1);
orSubjects.add(subject2);
EntitlementSubject migratedSubject1 = mock(EntitlementSubject.class);
EntitlementSubject migratedSubject2 = mock(EntitlementSubject.class);
given(policy.getSubject()).willReturn(orSubject);
given(orSubject.getESubjects()).willReturn(orSubjects);
given(subject1.getClassName()).willReturn("SUBJECT1_CLASS_NAME");
given(subject2.getClassName()).willReturn("SUBJECT2_CLASS_NAME");
given(conditionUpgradeMap.migrateSubjectCondition(eq("SUBJECT1_CLASS_NAME"), eq(subject1), Matchers.<MigrationReport>anyObject())).willReturn(migratedSubject1);
given(conditionUpgradeMap.migrateSubjectCondition(eq("SUBJECT2_CLASS_NAME"), eq(subject2), Matchers.<MigrationReport>anyObject())).willReturn(migratedSubject2);
//When
conditionUpgrader.dryRunPolicyUpgrade(policy);
//Then
ArgumentCaptor<Set> subjectCaptor = ArgumentCaptor.forClass(Set.class);
verify(orSubject).setESubjects(subjectCaptor.capture());
assertThat(subjectCaptor.getValue()).hasSize(2).contains(migratedSubject1, migratedSubject2);
verify(policy, never()).setSubject(Matchers.<EntitlementSubject>anyObject());
verify(policy, never()).setCondition(Matchers.<EntitlementCondition>anyObject());
}
use of com.sun.identity.entitlement.OrSubject in project OpenAM by OpenRock.
the class PolicyConditionUpgraderTest method isPolicyWithOrSubjectConditionUpgradable.
@Test(dataProvider = "isPolicyWithOrSubjectConditionUpgradableDataProvider")
public void isPolicyWithOrSubjectConditionUpgradable(Class<? extends EntitlementSubject> sub1, boolean sub1InMap, Class<? extends EntitlementSubject> sub2, boolean sub2InMap, boolean expectedResult) {
//Given
Privilege policy = mock(Privilege.class);
OrSubject orSubject = mock(OrSubject.class);
Set<EntitlementSubject> orSubjects = new HashSet<EntitlementSubject>();
EntitlementSubject subject1 = mock(sub1);
EntitlementSubject subject2 = mock(sub2);
orSubjects.add(subject1);
orSubjects.add(subject2);
given(policy.getSubject()).willReturn(orSubject);
given(orSubject.getESubjects()).willReturn(orSubjects);
if (subject1 instanceof PolicySubject) {
given(((PolicySubject) subject1).getClassName()).willReturn("SUBJECT1_CLASS_NAME");
}
if (subject2 instanceof PolicySubject) {
given(((PolicySubject) subject2).getClassName()).willReturn("SUBJECT2_CLASS_NAME");
}
given(conditionUpgradeMap.containsSubjectCondition("SUBJECT1_CLASS_NAME")).willReturn(sub1InMap);
given(conditionUpgradeMap.containsSubjectCondition("SUBJECT2_CLASS_NAME")).willReturn(sub2InMap);
//When
boolean upgradable = conditionUpgrader.isPolicyUpgradable(policy);
//Then
assertThat(upgradable).isEqualTo(expectedResult);
}
use of com.sun.identity.entitlement.OrSubject in project OpenAM by OpenRock.
the class PrivilegeUtils method toEntitlementSubject.
private static EntitlementSubject toEntitlementSubject(Policy policy) throws PolicyException {
Set<String> subjectNames = policy.getSubjectNames();
Set<EntitlementSubject> entitlementSubjects = new HashSet<EntitlementSubject>();
if (subjectNames != null) {
for (String subjectName : subjectNames) {
Subject subject = policy.getSubject(subjectName);
boolean exclusive = policy.isSubjectExclusive(subjectName);
boolean dealtWith = false;
if (!dealtWith) {
EntitlementSubject sbj = mapGenericSubject(subjectName, subject, exclusive);
if (sbj != null) {
entitlementSubjects.add(sbj);
}
}
}
}
if (entitlementSubjects.isEmpty()) {
return null;
}
return (entitlementSubjects.size() == 1) ? entitlementSubjects.iterator().next() : new OrSubject(entitlementSubjects);
}
use of com.sun.identity.entitlement.OrSubject in project OpenAM by OpenRock.
the class OpenSSOApplicationPrivilegeManager method toPrivilege.
/**
* Creates two privileges here
*/
private Privilege[] toPrivilege(ApplicationPrivilege appPrivilege) throws EntitlementException {
Privilege[] results = new Privilege[2];
try {
Privilege actualP = Privilege.getNewInstance();
actualP.setName(appPrivilege.getName());
actualP.setDescription(appPrivilege.getDescription());
Set<String> res = createDelegationResources(appPrivilege);
Entitlement entitlement = new Entitlement(APPL_NAME, res, getActionValues(appPrivilege.getActionValues()));
actualP.setEntitlement(entitlement);
Privilege ghostP = Privilege.getNewInstance();
ghostP.setName(GHOST_PRIVILEGE_NAME_PREFIX + appPrivilege.getName());
Set<String> ghostRes = new HashSet<String>();
String currentOrgDN = DNMapper.orgNameToDN(realm);
Object[] param = { currentOrgDN };
ghostRes.add(MessageFormat.format(SUN_AM_REALM_RESOURCE, param));
ghostRes.add(MessageFormat.format(SUN_IDREPO_RESOURCE, param));
entitlement = new Entitlement(APPL_NAME, ghostRes, getActionValues(ApplicationPrivilege.PossibleAction.READ));
ghostP.setEntitlement(entitlement);
Set<SubjectImplementation> subjects = appPrivilege.getSubjects();
Set<EntitlementSubject> eSubjects = new HashSet<EntitlementSubject>();
for (SubjectImplementation i : subjects) {
eSubjects.add((EntitlementSubject) i);
}
OrSubject orSubject = new OrSubject(eSubjects);
actualP.setSubject(orSubject);
actualP.setCondition(appPrivilege.getCondition());
ghostP.setSubject(orSubject);
ghostP.setCondition(appPrivilege.getCondition());
Set<String> applIndexes = new HashSet<String>();
applIndexes.addAll(appPrivilege.getApplicationNames());
actualP.setApplicationIndexes(applIndexes);
results[0] = actualP;
results[1] = ghostP;
} catch (UnsupportedEncodingException ex) {
String[] params = {};
throw new EntitlementException(324, params);
}
return results;
}
use of com.sun.identity.entitlement.OrSubject in project OpenAM by OpenRock.
the class OpenSSOApplicationPrivilegeManager method toApplicationPrivilege.
private ApplicationPrivilege toApplicationPrivilege(Privilege p) throws EntitlementException {
ApplicationPrivilege ap = new ApplicationPrivilege(p.getName());
ap.setDescription(p.getDescription());
ap.setCreatedBy(p.getCreatedBy());
ap.setCreationDate(p.getCreationDate());
ap.setLastModifiedBy(p.getLastModifiedBy());
ap.setLastModifiedDate(p.getLastModifiedDate());
Entitlement ent = p.getEntitlement();
Set<String> resourceNames = ent.getResourceNames();
Map<String, Set<String>> mapAppToRes = getApplicationPrivilegeResourceNames(resourceNames);
ap.setApplicationResources(mapAppToRes);
ap.setActionValues(getActionValues(ent.getActionValues()));
Set<SubjectImplementation> subjects = new HashSet<SubjectImplementation>();
if (p.getSubject() instanceof OrSubject) {
OrSubject orSubject = (OrSubject) p.getSubject();
for (EntitlementSubject es : orSubject.getESubjects()) {
if (es instanceof SubjectImplementation) {
subjects.add((SubjectImplementation) es);
}
}
} else if (p.getSubject() instanceof SubjectImplementation) {
subjects.add((SubjectImplementation) p.getSubject());
}
ap.setSubject(subjects);
EntitlementCondition cond = p.getCondition();
if (cond instanceof SimpleTimeCondition) {
ap.setCondition(cond);
}
return ap;
}
Aggregations