use of org.apereo.portal.security.IAuthorizationPrincipal in project uPortal by Jasig.
the class AuthorizationTester method testDoesPrincipalHavePermission.
public void testDoesPrincipalHavePermission() throws Exception {
print("***** ENTERING AuthorizationTester.testDoesPrincipalHavePermission() *****");
String msg = null;
IPermission testPermission = null;
boolean testResult = false;
int idx = 0;
msg = "Creating authorizationPrincipal for student.";
print(msg);
IAuthorizationPrincipal prin = getService().newPrincipal("student", IPERSON_CLASS);
assertNotNull(msg, prin);
testPermission = (IPermission) testPermissions.get(0);
msg = "Testing " + testPermission + " (should be TRUE -- inherited from Everyone)";
print(msg);
testResult = prin.hasPermission(OWNER, TEST_ACTIVITY, testPermission.getTarget());
assertTrue(msg, testResult);
testPermission = (IPermission) testPermissions.get(1);
msg = "Testing " + testPermission + " (should be FALSE -- directly denied)";
print(msg);
testResult = prin.hasPermission(OWNER, TEST_ACTIVITY, testPermission.getTarget());
assertTrue(msg, !testResult);
msg = "Testing the rest of the test permissions (should be TRUE).";
print(msg);
for (idx = 2; idx < NUMBER_TEST_PERMISSIONS; idx++) {
testPermission = (IPermission) testPermissions.get(idx);
testResult = prin.hasPermission(OWNER, TEST_ACTIVITY, testPermission.getTarget());
assertTrue(msg, testResult);
}
print("***** LEAVING AuthorizationTester.testDoesPrincipalHavePermission() *****" + CR);
}
use of org.apereo.portal.security.IAuthorizationPrincipal in project uPortal by Jasig.
the class GroupListHelperImpl method getPrincipalForEntity.
public IAuthorizationPrincipal getPrincipalForEntity(JsonEntityBean entity) {
// attempt to determine the entity type class for this principal
Class entityType;
EntityEnum jsonType = entity.getEntityType();
if (jsonType.isGroup()) {
entityType = IEntityGroup.class;
} else {
entityType = jsonType.getClazz();
}
// construct an authorization principal for this JsonEntityBean
AuthorizationService authService = AuthorizationService.instance();
IAuthorizationPrincipal p = authService.newPrincipal(entity.getId(), entityType);
return p;
}
use of org.apereo.portal.security.IAuthorizationPrincipal in project uPortal by Jasig.
the class GroupListHelperImpl method getIndividualBestRootEntity.
@Override
public JsonEntityBean getIndividualBestRootEntity(final IPerson person, final String groupType, final String permissionOwner, final String[] permissionActivities) {
if (log.isDebugEnabled()) {
log.debug("Choosing best root group for user='" + person.getUserName() + "', groupType='" + groupType + "', permissionOwner='" + permissionOwner + "', permissionActivities='" + Arrays.toString(permissionActivities) + "'");
}
final IAuthorizationPrincipal principal = AuthorizationPrincipalHelper.principalFromUser(person);
final JsonEntityBean canonicalRootGroup = getRootEntity(groupType);
if (log.isDebugEnabled()) {
log.debug("Found for groupType='" + groupType + "' the following canonicalRootGroup: " + canonicalRootGroup);
}
/*
* First check the canonical root group for the applicable activities
* (NOTE: the uPortal permissions infrastructure handles checking of
* special, collective targets like "ALL_GROUPS" and "All_categories").
*/
for (String activity : permissionActivities) {
if (principal.hasPermission(permissionOwner, activity, canonicalRootGroup.getId())) {
return canonicalRootGroup;
}
}
// So much for the easy path -- see if the user has any records at all for this specific owner/activity
// Default
JsonEntityBean rslt = null;
final List<IPermission> permissionsOfRelevantActivity = new ArrayList<IPermission>();
for (String activity : permissionActivities) {
permissionsOfRelevantActivity.addAll(Arrays.asList(principal.getAllPermissions(permissionOwner, activity, null)));
}
if (log.isDebugEnabled()) {
log.debug("For user='" + person.getUserName() + "', groupType='" + groupType + "', permissionOwner='" + permissionOwner + "', permissionActivities='" + Arrays.toString(permissionActivities) + "' permissionsOfRelevantTypes.size()=" + permissionsOfRelevantActivity.size());
}
switch(permissionsOfRelevantActivity.size()) {
case 0:
// No problem -- user doesn't have any of this sort of permission (leave it null)
break;
default:
// root group to send back. With luck there aren't many matches.
for (IPermission p : permissionsOfRelevantActivity) {
IEntityGroup groupMember = GroupService.findGroup(p.getTarget());
final JsonEntityBean candidate = getEntity(groupMember);
// Pass on any matches of the wrong groupType...
if (!candidate.getEntityTypeAsString().equalsIgnoreCase(groupType)) {
continue;
}
if (rslt == null) {
// First allowable selection; run with this one
// unless/until we're forced to make a choice.
rslt = candidate;
} else {
// the same rich hierarchy.
if (candidate.getChildren().size() > rslt.getChildren().size()) {
rslt = candidate;
}
}
}
break;
}
if (log.isDebugEnabled()) {
log.debug("Selected for user='" + person.getUserName() + "', groupType='" + groupType + "', permissionOwner='" + permissionOwner + "', permissionActivities='" + Arrays.toString(permissionActivities) + "' the following best root group: " + rslt);
}
return rslt;
}
use of org.apereo.portal.security.IAuthorizationPrincipal in project uPortal by Jasig.
the class ApiPermissionsService method getAssignmentsForPerson.
@Override
public Set<Assignment> getAssignmentsForPerson(String username, boolean includeInherited) {
Set<Assignment> rslt = new HashSet<Assignment>();
IAuthorizationPrincipal authP = this.authorizationService.newPrincipal(username, EntityEnum.PERSON.getClazz());
// first get the permissions explicitly set for this principal
IPermission[] directPermissions = permissionStore.select(null, authP.getPrincipalString(), null, null, null);
for (IPermission permission : directPermissions) {
if (authP.hasPermission(permission.getOwner(), permission.getActivity(), permission.getTarget())) {
Assignment a = createAssignment(permission, authP, false);
if (a != null) {
rslt.add(a);
}
}
}
if (includeInherited) {
IGroupMember member = GroupService.getGroupMember(authP.getKey(), authP.getType());
for (IEntityGroup parent : member.getAncestorGroups()) {
IAuthorizationPrincipal parentPrincipal = this.authorizationService.newPrincipal(parent);
IPermission[] parentPermissions = permissionStore.select(null, parentPrincipal.getPrincipalString(), null, null, null);
for (IPermission permission : parentPermissions) {
if (authP.hasPermission(permission.getOwner(), permission.getActivity(), permission.getTarget())) {
Assignment a = createAssignment(permission, authP, true);
if (a != null) {
rslt.add(a);
}
}
}
}
}
return rslt;
}
use of org.apereo.portal.security.IAuthorizationPrincipal in project uPortal by Jasig.
the class ApiPermissionsService method createAssignment.
/*
* Implementation
*/
private Assignment createAssignment(IPermission permission, IAuthorizationPrincipal authP, boolean inherited) {
Assignment rslt = null;
try {
// Owner
IPermissionOwner owner = permissionOwnerDao.getPermissionOwner(permission.getOwner());
Owner ownerImpl = new OwnerImpl(permission.getOwner(), owner.getName());
// Activity
IPermissionActivity activity = permissionOwnerDao.getPermissionActivity(permission.getOwner(), permission.getActivity());
Activity activityImpl = new ActivityImpl(permission.getActivity(), activity.getName());
// Principal
Principal principalImpl = new PrincipalImpl(authP.getKey(), authP.getPrincipalString());
// Target
// default
Target targetImpl = null;
IPermissionTargetProvider targetProvider = targetProviderRegistry.getTargetProvider(activity.getTargetProviderKey());
IPermissionTarget target = targetProvider.getTarget(permission.getTarget());
if (target != null) {
targetImpl = new TargetImpl(permission.getTarget(), target.getName());
}
rslt = new AssignmentImpl(ownerImpl, activityImpl, principalImpl, targetImpl, inherited);
} catch (Exception e) {
log.warn("Exception while adding permission", e);
}
return rslt;
}
Aggregations