Search in sources :

Example 1 with BackendException

use of org.apache.felix.useradmin.BackendException in project felix by apache.

the class RoleRepository method removeRoleFromAllGroups.

/**
 * Removes a given role as (required)member from any groups it is member of.
 *
 * @param removedRole the role that is removed from the store already, cannot be <code>null</code>.
 * @throws BackendException in case of problems accessing the store.
 */
private void removeRoleFromAllGroups(Role removedRole) {
    try {
        Role[] roles = m_store.getRoles(null);
        for (int i = 0; i < roles.length; i++) {
            if (roles[i].getType() == Role.GROUP) {
                Group group = (Group) roles[i];
                // Don't check whether the given role is actually a member
                // of the group, but let the group itself figure this out...
                group.removeMember(removedRole);
            }
        }
    } catch (Exception e) {
        throw new BackendException("Failed to get all roles!", e);
    }
}
Also used : ObservableRole(org.apache.felix.useradmin.impl.role.ObservableRole) Role(org.osgi.service.useradmin.Role) Group(org.osgi.service.useradmin.Group) BackendException(org.apache.felix.useradmin.BackendException) BackendException(org.apache.felix.useradmin.BackendException)

Example 2 with BackendException

use of org.apache.felix.useradmin.BackendException in project felix by apache.

the class RoleRepository method getRoles.

/**
 * Returns a collection with all roles matching a given key-value pair.
 *
 * @param key the key to search for;
 * @param value the value to search for.
 * @return a list with all matching roles, can be empty, but never <code>null</code>.
 */
public List getRoles(String key, String value) {
    if (key == null) {
        throw new IllegalArgumentException("Key cannot be null!");
    }
    if (value == null) {
        throw new IllegalArgumentException("Value cannot be null!");
    }
    List matchingRoles = new ArrayList();
    try {
        String criteria = "(".concat(key).concat("=").concat(value).concat(")");
        Role[] roles = m_store.getRoles(criteria);
        for (int i = 0; i < roles.length; i++) {
            Role role = roles[i];
            if (!isPredefinedRole(role.getName())) {
                matchingRoles.add(wireChangeListener(role));
            }
        }
    } catch (Exception e) {
        throw new BackendException("Failed to get roles!", e);
    }
    return matchingRoles;
}
Also used : ObservableRole(org.apache.felix.useradmin.impl.role.ObservableRole) Role(org.osgi.service.useradmin.Role) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) List(java.util.List) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BackendException(org.apache.felix.useradmin.BackendException) BackendException(org.apache.felix.useradmin.BackendException)

Example 3 with BackendException

use of org.apache.felix.useradmin.BackendException in project felix by apache.

the class RoleRepository method removeRole.

/**
 * Removes a given role from this manager.
 *
 * @param role the role to remove, cannot be <code>null</code>.
 * @return <code>true</code> if the role was removed (i.e., it was managed by this manager), or <code>false</code> if it was not found.
 */
public boolean removeRole(String name) {
    if (name == null) {
        throw new IllegalArgumentException("Name cannot be null!");
    }
    checkPermissions();
    // Cannot remove predefined roles...
    if (isPredefinedRole(name)) {
        return false;
    }
    try {
        Role result = m_store.removeRole(name);
        if (result != null) {
            // FELIX-3755: Remove the role as (required)member from all groups...
            removeRoleFromAllGroups(result);
            unwireChangeListener(result);
            m_roleChangeReflector.roleRemoved(result);
            return true;
        }
        return false;
    } catch (Exception e) {
        throw new BackendException("Failed to remove role " + name + "!", e);
    }
}
Also used : ObservableRole(org.apache.felix.useradmin.impl.role.ObservableRole) Role(org.osgi.service.useradmin.Role) BackendException(org.apache.felix.useradmin.BackendException) BackendException(org.apache.felix.useradmin.BackendException)

Example 4 with BackendException

use of org.apache.felix.useradmin.BackendException in project felix by apache.

the class RoleRepository method addRole.

/**
 * Adds a given role to this manager.
 *
 * @param role the role to add, cannot be <code>null</code>. If it is already contained by this manager, this method will not do anything.
 * @return the given role if added, <code>null</code> otherwise.
 */
public Role addRole(String name, int type) {
    if ((name == null) || "".equals(name.trim())) {
        throw new IllegalArgumentException("Name cannot be null or empty!");
    }
    if (type != Role.GROUP && type != Role.USER) {
        throw new IllegalArgumentException("Invalid role type!");
    }
    checkPermissions();
    try {
        Role result = m_store.addRole(name, type);
        if (result != null) {
            result = wireChangeListener(result);
            m_roleChangeReflector.roleAdded(result);
        }
        return result;
    } catch (Exception e) {
        throw new BackendException("Adding role " + name + " failed!", e);
    }
}
Also used : ObservableRole(org.apache.felix.useradmin.impl.role.ObservableRole) Role(org.osgi.service.useradmin.Role) BackendException(org.apache.felix.useradmin.BackendException) BackendException(org.apache.felix.useradmin.BackendException)

Aggregations

BackendException (org.apache.felix.useradmin.BackendException)4 ObservableRole (org.apache.felix.useradmin.impl.role.ObservableRole)4 Role (org.osgi.service.useradmin.Role)4 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Group (org.osgi.service.useradmin.Group)1