All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cleverpine.viravaspringhelper.dto.Permission Maven / Gradle / Ivy

There is a newer version: 3.1.6
Show newest version
package com.cleverpine.viravaspringhelper.dto;

import com.cleverpine.viravaspringhelper.core.BaseResource;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class Permission {
    private final BaseResource resource;

    private final Set scopeTypes;

    private final ScopeHolder scopeHolder;

    private Permission(BaseResource resource, Set scopeTypes) {
        this.resource = resource;
        this.scopeTypes = Collections.unmodifiableSet(scopeTypes);
        this.scopeHolder = new ScopeHolder(
                scopeTypes.contains(ScopeType.CREATE),
                scopeTypes.contains(ScopeType.READ),
                scopeTypes.contains(ScopeType.UPDATE),
                scopeTypes.contains(ScopeType.DELETE));
    }

    public static Permission of(PermissionInitializer permissionBuilder) {
        return new Permission(permissionBuilder.getResource(), permissionBuilder.getScopeTypes());
    }

    public static Permission of(BaseResource resource, Scope... scopes) {
        var scopeTypes = scopes == null ?
                new HashSet() :
                Arrays.stream(scopes).flatMap(scope -> scope.getScopeTypes().stream()).collect(Collectors.toSet());
        return new Permission(resource, scopeTypes);
    }

    public BaseResource getResource() {
        return resource;
    }

    public Set getScopeTypes() {
        return Set.copyOf(scopeTypes);
    }

    public ScopeHolder getScopeHolder() {
        return scopeHolder;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy