Search in sources :

Example 6 with UserAdmin

use of org.osgi.service.useradmin.UserAdmin 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 7 with UserAdmin

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

the class UserAdminIntegrationTest method testFelix3735_StopRunningStoreRetainsDataOk.

/**
 * Tests that stopping a filled store and starting it again will cause it to
 * properly restore its state.
 */
@Test
public void testFelix3735_StopRunningStoreRetainsDataOk() throws Exception {
    final String userName = "testUser";
    final String groupName = "testGroup";
    UserAdmin userAdmin = awaitService(UserAdmin.class.getName());
    Bundle fileStoreBundle = getFileStoreBundle();
    // Start a suitable storage service...
    fileStoreBundle.start();
    // Fill the user admin with some data...
    User testUser = (User) userAdmin.createRole(userName, Role.USER);
    testUser.getProperties().put("key", "value");
    Group testGroup = (Group) userAdmin.createRole(groupName, Role.GROUP);
    testGroup.addMember(testUser);
    // Stop the file store...
    fileStoreBundle.stop();
    // retrieve the useradmin again...
    userAdmin = awaitService(UserAdmin.class.getName());
    // Verify the user + group are gone (no store available)...
    assertNull(userAdmin.getRole(userName));
    assertNull(userAdmin.getRole(groupName));
    // Start the file store...
    fileStoreBundle.start();
    // Verify the user + group are gone (no store available)...
    User readUser = (User) userAdmin.getRole(userName);
    assertNotNull(readUser);
    assertEquals(userName, readUser.getName());
    assertEquals("value", readUser.getProperties().get("key"));
    Group readGroup = (Group) userAdmin.getRole(groupName);
    assertNotNull(readGroup);
    assertEquals(groupName, readGroup.getName());
    assertEquals(1, readGroup.getMembers().length);
    assertEquals(readUser, readGroup.getMembers()[0]);
}
Also used : Group(org.osgi.service.useradmin.Group) User(org.osgi.service.useradmin.User) UserAdmin(org.osgi.service.useradmin.UserAdmin) Bundle(org.osgi.framework.Bundle) Test(org.junit.Test)

Example 8 with UserAdmin

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

the class UserAdminIntegrationTest method testFelix3735_StartStoreAfterUserAdminInitializesOk.

/**
 * Tests that starting the file store <em>after</em> the user admin service
 * is started will cause it to be properly initialized.
 */
@Test
public void testFelix3735_StartStoreAfterUserAdminInitializesOk() throws Exception {
    final String userName = "anotherTestUser";
    final String groupName = "anotherTestGroup";
    UserAdmin userAdmin = awaitService(UserAdmin.class.getName());
    Bundle fileStoreBundle = getFileStoreBundle();
    // Start a suitable storage service...
    fileStoreBundle.start();
    // Fill the user admin with some data...
    User testUser = (User) userAdmin.createRole(userName, Role.USER);
    testUser.getProperties().put("key", "value");
    Group testGroup = (Group) userAdmin.createRole(groupName, Role.GROUP);
    testGroup.addMember(testUser);
    // Stop the file store...
    fileStoreBundle.stop();
    Bundle userAdminBundle = findBundle(ORG_APACHE_FELIX_USERADMIN);
    assertNotNull(userAdminBundle);
    userAdminBundle.stop();
    // Obtain user admin service again; shouldn't be available...
    userAdmin = getService(UserAdmin.class.getName());
    assertNull(userAdmin);
    userAdminBundle.start();
    // Obtain user admin service again; should be available now...
    userAdmin = awaitService(UserAdmin.class.getName());
    assertNotNull(userAdmin);
    // Verify the user + group are gone (no store available)...
    assertNull(userAdmin.getRole(userName));
    assertNull(userAdmin.getRole(groupName));
    // Start the file store...
    fileStoreBundle.start();
    // Verify the user + group are gone (no store available)...
    User readUser = (User) userAdmin.getRole(userName);
    assertNotNull(readUser);
    assertEquals(userName, readUser.getName());
    assertEquals("value", readUser.getProperties().get("key"));
    Group readGroup = (Group) userAdmin.getRole(groupName);
    assertNotNull(readGroup);
    assertEquals(groupName, readGroup.getName());
    assertEquals(1, readGroup.getMembers().length);
    assertEquals(readUser, readGroup.getMembers()[0]);
}
Also used : Group(org.osgi.service.useradmin.Group) User(org.osgi.service.useradmin.User) UserAdmin(org.osgi.service.useradmin.UserAdmin) Bundle(org.osgi.framework.Bundle) Test(org.junit.Test)

Example 9 with UserAdmin

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

the class UserAdminEventToEventAdminBridge method roleChanged.

public void roleChanged(final UserAdminEvent event) {
    final ServiceReference ref = m_context.getServiceReference(EventAdmin.class.getName());
    if (null != ref) {
        final EventAdmin eventAdmin = (EventAdmin) m_context.getService(ref);
        if (null != eventAdmin) {
            final String topic;
            switch(event.getType()) {
                case UserAdminEvent.ROLE_CHANGED:
                    topic = "org/osgi/service/useradmin/UserAdmin/ROLE_CHANGED";
                    break;
                case UserAdminEvent.ROLE_CREATED:
                    topic = "org/osgi/service/useradmin/UserAdmin/ROLE_CREATED";
                    break;
                case UserAdminEvent.ROLE_REMOVED:
                    topic = "org/osgi/service/useradmin/UserAdmin/ROLE_REMOVED";
                    break;
                default:
                    m_context.ungetService(ref);
                    return;
            }
            final Hashtable properties = new Hashtable();
            properties.put(EventConstants.EVENT, event);
            properties.put("role", event.getRole());
            properties.put("role.name", event.getRole().getName());
            properties.put("role.type", new Integer(event.getRole().getType()));
            final ServiceReference eventRef = event.getServiceReference();
            if (null == eventRef) {
                throw new IllegalArgumentException("UserAdminEvent.getServiceReference() may not be null");
            }
            properties.put(EventConstants.SERVICE, eventRef);
            properties.put(EventConstants.SERVICE_ID, eventRef.getProperty(EventConstants.SERVICE_ID));
            final Object objectClass = eventRef.getProperty(Constants.OBJECTCLASS);
            if (!(objectClass instanceof String[]) || !Arrays.asList((String[]) objectClass).contains(UserAdmin.class.getName())) {
                throw new IllegalArgumentException("Bad objectclass: " + objectClass);
            }
            properties.put(EventConstants.SERVICE_OBJECTCLASS, objectClass);
            properties.put(EventConstants.SERVICE_PID, eventRef.getProperty(EventConstants.SERVICE_PID));
            eventAdmin.postEvent(new Event(topic, properties));
            m_context.ungetService(ref);
        }
    }
}
Also used : UserAdmin(org.osgi.service.useradmin.UserAdmin) EventAdmin(org.osgi.service.event.EventAdmin) Hashtable(java.util.Hashtable) UserAdminEvent(org.osgi.service.useradmin.UserAdminEvent) Event(org.osgi.service.event.Event) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

UserAdmin (org.osgi.service.useradmin.UserAdmin)9 Test (org.junit.Test)7 Role (org.osgi.service.useradmin.Role)5 User (org.osgi.service.useradmin.User)4 Bundle (org.osgi.framework.Bundle)3 Group (org.osgi.service.useradmin.Group)3 Hashtable (java.util.Hashtable)1 SimpleWebConsolePlugin (org.apache.felix.webconsole.SimpleWebConsolePlugin)1 ServiceReference (org.osgi.framework.ServiceReference)1 Event (org.osgi.service.event.Event)1 EventAdmin (org.osgi.service.event.EventAdmin)1 UserAdminEvent (org.osgi.service.useradmin.UserAdminEvent)1