Search in sources :

Example 46 with Role

use of org.osgi.service.useradmin.Role in project felix by apache.

the class RoleRepositorySerializer method resolveGroupMembers.

/**
 * Resolves all basic and required group members for a given group, based on the names from the given stub group.
 *
 * @param stubGroup the stub group to convert, cannot be <code>null</code>;
 * @param repository the repository to take the roles from, cannot be <code>null</code>.
 * @return a concrete {@link Group} instance with all members resolved, or <code>null</code> if not all members could be resolved.
 * @throws IOException in case a referenced role was not found in the repository.
 */
private void resolveGroupMembers(StubGroupImpl stubGroup, Group group, Map repository) throws IOException {
    List names = stubGroup.getMemberNames();
    int size = names.size();
    for (int i = 0; i < size; i++) {
        String name = (String) names.get(i);
        Role role = getRoleFromRepository(repository, name);
        if (role == null) {
            throw new IOException("Unable to find referenced basic member: " + name);
        }
        group.addMember(role);
    }
    names = stubGroup.getRequiredMemberNames();
    size = names.size();
    for (int i = 0; i < size; i++) {
        String name = (String) names.get(i);
        Role role = getRoleFromRepository(repository, name);
        if (role == null) {
            throw new IOException("Unable to find referenced required member: " + name);
        }
        group.addRequiredMember(role);
    }
}
Also used : Role(org.osgi.service.useradmin.Role) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Example 47 with Role

use of org.osgi.service.useradmin.Role in project felix by apache.

the class MongoDBStoreTest method testFelix4399_FetchEmptyRoleOk.

/**
 * Tests that fetching an empty role without properties or other roles does not cause a NPE.
 */
@Test
public void testFelix4399_FetchEmptyRoleOk() throws Exception {
    UserAdmin ua = getUserAdmin();
    String roleName = "emptyRole";
    if (canRunTest()) {
        Role emptyRole = ua.createRole(roleName, Role.USER);
        assertNotNull("Collection not empty?!", emptyRole);
        Role readRole = ua.getRole(roleName);
        assertNotNull("Unable to read back created empty role?!", readRole);
        assertEquals("Names not equal?!", emptyRole.getName(), readRole.getName());
        assertEquals("Types not equal?!", emptyRole.getType(), readRole.getType());
        Role[] readRoles = ua.getRoles(null);
        assertNotNull("Unable to read back created empty role?!", readRoles);
        assertEquals(1, readRoles.length);
    }
}
Also used : Role(org.osgi.service.useradmin.Role) UserAdmin(org.osgi.service.useradmin.UserAdmin) Test(org.junit.Test)

Example 48 with Role

use of org.osgi.service.useradmin.Role in project felix by apache.

the class MongoDBStoreTest method testRemoveRoleOk.

/**
 * Tests that removing a role works correctly.
 */
@Test
public void testRemoveRoleOk() throws Exception {
    UserAdmin ua = getUserAdmin();
    String roleName = "newRole";
    Role[] readRoles;
    if (canRunTest()) {
        Role role = ua.createRole(roleName, Role.USER);
        assertNotNull("Collection not empty?!", role);
        readRoles = ua.getRoles(null);
        assertNotNull("No roles stored?!", readRoles);
        assertEquals(1, readRoles.length);
        ua.removeRole(roleName);
        readRoles = ua.getRoles(null);
        assertNull("Still roles stored?!", readRoles);
    }
}
Also used : Role(org.osgi.service.useradmin.Role) UserAdmin(org.osgi.service.useradmin.UserAdmin) Test(org.junit.Test)

Example 49 with Role

use of org.osgi.service.useradmin.Role in project felix by apache.

the class MongoDBStoreTest method testUpdateRoleOk.

/**
 * Tests that removing a role works correctly.
 */
@Test
public void testUpdateRoleOk() throws Exception {
    UserAdmin ua = getUserAdmin();
    String roleName = "role1";
    Role[] readRoles;
    if (canRunTest()) {
        User role = (User) ua.createRole(roleName, Role.USER);
        assertNotNull("Collection not empty?!", role);
        readRoles = ua.getRoles(null);
        assertNotNull("No roles stored?!", readRoles);
        assertEquals(1, readRoles.length);
        role.getProperties().put("key", "value");
        // Wait a little to ensure everything is written...
        Thread.sleep(100);
        readRoles = ua.getRoles("(key=value)");
        assertNotNull("Role not updated?!", readRoles);
        assertEquals(1, readRoles.length);
    }
}
Also used : Role(org.osgi.service.useradmin.Role) User(org.osgi.service.useradmin.User) UserAdmin(org.osgi.service.useradmin.UserAdmin) Test(org.junit.Test)

Example 50 with Role

use of org.osgi.service.useradmin.Role in project felix by apache.

the class MongoDBStore method removeRole.

@Override
public Role removeRole(String roleName) throws MongoException {
    DBCollection coll = getCollection();
    Role role = getRole(roleName);
    if (role == null) {
        return null;
    }
    WriteResult result = coll.remove(getTemplateObject(role));
    if (result.getLastError() != null) {
        result.getLastError().throwOnError();
    }
    return role;
}
Also used : Role(org.osgi.service.useradmin.Role) DBCollection(com.mongodb.DBCollection) WriteResult(com.mongodb.WriteResult)

Aggregations

Role (org.osgi.service.useradmin.Role)98 Group (org.osgi.service.useradmin.Group)29 IOException (java.io.IOException)17 CountDownLatch (java.util.concurrent.CountDownLatch)13 List (java.util.List)9 User (org.osgi.service.useradmin.User)9 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)6 Collection (java.util.Collection)5 UserAdmin (org.osgi.service.useradmin.UserAdmin)5 DBCollection (com.mongodb.DBCollection)4 Iterator (java.util.Iterator)4 BackendException (org.apache.felix.useradmin.BackendException)4 ObservableRole (org.apache.felix.useradmin.impl.role.ObservableRole)4 Authorization (org.osgi.service.useradmin.Authorization)4 WriteResult (com.mongodb.WriteResult)3 Filter (org.osgi.framework.Filter)3 BasicDBObject (com.mongodb.BasicDBObject)2 DBObject (com.mongodb.DBObject)2 Dictionary (java.util.Dictionary)2