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

com.hp.autonomy.frontend.configuration.authentication.Role Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014-2015 Open Text.
 *
 * Licensed under the MIT License (the "License"); you may not use this file
 * except in compliance with the License.
 *
 * The only warranties for products and services of Open Text and its affiliates
 * and licensors ("Open Text") are as may be set forth in the express warranty
 * statements accompanying such products and services. Nothing herein should be
 * construed as constituting an additional warranty. Open Text shall not be
 * liable for technical or editorial errors or omissions contained herein. The
 * information contained herein is subject to change without notice.
 */

package com.hp.autonomy.frontend.configuration.authentication;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.HashSet;
import java.util.Set;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Data
public class Role {

    private final String name;
    private final Set privileges;
    private final Set parent;
    private final String sessionAttribute;

    public Role getParent(final String roleName){
        for(final Role role : this.parent){
            if(role.getName().equals(roleName)){
                return role;
            }
        }

        return null;
    }

    public Set getAncestors(){
        final Set ancestors = new HashSet<>();
        this.getRecursiveAncestors(this.getParent(), ancestors);

        return ancestors;
    }

    public boolean isAuthorized(final String privilege){
        if(this.privileges.contains(privilege)){
            return true;
        }

        for(final Role role: this.getAncestors()){
            if(role.getPrivileges().contains(privilege)){
                return true;
            }
        }

        return false;
    }

    public boolean isAuthorized(final Set privileges){
        for(final String privilege : privileges){
            if(!this.isAuthorized(privilege)){
                return false;
            }
        }

        return true;
    }

    private void getRecursiveAncestors(final Set roles, final Set ancestors){
        for(final Role role : roles){
            this.getRecursiveAncestors(role, ancestors);
        }
    }

    private void getRecursiveAncestors(final Role role, final Set ancestors){
        ancestors.add(role);

        if(role.getParent().isEmpty()) {
            return;
        }

        this.getRecursiveAncestors(role.getParent(), ancestors);
    }

    @Setter
    @Accessors(chain = true)
    public static class Builder {

        private String name;
        private Set privileges = new HashSet<>();
        private Set parent = new HashSet<>();
        private String sessionAttribute;

        public Role build() {
            return new Role(name, privileges, parent, sessionAttribute);
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy