Search in sources :

Example 51 with Session

use of org.apache.directory.fortress.core.model.Session in project directory-fortress-core by apache.

the class AccessMgrImplTest method createSessionsWithRoles.

/**
 * @param msg
 * @param uArray
 * @param rArray
 */
public static void createSessionsWithRoles(String msg, String[][] uArray, String[][] rArray) {
    LogUtil.logIt(msg);
    try {
        AccessMgr accessMgr = AccessMgrFactory.createInstance(TestUtils.getContext());
        for (String[] usr : uArray) {
            User user = UserTestData.getUser(usr);
            List<UserRole> rlsRequested = new ArrayList<>();
            int cnt = 0;
            for (String[] rle : rArray) {
                rlsRequested.add(RoleTestData.getUserRole(user.getUserId(), rle));
                user.setRoles(rlsRequested);
                Session session = accessMgr.createSession(user, false);
                assertTrue(CLS_NM + ".createSessionsWithRoles failed role search USER [" + user.getUserId() + "] CNT [" + ++cnt + "] size [" + session.getRoles().size() + "]", cnt == session.getRoles().size());
                String userId = accessMgr.getUserId(session);
                assertTrue(CLS_NM + ".createSessionsWithRoles failed compare found userId [" + userId + "] valid userId [" + UserTestData.getUserId(usr) + "]", userId.equalsIgnoreCase(UserTestData.getUserId(usr)));
                UserTestData.assertEquals(user, usr);
            }
        }
        LOG.debug("createSessionsWithRoles successful");
    } catch (SecurityException ex) {
        LOG.error("createSessionsWithRoles: failed with SecurityException rc=" + ex.getErrorId() + ", msg=" + ex.getMessage(), ex);
        fail(ex.getMessage());
    }
}
Also used : User(org.apache.directory.fortress.core.model.User) AccessMgr(org.apache.directory.fortress.core.AccessMgr) UserRole(org.apache.directory.fortress.core.model.UserRole) ArrayList(java.util.ArrayList) SecurityException(org.apache.directory.fortress.core.SecurityException) Session(org.apache.directory.fortress.core.model.Session)

Example 52 with Session

use of org.apache.directory.fortress.core.model.Session in project directory-fortress-core by apache.

the class AccessMgrImplTest method sessionPermissionsH.

/**
 * @param msg
 * @param uArray
 * @param oArray
 * @param opArray
 */
public static void sessionPermissionsH(String msg, String[][] uArray, String[][] oArray, String[][] opArray) {
    LogUtil.logIt(msg);
    try {
        AccessMgr accessMgr = AccessMgrFactory.createInstance(TestUtils.getContext());
        int i = 0;
        for (String[] usr : uArray) {
            i++;
            User user = UserTestData.getUser(usr);
            Session session = accessMgr.createSession(user, false);
            assertNotNull(session);
            List<Permission> pOps = accessMgr.sessionPermissions(session);
            assertNotNull(pOps);
            // LOG.warn("sessionPermissionsH list size user [" + user.getUserId() + "] expected len=" +
            // (11 - i) * opArray.length + " actual len=" + pOps.size());
            assertEquals(CLS_NM + ".sessionPermissionsH failed list size user[" + user.getUserId() + "]", (11 - i) * opArray.length, pOps.size());
            // Iterate over objs x ops, see if every expected valid permission is contained within the returned list:
            int j = 0;
            for (String[] obj : oArray) {
                j++;
                // positive tests:
                if (i == j || i < j) {
                    int k = 0;
                    for (String[] op : opArray) {
                        k++;
                        Permission validPOp = PermTestData.getOp(PermTestData.getName(obj), op);
                        assertTrue(CLS_NM + ".sessionPermissionsH failed perm list compare USER [" + user.getUserId() + "] PERM Obj [" + PermTestData.getName(obj) + "] " + "OPER [" + PermTestData.getName(op) + "]", pOps.contains(validPOp));
                        boolean result = accessMgr.checkAccess(session, new Permission(PermTestData.getName(obj), PermTestData.getName(op)));
                        assertTrue(CLS_NM + ".sessionPermissionsH failed checkAccess USER [" + user.getUserId() + "] PERM Obj [" + PermTestData.getName(obj) + "] " + "OPER [" + PermTestData.getName(op) + "]", result);
                    }
                } else // negative tests:
                {
                    int k = 0;
                    for (String[] op : opArray) {
                        k++;
                        Permission validPOp = PermTestData.getOp(PermTestData.getName(obj), op);
                        assertTrue(CLS_NM + ".sessionPermissionsH failed negative perm list compare USER [" + user.getUserId() + "] PERM Obj [" + PermTestData.getName(obj) + "] " + "OPER [" + PermTestData.getName(op) + "]", !pOps.contains(validPOp));
                        boolean result = accessMgr.checkAccess(session, new Permission(PermTestData.getName(obj), PermTestData.getName(op)));
                        assertTrue(CLS_NM + ".sessionPermissionsH failed negative checkAccess USER [" + user.getUserId() + "] PERM Obj [" + PermTestData.getName(obj) + "] " + "OPER [" + PermTestData.getName(op) + "]", !result);
                    }
                }
            }
        }
    } catch (SecurityException ex) {
        LOG.error("sessionPermissionsH: failed with SecurityException rc=" + ex.getErrorId() + ", msg=" + ex.getMessage(), ex);
        fail(ex.getMessage());
    }
}
Also used : User(org.apache.directory.fortress.core.model.User) AccessMgr(org.apache.directory.fortress.core.AccessMgr) Permission(org.apache.directory.fortress.core.model.Permission) SecurityException(org.apache.directory.fortress.core.SecurityException) Session(org.apache.directory.fortress.core.model.Session)

Example 53 with Session

use of org.apache.directory.fortress.core.model.Session in project directory-fortress-core by apache.

the class AccessMgrImplTest method sessionPermissions.

/**
 * @param msg
 * @param uArray
 * @param oArray
 * @param opArray
 */
public static void sessionPermissions(String msg, String[][] uArray, String[][] oArray, String[][] opArray) {
    LogUtil.logIt(msg);
    try {
        AccessMgr accessMgr = AccessMgrFactory.createInstance(TestUtils.getContext());
        for (String[] usr : uArray) {
            User user = UserTestData.getUser(usr);
            Session session = accessMgr.createSession(user, false);
            assertNotNull(session);
            List<Permission> pOps = accessMgr.sessionPermissions(session);
            assertNotNull(pOps);
            // There should be objs * ops number of perms in the list returned from sessionPermissions method:
            assertEquals(CLS_NM + ".sessionPermissions failed list size user[" + user.getUserId() + "]", oArray.length * opArray.length, pOps.size());
            // Iterate over objs x ops, see if every expected valid permission is contained within the returned list:
            for (String[] obj : oArray) {
                for (String[] op : opArray) {
                    Permission validPOp = PermTestData.getOp(PermTestData.getName(obj), op);
                    assertTrue(CLS_NM + ".sessionPermissions failed perm list compare USER [" + user.getUserId() + "] PERM Obj [" + PermTestData.getName(obj) + "] " + "OPER [" + PermTestData.getName(op) + "]", pOps.contains(validPOp));
                }
            }
        }
    } catch (SecurityException ex) {
        LOG.error("sessionPermissions: failed with SecurityException rc=" + ex.getErrorId() + ", msg=" + ex.getMessage(), ex);
        fail(ex.getMessage());
    }
}
Also used : User(org.apache.directory.fortress.core.model.User) AccessMgr(org.apache.directory.fortress.core.AccessMgr) Permission(org.apache.directory.fortress.core.model.Permission) SecurityException(org.apache.directory.fortress.core.SecurityException) Session(org.apache.directory.fortress.core.model.Session)

Example 54 with Session

use of org.apache.directory.fortress.core.model.Session in project directory-fortress-core by apache.

the class AccessMgrImplTest method createSessionsHier.

/**
 * @param msg
 * @param uArray
 */
public static void createSessionsHier(String msg, String[][] uArray) {
    LogUtil.logIt(msg);
    try {
        AccessMgr accessMgr = AccessMgrFactory.createInstance(TestUtils.getContext());
        for (String[] usr : uArray) {
            User user = UserTestData.getUser(usr);
            Session session = accessMgr.createSession(user, false);
            assertNotNull(session);
            String userId = accessMgr.getUserId(session);
            assertTrue(CLS_NM + ".createSessionsHier failed compare found userId [" + userId + "] valid userId [" + UserTestData.getUserId(usr) + "]", userId.equalsIgnoreCase(UserTestData.getUserId(usr)));
            UserTestData.assertEquals(user, usr);
            // Get the authorized roles for this user:
            Collection<String> authZRoles = UserTestData.getAuthorizedRoles(usr);
            // If there are any assigned roles, add them to list of authorized.
            Set<String> asgnRoles = UserTestData.getAssignedRoles(usr);
            assertNotNull(asgnRoles);
            assertTrue(asgnRoles.size() > 0);
            for (String asgnRole : asgnRoles) {
                authZRoles.add(asgnRole);
            }
            Set<String> actualRoles = accessMgr.authorizedRoles(session);
            assertNotNull(actualRoles);
            assertEquals(CLS_NM + ".createSessionsHier authorized roles list size test case failed for [" + user.getUserId() + "]", authZRoles.size(), actualRoles.size());
            for (String name : authZRoles) {
                assertTrue(CLS_NM + ".createSessionsHier authorized roles compare test case failed for USER [" + user.getUserId() + "] expect role [" + name + "] nout found", actualRoles.contains(name));
            }
        }
        LOG.debug("createSessionsHier successful");
    } catch (SecurityException ex) {
        LOG.error("createSessionsHier: failed with SecurityException rc=" + ex.getErrorId() + ", msg=" + ex.getMessage(), ex);
        fail(ex.getMessage());
    }
}
Also used : User(org.apache.directory.fortress.core.model.User) AccessMgr(org.apache.directory.fortress.core.AccessMgr) SecurityException(org.apache.directory.fortress.core.SecurityException) Session(org.apache.directory.fortress.core.model.Session)

Example 55 with Session

use of org.apache.directory.fortress.core.model.Session in project directory-fortress-core by apache.

the class DelegatedMgrImplTest method checkAccess.

public static void checkAccess(String msg, String[][] uArray, String[][] oArray, String[][] opArray, String[][] oArrayBad, String[][] opArrayBad) {
    LogUtil.logIt(msg);
    try {
        DelAccessMgr dAccessMgr = DelAccessMgrFactory.createInstance(TestUtils.getContext());
        AccessMgr accessMgr = (AccessMgr) dAccessMgr;
        for (String[] usr : uArray) {
            User user = UserTestData.getUser(usr);
            Session session = accessMgr.createSession(user, false);
            assertNotNull(session);
            int i = 0;
            for (String[] obj : oArray) {
                int j = 0;
                for (String[] op : opArray) {
                    // Call checkAccess method
                    assertTrue(CLS_NM + ".checkAccess failed userId [" + user.getUserId() + "] Perm objName [" + PermTestData.getName(obj) + "] operationName [" + PermTestData.getName(op) + "]", dAccessMgr.checkAccess(session, new Permission(PermTestData.getName(obj), PermTestData.getName(op))));
                    j++;
                }
                i++;
            }
            i = 0;
            for (String[] obj : oArrayBad) {
                int j = 0;
                for (String[] op : opArrayBad) {
                    // Call checkAccess method (this should fail):
                    try {
                        boolean result = dAccessMgr.checkAccess(session, new Permission(PermTestData.getName(oArrayBad[i]), PermTestData.getName(opArrayBad[j])));
                        assertTrue(CLS_NM + ".checkAccess failed userId [" + user.getUserId() + "] Perm objName [" + PermTestData.getName(oArrayBad[i]) + "] operationName [" + PermTestData.getName(opArrayBad[j]) + "]", !result);
                    } catch (SecurityException se) {
                        // The expected condition is security exception perm not exist:
                        assertTrue(CLS_NM + ".checkAccess failed userId [" + user.getUserId() + "] Perm objName [" + PermTestData.getName(oArrayBad[i]) + "] operationName [" + PermTestData.getName(opArrayBad[j]) + "], negative use case, incorrect exception id=" + se.getErrorId(), se.getErrorId() == GlobalErrIds.PERM_NOT_EXIST);
                    }
                    j++;
                }
                i++;
            }
        }
        LOG.debug("checkAccess successful");
    } catch (SecurityException ex) {
        LOG.error("checkAccess: caught SecurityException rc=" + ex.getErrorId() + ", msg: " + ex.getMessage(), ex);
        fail(ex.getMessage());
    }
}
Also used : User(org.apache.directory.fortress.core.model.User) DelAccessMgr(org.apache.directory.fortress.core.DelAccessMgr) AccessMgr(org.apache.directory.fortress.core.AccessMgr) Permission(org.apache.directory.fortress.core.model.Permission) SecurityException(org.apache.directory.fortress.core.SecurityException) DelAccessMgr(org.apache.directory.fortress.core.DelAccessMgr) Session(org.apache.directory.fortress.core.model.Session)

Aggregations

Session (org.apache.directory.fortress.core.model.Session)70 SecurityException (org.apache.directory.fortress.core.SecurityException)62 User (org.apache.directory.fortress.core.model.User)51 AccessMgr (org.apache.directory.fortress.core.AccessMgr)32 UserRole (org.apache.directory.fortress.core.model.UserRole)28 AccelMgr (org.apache.directory.fortress.core.AccelMgr)12 Permission (org.apache.directory.fortress.core.model.Permission)12 FortRequest (org.apache.directory.fortress.core.model.FortRequest)10 FortResponse (org.apache.directory.fortress.core.model.FortResponse)10 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 DelAccessMgr (org.apache.directory.fortress.core.DelAccessMgr)4 UserAdminRole (org.apache.directory.fortress.core.model.UserAdminRole)4 PasswordException (org.apache.directory.fortress.core.PasswordException)3 lombok.val (lombok.val)2 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)2 org.apache.directory.fortress.core (org.apache.directory.fortress.core)2 AdminRole (org.apache.directory.fortress.core.model.AdminRole)2 ObjectFactory (org.apache.directory.fortress.core.model.ObjectFactory)2 Role (org.apache.directory.fortress.core.model.Role)2