Example usage for org.apache.shiro.realm AuthorizingRealm isPermitted

List of usage examples for org.apache.shiro.realm AuthorizingRealm isPermitted

Introduction

In this page you can find the example usage for org.apache.shiro.realm AuthorizingRealm isPermitted.

Prototype

protected boolean[] isPermitted(List<Permission> permissions, AuthorizationInfo info) 

Source Link

Usage

From source file:ddf.catalog.plugin.resource.usage.TestResourceUsagePlugin.java

License:Open Source License

private ResourceRequest getMockResourceRequest(String resourceSize, String expectedUsername) {

    AuthorizingRealm realm = mock(AuthorizingRealm.class);

    when(realm.getName()).thenReturn("mockRealm");
    when(realm.isPermitted(any(PrincipalCollection.class), any(Permission.class))).thenReturn(true);

    Collection<Realm> realms = new ArrayList<Realm>();
    realms.add(realm);/*  w  w w .jav a 2  s.co m*/

    DefaultSecurityManager manager = new DefaultSecurityManager();
    manager.setRealms(realms);

    SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(new Principal() {
        @Override
        public String getName() {
            return expectedUsername;
        }

        @Override
        public String toString() {
            return expectedUsername;
        }
    }, realm.getName());

    subject = new MockSubject(manager, principalCollection);

    ResourceRequest resourceRequest = mock(ResourceRequest.class);
    Map<String, Serializable> requestProperties = new HashMap<>();

    requestProperties.put(SecurityConstants.SECURITY_SUBJECT, subject);

    requestProperties.put(Metacard.RESOURCE_SIZE, resourceSize);
    when(resourceRequest.getPropertyNames()).thenReturn(requestProperties.keySet());
    when(resourceRequest.getPropertyValue(SecurityConstants.SECURITY_SUBJECT)).thenReturn(subject);
    when(resourceRequest.getPropertyValue(Metacard.RESOURCE_SIZE)).thenReturn(resourceSize);
    return resourceRequest;
}

From source file:ddf.catalog.security.filter.plugin.test.FilterPluginTest.java

License:Open Source License

@Before
public void setup() {
    AuthorizingRealm realm = mock(AuthorizingRealm.class);

    when(realm.getName()).thenReturn("mockRealm");
    when(realm.isPermitted(any(PrincipalCollection.class), any(Permission.class))).then(makeDecision());

    Collection<org.apache.shiro.realm.Realm> realms = new ArrayList<>();
    realms.add(realm);//from w ww .  jav  a  2  s .co m

    DefaultSecurityManager manager = new DefaultSecurityManager();
    manager.setRealms(realms);
    SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(new Principal() {
        @Override
        public String getName() {
            return "testuser";
        }
    }, realm.getName());
    Subject systemSubject = new MockSubject(manager, principalCollection);

    plugin = new FilterPlugin() {
        @Override
        protected Subject getSystemSubject() {
            return systemSubject;
        }
    };
    QueryRequestImpl request = getSampleRequest();
    Map<String, Serializable> properties = new HashMap<>();

    Subject subject = new MockSubject(manager, principalCollection);
    properties.put(SecurityConstants.SECURITY_SUBJECT, subject);
    request.setProperties(properties);

    incomingResponse = new QueryResponseImpl(request);
    ResourceRequest resourceRequest = mock(ResourceRequest.class);
    when(resourceRequest.getProperties()).thenReturn(properties);
    resourceResponse = new ResourceResponseImpl(resourceRequest, mock(Resource.class));
    resourceResponse.setProperties(properties);

    DeleteRequest deleteRequest = mock(DeleteRequest.class);
    when(deleteRequest.getProperties()).thenReturn(properties);
    List<Metacard> deletedMetacards = new ArrayList<>();
    deletedMetacards.add(getExactRolesMetacard());
    deleteResponse = new DeleteResponseImpl(deleteRequest, properties, deletedMetacards);

    List<Metacard> badDeletedMetacards = new ArrayList<>();
    badDeletedMetacards.add(getMoreRolesMetacard());
    badDeleteResponse = new DeleteResponseImpl(deleteRequest, properties, badDeletedMetacards);

    createRequest = new CreateRequestImpl(getExactRolesMetacard());
    createRequest.setProperties(properties);

    badCreateRequest = new CreateRequestImpl(getMoreRolesMetacard());
    badCreateRequest.setProperties(properties);

    updateRequest = new UpdateRequestImpl(getExactRolesMetacard().getId(), getExactRolesMetacard());
    updateRequest.setProperties(properties);

    ResultImpl result1 = new ResultImpl(getMoreRolesMetacard());
    ResultImpl result2 = new ResultImpl(getMissingRolesMetacard());
    ResultImpl result3 = new ResultImpl(getExactRolesMetacard());
    ResultImpl result4 = new ResultImpl(getNoRolesMetacard());
    ResultImpl result5 = new ResultImpl(getNoSecurityAttributeMetacard());
    incomingResponse.addResult(result1, false);
    incomingResponse.addResult(result2, false);
    incomingResponse.addResult(result3, false);
    incomingResponse.addResult(result4, false);
    incomingResponse.addResult(result5, true);
}

From source file:ddf.catalog.security.operation.plugin.OperationPluginTest.java

License:Open Source License

@Before
public void setup() {
    plugin = new OperationPlugin();

    AuthorizingRealm realm = mock(AuthorizingRealm.class);

    when(realm.getName()).thenReturn("mockRealm");
    when(realm.isPermitted(any(PrincipalCollection.class), any(Permission.class))).then(makeDecision());

    Collection<Realm> realms = new ArrayList<Realm>();
    realms.add(realm);//from   ww  w  . j  a va2s. com

    DefaultSecurityManager manager = new DefaultSecurityManager();
    manager.setRealms(realms);

    SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(new Principal() {
        @Override
        public String getName() {
            return "testuser";
        }
    }, realm.getName());

    subject = new MockSubject(manager, principalCollection);
}

From source file:org.codice.ddf.resourcemanagement.usage.ResourceUsagePluginTest.java

License:Open Source License

private void setSubject(String expectedUsername) {
    AuthorizingRealm realm = mock(AuthorizingRealm.class);
    when(realm.getName()).thenReturn("mockRealm");
    when(realm.isPermitted(any(PrincipalCollection.class), any(Permission.class))).thenReturn(true);
    Collection<Realm> realms = new ArrayList<>();
    realms.add(realm);/*w  w  w . j  a  v a 2  s.  c o m*/
    DefaultSecurityManager manager = new DefaultSecurityManager();
    manager.setRealms(realms);
    SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(new Principal() {
        @Override
        public String getName() {
            return expectedUsername;
        }

        @Override
        public String toString() {
            return expectedUsername;
        }
    }, realm.getName());

    subject = new MockSubject(manager, principalCollection);
}