Search in sources :

Example 6 with Tenants

use of org.apache.nifi.authorization.file.tenants.generated.Tenants in project nifi by apache.

the class FileUserGroupProvider method addGroup.

@Override
public synchronized Group addGroup(Group group) throws AuthorizationAccessException {
    if (group == null) {
        throw new IllegalArgumentException("Group cannot be null");
    }
    final UserGroupHolder holder = userGroupHolder.get();
    final Tenants tenants = holder.getTenants();
    // create a new JAXB Group based on the incoming Group
    final org.apache.nifi.authorization.file.tenants.generated.Group jaxbGroup = new org.apache.nifi.authorization.file.tenants.generated.Group();
    jaxbGroup.setIdentifier(group.getIdentifier());
    jaxbGroup.setName(group.getName());
    // add each user to the group
    for (String groupUser : group.getUsers()) {
        org.apache.nifi.authorization.file.tenants.generated.Group.User jaxbGroupUser = new org.apache.nifi.authorization.file.tenants.generated.Group.User();
        jaxbGroupUser.setIdentifier(groupUser);
        jaxbGroup.getUser().add(jaxbGroupUser);
    }
    tenants.getGroups().getGroup().add(jaxbGroup);
    saveAndRefreshHolder(tenants);
    return userGroupHolder.get().getGroupsById().get(group.getIdentifier());
}
Also used : Tenants(org.apache.nifi.authorization.file.tenants.generated.Tenants)

Example 7 with Tenants

use of org.apache.nifi.authorization.file.tenants.generated.Tenants in project nifi by apache.

the class FileUserGroupProvider method updateGroup.

@Override
public synchronized Group updateGroup(Group group) throws AuthorizationAccessException {
    if (group == null) {
        throw new IllegalArgumentException("Group cannot be null");
    }
    final UserGroupHolder holder = userGroupHolder.get();
    final Tenants tenants = holder.getTenants();
    // find the group that needs to be update
    org.apache.nifi.authorization.file.tenants.generated.Group updateGroup = null;
    for (org.apache.nifi.authorization.file.tenants.generated.Group jaxbGroup : tenants.getGroups().getGroup()) {
        if (jaxbGroup.getIdentifier().equals(group.getIdentifier())) {
            updateGroup = jaxbGroup;
            break;
        }
    }
    // if the group wasn't found return null, otherwise update the group and save changes
    if (updateGroup == null) {
        return null;
    }
    // reset the list of users and add each user to the group
    updateGroup.getUser().clear();
    for (String groupUser : group.getUsers()) {
        org.apache.nifi.authorization.file.tenants.generated.Group.User jaxbGroupUser = new org.apache.nifi.authorization.file.tenants.generated.Group.User();
        jaxbGroupUser.setIdentifier(groupUser);
        updateGroup.getUser().add(jaxbGroupUser);
    }
    updateGroup.setName(group.getName());
    saveAndRefreshHolder(tenants);
    return userGroupHolder.get().getGroupsById().get(group.getIdentifier());
}
Also used : Tenants(org.apache.nifi.authorization.file.tenants.generated.Tenants)

Example 8 with Tenants

use of org.apache.nifi.authorization.file.tenants.generated.Tenants in project nifi by apache.

the class FileUserGroupProvider method unmarshallTenants.

private Tenants unmarshallTenants() throws JAXBException {
    final Unmarshaller unmarshaller = JAXB_TENANTS_CONTEXT.createUnmarshaller();
    unmarshaller.setSchema(tenantsSchema);
    try {
        final XMLStreamReader xsr = XmlUtils.createSafeReader(new StreamSource(tenantsFile));
        final JAXBElement<Tenants> element = unmarshaller.unmarshal(xsr, Tenants.class);
        return element.getValue();
    } catch (XMLStreamException e) {
        throw new JAXBException("Error unmarshalling tenants", e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(javax.xml.bind.JAXBException) Tenants(org.apache.nifi.authorization.file.tenants.generated.Tenants) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 9 with Tenants

use of org.apache.nifi.authorization.file.tenants.generated.Tenants in project nifi by apache.

the class FileUserGroupProvider method load.

/**
 * Loads the authorizations file and populates the AuthorizationsHolder, only called during start-up.
 *
 * @throws JAXBException            Unable to reload the authorized users file
 * @throws IllegalStateException    Unable to sync file with restore
 * @throws SAXException             Unable to unmarshall tenants
 */
private synchronized void load() throws JAXBException, IllegalStateException, SAXException {
    final Tenants tenants = unmarshallTenants();
    if (tenants.getUsers() == null) {
        tenants.setUsers(new Users());
    }
    if (tenants.getGroups() == null) {
        tenants.setGroups(new Groups());
    }
    final UserGroupHolder userGroupHolder = new UserGroupHolder(tenants);
    final boolean emptyTenants = userGroupHolder.getAllUsers().isEmpty() && userGroupHolder.getAllGroups().isEmpty();
    final boolean hasLegacyAuthorizedUsers = (legacyAuthorizedUsersFile != null && !StringUtils.isBlank(legacyAuthorizedUsersFile));
    if (emptyTenants) {
        if (hasLegacyAuthorizedUsers) {
            logger.info("Loading users from legacy model " + legacyAuthorizedUsersFile + " into new users file.");
            convertLegacyAuthorizedUsers(tenants);
        }
        populateInitialUsers(tenants);
        // save any changes that were made and repopulate the holder
        saveAndRefreshHolder(tenants);
    } else {
        this.userGroupHolder.set(userGroupHolder);
    }
}
Also used : Groups(org.apache.nifi.authorization.file.tenants.generated.Groups) Tenants(org.apache.nifi.authorization.file.tenants.generated.Tenants) Users(org.apache.nifi.authorization.file.tenants.generated.Users)

Aggregations

Tenants (org.apache.nifi.authorization.file.tenants.generated.Tenants)9 JAXBException (javax.xml.bind.JAXBException)2 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 StreamSource (javax.xml.transform.stream.StreamSource)1 AuthorizerCreationException (org.apache.nifi.authorization.exception.AuthorizerCreationException)1 Groups (org.apache.nifi.authorization.file.tenants.generated.Groups)1 Users (org.apache.nifi.authorization.file.tenants.generated.Users)1 PropertyValue (org.apache.nifi.components.PropertyValue)1 SAXException (org.xml.sax.SAXException)1