Search in sources :

Example 1 with PrincipalType

use of com.enonic.xp.security.PrincipalType in project xp by enonic.

the class PrincipalKeyNodeTranslator method toKey.

public static PrincipalKey toKey(final Node node) {
    final String principalId = node.name().toString();
    final PropertyTree rootDataSet = node.data();
    final String principalType = rootDataSet.getString(PrincipalPropertyNames.PRINCIPAL_TYPE_KEY);
    final String idProviderKey = rootDataSet.getString(PrincipalPropertyNames.ID_PROVIDER_KEY);
    final PrincipalType type = PrincipalType.valueOf(principalType);
    switch(type) {
        case USER:
            return PrincipalKey.ofUser(IdProviderKey.from(idProviderKey), principalId);
        case GROUP:
            return PrincipalKey.ofGroup(IdProviderKey.from(idProviderKey), principalId);
        case ROLE:
            return PrincipalKey.ofRole(principalId);
    }
    throw new IllegalArgumentException("Invalid principal type in node: " + type);
}
Also used : PropertyTree(com.enonic.xp.data.PropertyTree) PrincipalType(com.enonic.xp.security.PrincipalType)

Example 2 with PrincipalType

use of com.enonic.xp.security.PrincipalType in project xp by enonic.

the class PrincipalNodeTranslator method doCreatePrincipalFromNode.

private static Principal doCreatePrincipalFromNode(final Node node) {
    final PropertySet nodeAsSet = node.data().getRoot();
    if (nodeAsSet.isNull(PrincipalPropertyNames.PRINCIPAL_TYPE_KEY)) {
        throw new IllegalArgumentException("Property " + PrincipalPropertyNames.PRINCIPAL_TYPE_KEY + " not found on node with id " + node.id());
    }
    final PrincipalType principalType = PrincipalType.valueOf(nodeAsSet.getString(PrincipalPropertyNames.PRINCIPAL_TYPE_KEY));
    switch(principalType) {
        case USER:
            return createUserFromNode(node);
        case GROUP:
            return createGroupFromNode(node);
        case ROLE:
            return createRoleFromNode(node);
        default:
            throw new IllegalArgumentException("Not able to translate principal-type " + principalType);
    }
}
Also used : PropertySet(com.enonic.xp.data.PropertySet) PrincipalType(com.enonic.xp.security.PrincipalType)

Example 3 with PrincipalType

use of com.enonic.xp.security.PrincipalType in project xp by enonic.

the class PrincipalQueryNodeQueryTranslator method translate.

public static NodeQuery translate(final PrincipalQuery principalQuery) {
    final NodeQuery.Builder nodeQueryBuilder = NodeQuery.create().from(principalQuery.getFrom()).size(principalQuery.getSize());
    final IdProviderKeys idProviders = principalQuery.getIdProviders();
    if (idProviders.isNotEmpty()) {
        nodeQueryBuilder.addQueryFilter(ValueFilter.create().fieldName(ID_PROVIDER_KEY).addValues(idProviders.stream().map(IdProviderKey::toString).collect(toList())).build());
    }
    final Set<PrincipalType> types = principalQuery.getPrincipalTypes();
    if (!types.isEmpty()) {
        nodeQueryBuilder.addQueryFilter(ValueFilter.create().fieldName(PRINCIPAL_TYPE_KEY).addValues(types.stream().map(Object::toString).collect(toList())).build());
    }
    final String searchText = principalQuery.getSearchText();
    if (!nullToEmpty(searchText).isBlank()) {
        nodeQueryBuilder.query(getQueryExpression(searchText));
    }
    final String email = principalQuery.getEmail();
    if (!nullToEmpty(email).isBlank()) {
        nodeQueryBuilder.addQueryFilter(ValueFilter.create().fieldName(EMAIL_KEY).addValues(email).build());
    }
    final String name = principalQuery.getName();
    if (!nullToEmpty(name).isBlank()) {
        nodeQueryBuilder.addQueryFilter(ValueFilter.create().fieldName(NAME_KEY).addValues(name).build());
    }
    final String displayName = principalQuery.getDisplayName();
    if (!nullToEmpty(displayName).isBlank()) {
        nodeQueryBuilder.addQueryFilter(ValueFilter.create().fieldName(DISPLAY_NAME_KEY).addValues(displayName).build());
    }
    return nodeQueryBuilder.build();
}
Also used : NodeQuery(com.enonic.xp.node.NodeQuery) IdProviderKey(com.enonic.xp.security.IdProviderKey) IdProviderKeys(com.enonic.xp.security.IdProviderKeys) PrincipalType(com.enonic.xp.security.PrincipalType)

Aggregations

PrincipalType (com.enonic.xp.security.PrincipalType)3 PropertySet (com.enonic.xp.data.PropertySet)1 PropertyTree (com.enonic.xp.data.PropertyTree)1 NodeQuery (com.enonic.xp.node.NodeQuery)1 IdProviderKey (com.enonic.xp.security.IdProviderKey)1 IdProviderKeys (com.enonic.xp.security.IdProviderKeys)1