Search in sources :

Example 31 with Account

use of org.exist.security.Account in project exist by eXist-db.

the class RpcConnection method updateAccount.

/**
 * Added by {Marco.Tampucci, Massimo.Martinelli} @isti.cnr.it
 *
 * modified by Chris Tomlinson based on above updateAccount - it appears
 * that this code can rely on the SecurityManager to enforce policy about
 * whether user is or is not permitted to update the Account with name.
 *
 * This is called via RemoteUserManagementService.addUserGroup(Account)
 *
 * @param name user name to update
 * @param groups list of groups the user is added to
 * @return true, if action succeeded
 */
@Override
public boolean updateAccount(final String name, final List<String> groups) {
    try {
        return withDb((broker, transaction) -> {
            final SecurityManager manager = broker.getBrokerPool().getSecurityManager();
            Account u;
            if (!manager.hasAccount(name)) {
                u = new UserAider(name);
            } else {
                u = manager.getAccount(name);
            }
            for (final String g : groups) {
                if (!u.hasGroup(g)) {
                    u.addGroup(g);
                }
            }
            return manager.updateAccount(u);
        });
    } catch (final EXistException | PermissionDeniedException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("addUserGroup encountered error", e);
        }
        return false;
    }
}
Also used : Account(org.exist.security.Account) SecurityManager(org.exist.security.SecurityManager) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException) UserAider(org.exist.security.internal.aider.UserAider)

Example 32 with Account

use of org.exist.security.Account in project exist by eXist-db.

the class ListUsersTask method execute.

/* (non-Javadoc)
     * @see org.apache.tools.ant.Task#execute()
     */
public void execute() throws BuildException {
    super.execute();
    try {
        log("Listing all users", Project.MSG_DEBUG);
        final Account[] users = service.getAccounts();
        if (users != null) {
            boolean isFirst = true;
            final StringBuilder buffer = new StringBuilder();
            for (final Account user : users) {
                // only insert separator for 2nd or later item
                if (isFirst) {
                    isFirst = false;
                } else {
                    buffer.append(separator);
                }
                buffer.append(user.getName());
            }
            if (buffer.length() > 0) {
                log("Setting output property " + outputproperty + " to " + buffer.toString(), Project.MSG_DEBUG);
                getProject().setNewProperty(outputproperty, buffer.toString());
            }
        }
    } catch (final XMLDBException e) {
        final String msg = "XMLDB exception caught: " + e.getMessage();
        if (failonerror) {
            throw (new BuildException(msg, e));
        } else {
            log(msg, e, Project.MSG_ERR);
        }
    }
}
Also used : Account(org.exist.security.Account) XMLDBException(org.xmldb.api.base.XMLDBException) BuildException(org.apache.tools.ant.BuildException)

Example 33 with Account

use of org.exist.security.Account in project exist by eXist-db.

the class RpcConnection method getAccounts.

@Override
public List<Map<String, Object>> getAccounts() throws EXistException, PermissionDeniedException {
    final java.util.Collection<Account> users = factory.getBrokerPool().getSecurityManager().getUsers();
    final List<Map<String, Object>> result = new ArrayList<>();
    for (final Account user : users) {
        result.add(toMap(user));
    }
    return result;
}
Also used : Account(org.exist.security.Account) java.util(java.util) org.exist.util(org.exist.util) LockedDocumentMap(org.exist.storage.lock.LockedDocumentMap)

Example 34 with Account

use of org.exist.security.Account in project exist by eXist-db.

the class RpcConnection method getGroup.

@Override
public Map<String, Object> getGroup(final String name) throws EXistException, PermissionDeniedException {
    return withDb((broker, transaction) -> {
        final SecurityManager securityManager = factory.getBrokerPool().getSecurityManager();
        final Group group = securityManager.getGroup(name);
        if (group != null) {
            final Map<String, Object> map = new HashMap<>();
            map.put("id", group.getId());
            map.put("realmId", group.getRealmId());
            map.put("name", name);
            final List<Account> groupManagers = group.getManagers();
            final List<String> managers = new ArrayList<>(groupManagers.size());
            for (final Account groupManager : groupManagers) {
                managers.add(groupManager.getName());
            }
            map.put("managers", managers);
            final Map<String, String> metadata = new HashMap<>();
            for (final SchemaType key : group.getMetadataKeys()) {
                metadata.put(key.getNamespace(), group.getMetadataValue(key));
            }
            map.put("metadata", metadata);
            return map;
        }
        return null;
    });
}
Also used : Group(org.exist.security.Group) Account(org.exist.security.Account) SecurityManager(org.exist.security.SecurityManager) EXistSchemaType(org.exist.security.EXistSchemaType) SchemaType(org.exist.security.SchemaType) AXSchemaType(org.exist.security.AXSchemaType)

Example 35 with Account

use of org.exist.security.Account in project exist by eXist-db.

the class RpcConnection method setUserPrimaryGroup.

@Override
public boolean setUserPrimaryGroup(final String username, final String groupName) throws EXistException, PermissionDeniedException {
    final SecurityManager manager = factory.getBrokerPool().getSecurityManager();
    if (!manager.hasGroup(groupName)) {
        throw new EXistException("Group '" + groupName + "' does not exist!");
    }
    if (!manager.hasAdminPrivileges(user)) {
        throw new PermissionDeniedException("Not allowed to modify user");
    }
    withDb((broker, transaction) -> {
        final Account account = manager.getAccount(username);
        final Group group = manager.getGroup(groupName);
        account.setPrimaryGroup(group);
        manager.updateAccount(account);
        return null;
    });
    return true;
}
Also used : Account(org.exist.security.Account) Group(org.exist.security.Group) SecurityManager(org.exist.security.SecurityManager) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException)

Aggregations

Account (org.exist.security.Account)60 PermissionDeniedException (org.exist.security.PermissionDeniedException)18 SecurityManager (org.exist.security.SecurityManager)17 EXistException (org.exist.EXistException)12 XMLDBException (org.xmldb.api.base.XMLDBException)11 Group (org.exist.security.Group)10 Collection (org.xmldb.api.base.Collection)10 AuthenticationException (org.exist.security.AuthenticationException)9 DBBroker (org.exist.storage.DBBroker)9 AbstractAccount (org.exist.security.AbstractAccount)7 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)7 UserAider (org.exist.security.internal.aider.UserAider)6 Txn (org.exist.storage.txn.Txn)6 DocumentImpl (org.exist.dom.persistent.DocumentImpl)5 Subject (org.exist.security.Subject)5 UserManagementService (org.exist.xmldb.UserManagementService)5 Permission (org.exist.security.Permission)4 XPathException (org.exist.xquery.XPathException)4 Before (org.junit.Before)4 Test (org.junit.Test)4