org.seedstack.seed.security.Role Maven / Gradle / Ivy
/**
* Copyright (c) 2013-2016, The SeedStack authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.seedstack.seed.security;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* Represents a Role and optionnally a collection of scopes on which it is
* given. This scope must be given to all the permissions this role grants.
*
* @author [email protected]
*/
public class Role {
/**
* name of the role
*/
private String name;
/**
* scopes
*/
private Collection scopes;
/** permissions */
private Set permissions;
/**
* Constructor with name
*
* @param name
* the name
*/
public Role(String name) {
this.name = name;
scopes = new ArrayList();
permissions = new HashSet();
}
/**
* Getter name
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Getter scopes
*
* @return the scopes
*/
public Collection getScopes() {
return scopes;
}
/**
* Getter permissions
*
* @return the permissions
*/
public Set getPermissions() {
return permissions;
}
/**
* Filters the scopes corresponding to a type
*
* @param the type of the scope to filter.
* @param scopeType the type of scope
* @return the scopes of the given type
*/
@SuppressWarnings("unchecked")
public Set getScopesByType(Class scopeType) {
Set typedScopes = new HashSet();
for (Scope scope : getScopes()) {
if (scopeType.isInstance(scope)) {
typedScopes.add((S) scope);
}
}
return typedScopes;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Role role = (Role) o;
return name.equals(role.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
/**
* Convenient method to get an unmodifiable role from a role
*
* @param role
* the role to protect
* @return the unmodifiable role
*/
public static Role unmodifiableRole(Role role) {
return new UnmodifiableRole(role);
}
private static final class UnmodifiableRole extends Role {
private Role role;
private UnmodifiableRole(Role role) {
super(role.getName());
this.role = role;
}
@Override
public Collection getScopes() {
return Collections.unmodifiableCollection(role.getScopes());
}
@Override
public Set getPermissions() {
return Collections.unmodifiableSet(role.getPermissions());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy