use of org.apache.directory.fortress.core.AccessMgr in project directory-fortress-core by apache.
the class AccessMgrSample method testSessionPermissions.
/**
* The sessionPermissions API is useful for GUI programs that need to cache all of the User's Permissions in the
* HTTP Session or application cache. This is useful when providing access control lists for menu items and other
* controls that sometimes need to check authorizations on. This API will return all permissions that are granted
* to User's activated Roles along with Permissions that have been granted directly to the User entity itself.
*/
public static void testSessionPermissions() {
String szLocation = ".testSessionPermissions";
try {
// Instantiate the AccessMgr implementation.
AccessMgr accessMgr = AccessMgrFactory.createInstance(TestUtils.getContext());
// utility function will create an Fortress Session. The Session contains the user's activated
// roles along with other related attributes and status information (i.e. password status)
Session session = createSession(CreateUserSample.TEST_USERID, CreateUserSample.TEST_PASSWORD, accessMgr);
assertNotNull(session);
List<Permission> perms = accessMgr.sessionPermissions(session);
assertNotNull(perms);
assertTrue(szLocation + " list check, expected: 5, actual:" + perms.size(), perms.size() == 5);
// iterate over expected permissions to make sure they are returned from sessionPermissions API.
for (int i = 1; i < 6; i++) {
// A Permission consists of an object name and operation name.
Permission checkPerm = new Permission(CreatePermSample.TEST_PERM_OBJECT, CreatePermSample.TEST_PERM_OPERATION_PREFIX + i);
boolean result = accessMgr.checkAccess(session, checkPerm);
assertTrue(szLocation, result);
LOG.info(szLocation + " user [" + session.getUserId() + "] permission object [" + checkPerm.getObjName() + "] operation name [" + checkPerm.getOpName() + "] success");
}
} catch (SecurityException ex) {
LOG.error(szLocation + " caught SecurityException rc=" + ex.getErrorId() + ", msg=" + ex.getMessage(), ex);
fail(ex.getMessage());
}
}
Aggregations