org.opentestsystem.shared.security.domain.SbacRole.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.shared.security.domain.SbacRole.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System 
 * Copyright (c) 2014 American Institutes for Research
 *   
 * Distributed under the AIR Open Source License, Version 1.0 
 * See accompanying file AIR-License-1_0.txt or at
 * https://bitbucket.org/sbacoss/eotds/wiki/AIR_Open_Source_License
 ******************************************************************************/
package org.opentestsystem.shared.security.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.opentestsystem.shared.progman.client.domain.Tenant;
import org.opentestsystem.shared.progman.client.domain.TenantType;
import org.springframework.security.core.GrantedAuthority;

import com.google.common.collect.Sets;

public class SbacRole implements Serializable {

    private static final long serialVersionUID = -8504277342583575502L;
    private boolean isApplicableToComponent;
    private String roleName;
    private final Map<TenantType, SbacEntity> entities = new HashMap<TenantType, SbacEntity>();
    private TenantType roleEntityLevel;
    private String roleEntityName;
    private Collection<SbacPermission> permissions;
    private Tenant effectiveTenant;
    private boolean isProtected = false;

    public SbacEntity getClient() {
        return this.entities.get(TenantType.CLIENT);
    }

    public SbacEntity getStateGroup() {
        return this.entities.get(TenantType.STATE_GROUP);
    }

    public SbacEntity getState() {
        return this.entities.get(TenantType.STATE);
    }

    public SbacEntity getDistrictGroup() {
        return this.entities.get(TenantType.DISTRICT_GROUP);
    }

    public SbacEntity getDistrict() {
        return this.entities.get(TenantType.DISTRICT);
    }

    public SbacEntity getInstitutionGroup() {
        return this.entities.get(TenantType.INSTITUTION_GROUP);
    }

    public SbacEntity getInstitution() {
        return this.entities.get(TenantType.INSTITUTION);
    }

    public SbacEntity getEntityByType(final TenantType type) {
        return this.entities.get(type);
    }

    public void addEntityValue(final TenantType type, final String entityId, final String inEntityName) {
        this.entities.put(type, new SbacEntity(type, entityId, inEntityName));
    }

    public String getRoleName() {
        return this.roleName;
    }

    public void setRoleName(final String inRoleName) {
        this.roleName = inRoleName;
    }

    public TenantType getRoleEntityLevel() {
        return this.roleEntityLevel;
    }

    public void setRoleEntityLevel(final String inString) {
        this.roleEntityLevel = inString == null ? null : TenantType.valueOf(inString.toUpperCase());
    }

    public String getRoleEntityName() {
        return roleEntityName;
    }

    public void setRoleEntityName(final String inRoleEntityName) {
        roleEntityName = inRoleEntityName;
    }

    public static Collection<GrantedAuthority> calculateGrantedAuthority(final Collection<SbacRole> inSbacRoles) {
        final Set<GrantedAuthority> ret = Sets.newTreeSet(new SbacPermission.GrantedAuthorityComparator());
        if (inSbacRoles != null && !inSbacRoles.isEmpty()) {
            for (final SbacRole role : inSbacRoles) {
                if (role.getPermissions() != null) {
                    ret.addAll(role.getPermissions());
                }
            }
        }
        return ret;
    }

    public SbacEntity getEffectiveEntity() {
        return getEntityByType(this.roleEntityLevel);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    public void setPermissions(final Collection<SbacPermission> inPermissions) {
        permissions = inPermissions;
    }

    public Collection<SbacPermission> getPermissions() {
        return permissions;
    }

    public void setEffectiveTenant(final Tenant inEffectiveTenant) {
        effectiveTenant = inEffectiveTenant;
    }

    public Tenant getEffectiveTenant() {
        return effectiveTenant;
    }

    public boolean hasPermission(final String permissionName, final String componentName) {
        if (permissions != null) {
            for (SbacPermission sbacPermission : permissions) {
                if (sbacPermission.getName().equals(permissionName)
                        && sbacPermission.getComponentName().equals(componentName)) {
                    return true;
                }
            }
        }
        return false;
    }

    public void setIsProtected(final boolean isProtected) {
        this.isProtected = isProtected;
    }

    public boolean isProtected() {
        return this.isProtected;
    }

    public boolean isApplicableToComponent() {
        return isApplicableToComponent;
    }

    public void setIsApplicableToTenant(final boolean inIsApplicableToComponent) {
        isApplicableToComponent = inIsApplicableToComponent;
    }

    public Collection<SbacEntity> getEntities() {
        return entities.values();
    }

}