Search in sources :

Example 76 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.

the class DelegatingSubject method popIdentity.

private PrincipalCollection popIdentity() {
    PrincipalCollection popped = null;
    List<PrincipalCollection> stack = getRunAsPrincipalsStack();
    if (!CollectionUtils.isEmpty(stack)) {
        popped = stack.remove(0);
        Session session;
        if (!CollectionUtils.isEmpty(stack)) {
            // persist the changed stack to the session
            session = getSession();
            session.setAttribute(RUN_AS_PRINCIPALS_SESSION_KEY, stack);
        } else {
            // stack is empty, remove it from the session:
            clearRunAsIdentities();
        }
    }
    return popped;
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) ProxiedSession(org.apache.shiro.session.ProxiedSession) Session(org.apache.shiro.session.Session)

Example 77 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.

the class TextConfigurationRealmTest method testHasRole.

/*
     * Tests that roles can't be read while the realm is being loaded. 
     */
@Test
public void testHasRole() throws InterruptedException {
    setUpForReadConfigurationTest();
    executeTest(new Runnable() {

        public void run() {
            PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
            assertTrue("principal doesn't have role when it should", realm.hasRole(principalCollection, "role2"));
            assertTrue("principal doesn't have all roles when it should", realm.hasAllRoles(principalCollection, Arrays.asList(new String[] { "role1", "role2" })));
        }
    });
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) Test(org.junit.Test)

Example 78 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.

the class TextConfigurationRealmTest method testCheckPermission.

/*
     * Tests that a principal's permissions can't be checked while the realm is being loaded. 
     */
@Test
public void testCheckPermission() throws InterruptedException {
    setUpForReadConfigurationTest();
    executeTest(new Runnable() {

        public void run() {
            PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
            try {
                realm.checkPermission(principalCollection, "role1_permission1");
                realm.checkPermissions(principalCollection, new String[] { "role1_permission1", "role2_permission2" });
            } catch (AuthorizationException ae) {
                fail("principal doesn't have permission when it should");
            }
        }
    });
}
Also used : AuthorizationException(org.apache.shiro.authz.AuthorizationException) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) Test(org.junit.Test)

Example 79 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.

the class TextConfigurationRealmTest method testIsPermitted.

/*
     * Tests that a principal's permissions can't be checked while the realm is being loaded. 
     */
@Test
public void testIsPermitted() throws InterruptedException {
    setUpForReadConfigurationTest();
    executeTest(new Runnable() {

        public void run() {
            PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
            assertTrue("permission not permitted when it should be", realm.isPermitted(principalCollection, "role1_permission1"));
            assertTrue("permission not permitted when it should be", realm.isPermittedAll(principalCollection, new String[] { "role1_permission1", "role2_permission2" }));
        }
    });
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) Test(org.junit.Test)

Example 80 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.

the class AccountInfoController method home.

@RequiresRoles("admin")
@RequestMapping("/account-info")
public String home(Model model) {
    String name = "World";
    Subject subject = SecurityUtils.getSubject();
    PrincipalCollection principalCollection = subject.getPrincipals();
    if (principalCollection != null && !principalCollection.isEmpty()) {
        name = principalCollection.getPrimaryPrincipal().toString();
    }
    model.addAttribute("name", name);
    return "account-info";
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Subject(org.apache.shiro.subject.Subject) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)88 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)40 Test (org.junit.Test)36 SecurityAssertion (ddf.security.assertion.SecurityAssertion)23 Subject (ddf.security.Subject)15 Subject (org.apache.shiro.subject.Subject)15 Principal (java.security.Principal)14 ArrayList (java.util.ArrayList)10 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)10 AuthorizationInfo (org.apache.shiro.authz.AuthorizationInfo)9 Permission (org.apache.shiro.authz.Permission)8 Session (org.apache.shiro.session.Session)8 SimpleSession (org.apache.shiro.session.mgt.SimpleSession)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Attribute (ddf.security.assertion.Attribute)5 Map (java.util.Map)5 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)5 CollectionPermission (ddf.security.permission.CollectionPermission)4