Search in sources :

Example 1 with User

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

the class UserManager method addCredential.

public void addCredential(String key, Object value, String username) throws IllegalArgumentException {
    try {
        if (value instanceof Byte[]) {
            Byte[] ByteValue = (Byte[]) value;
            byte[] primitive = new byte[ByteValue.length];
            for (int i = 0; i < ByteValue.length; i++) primitive[i] = ByteValue[i].byteValue();
            value = primitive;
        } else if (!(value instanceof String) && !(value instanceof byte[]))
            throw new IllegalArgumentException("Credentials can only be byte[] or String");
        User user = (User) ac.getUserAdmin().getRole(username);
        user.getCredentials().put(key, value);
    } catch (NullPointerException npe) {
        ac.debug("UserAdmin not available. ");
    }
}
Also used : User(org.osgi.service.useradmin.User)

Example 2 with User

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

the class RoleRepositorySerializer method writeRepository.

/**
 * Writes the given repository to the given output stream.
 *
 * @param repository the repository to write, cannot be <code>null</code>;
 * @param dos the output stream to write the data to, cannot be <code>null</code>.
 * @throws IOException in case of I/O problems.
 */
private void writeRepository(Map repository, DataOutputStream dos) throws IOException {
    Collection values = repository.values();
    Iterator valuesIter = values.iterator();
    // Write the total number of entries in our repository first...
    dos.writeInt(values.size());
    while (valuesIter.hasNext()) {
        Role role = (Role) valuesIter.next();
        int type = role.getType();
        dos.writeInt(type);
        if (Role.GROUP == type) {
            writeGroup((Group) role, dos);
        } else if (Role.USER == type) {
            writeUser((User) role, dos);
        } else {
            writeRole(role, dos);
        }
    }
}
Also used : Role(org.osgi.service.useradmin.Role) User(org.osgi.service.useradmin.User) Iterator(java.util.Iterator) Collection(java.util.Collection)

Example 3 with User

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

the class RoleRepositorySerializer method readUser.

/**
 * Reads a user from the given input stream.
 *
 * @param dis the input stream to read the data from, cannot be <code>null</code>.
 * @return the read user, never <code>null</code>.
 * @throws IOException in case of I/O problems.
 */
private User readUser(DataInputStream dis) throws IOException {
    User user = (User) RoleFactory.createRole(Role.USER, dis.readUTF());
    readDictionary(user.getProperties(), dis);
    readDictionary(user.getCredentials(), dis);
    return user;
}
Also used : User(org.osgi.service.useradmin.User)

Example 4 with User

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

the class RoleRepositorySerializerTest method createUser.

private User createUser(int idx) {
    String name = "User" + idx;
    User result = RoleFactory.createUser(name);
    setCredentials(result);
    setProperties(result);
    return result;
}
Also used : User(org.osgi.service.useradmin.User)

Example 5 with User

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

the class FileStoreInitializationTest method testStoreIsInitializedAndClosedProperlyOk.

/**
 * Tests that initialization and closing of the repository store is
 * performed correctly.
 */
@Test
public void testStoreIsInitializedAndClosedProperlyOk() throws Exception {
    UserAdmin ua = getUserAdmin();
    // Start the file store bundle...
    Bundle fileStoreBundle = getFileStoreBundle();
    fileStoreBundle.start();
    // Create two roles...
    User user = (User) ua.createRole("user1", Role.USER);
    assertNotNull(user);
    Group group = (Group) ua.createRole("group1", Role.GROUP);
    assertNotNull(group);
    group.addMember(user);
    group.addRequiredMember(ua.getRole(Role.USER_ANYONE));
    // Stop the file store; should persist the two roles...
    fileStoreBundle.stop();
    // Wait a little until the bundle is really stopped...
    Thread.sleep(100);
    // Retrieve the roles again; should both yield null due to the store not being available...
    user = (User) ua.getRole("user1");
    assertNull(user);
    group = (Group) ua.getRole("group1");
    assertNull(group);
    // This will not succeed: no backend to store the user in...
    assertNull(ua.createRole("user2", Role.USER));
    fileStoreBundle.start();
    awaitService(ORG_APACHE_FELIX_USERADMIN_FILESTORE);
    // Retrieve the roles again; should both yield valid values...
    user = (User) ua.getRole("user1");
    assertNotNull(user);
    group = (Group) ua.getRole("group1");
    assertNotNull(group);
    Role[] members = group.getMembers();
    assertNotNull(members);
    assertEquals(1, members.length);
    assertEquals("user1", members[0].getName());
    members = group.getRequiredMembers();
    assertNotNull(members);
    assertEquals(1, members.length);
    assertEquals(Role.USER_ANYONE, members[0].getName());
    user = (User) ua.getRole("user2");
    assertNull(user);
}
Also used : Role(org.osgi.service.useradmin.Role) 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)

Aggregations

User (org.osgi.service.useradmin.User)74 Group (org.osgi.service.useradmin.Group)32 Test (org.junit.Test)26 Hashtable (java.util.Hashtable)9 Role (org.osgi.service.useradmin.Role)9 Authorization (org.osgi.service.useradmin.Authorization)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 CompositeData (javax.management.openmbean.CompositeData)5 UserAdmin (org.osgi.service.useradmin.UserAdmin)4 IOException (java.io.IOException)3 TabularData (javax.management.openmbean.TabularData)3 Bundle (org.osgi.framework.Bundle)3 BasicDBObject (com.mongodb.BasicDBObject)2 Dictionary (java.util.Dictionary)2 UserData (org.apache.aries.jmx.codec.UserData)2 Collection (java.util.Collection)1 Enumeration (java.util.Enumeration)1 Iterator (java.util.Iterator)1 List (java.util.List)1 TabularDataSupport (javax.management.openmbean.TabularDataSupport)1