org.picketlink.authorization.util.AuthorizationUtil Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.picketlink.authorization.util;
import org.picketlink.Identity;
import org.picketlink.common.properties.Property;
import org.picketlink.common.properties.query.AnnotatedPropertyCriteria;
import org.picketlink.common.properties.query.PropertyQueries;
import org.picketlink.idm.IdentityManager;
import org.picketlink.idm.PartitionManager;
import org.picketlink.idm.RelationshipManager;
import org.picketlink.idm.config.IdentityConfiguration;
import org.picketlink.idm.config.IdentityStoreConfiguration;
import org.picketlink.idm.model.Account;
import org.picketlink.idm.model.AttributedType;
import org.picketlink.idm.model.IdentityType;
import org.picketlink.idm.model.Partition;
import org.picketlink.idm.model.Relationship;
import org.picketlink.idm.model.annotation.IdentityStereotype;
import org.picketlink.idm.model.annotation.RelationshipStereotype;
import org.picketlink.idm.model.annotation.StereotypeProperty;
import org.picketlink.idm.query.IdentityQueryBuilder;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import static org.picketlink.common.util.StringUtil.isNullOrEmpty;
import static org.picketlink.idm.model.annotation.IdentityStereotype.Stereotype.GROUP;
import static org.picketlink.idm.model.annotation.IdentityStereotype.Stereotype.ROLE;
import static org.picketlink.idm.model.annotation.RelationshipStereotype.Stereotype.GRANT;
import static org.picketlink.idm.model.annotation.RelationshipStereotype.Stereotype.GROUP_MEMBERSHIP;
import static org.picketlink.idm.model.annotation.StereotypeProperty.Property.IDENTITY_GROUP_NAME;
import static org.picketlink.idm.model.annotation.StereotypeProperty.Property.IDENTITY_ROLE_NAME;
/**
* Provides some comon authorization methods.
*
* @author Pedro Igor
*/
public class AuthorizationUtil {
/**
* Checks if the user is logged in.
*
* @param identity The {@link org.picketlink.Identity} instance representing an authenticated user.
*
* @return True if the user is logged in. Otherwise, returns false.
*/
public static boolean isLoggedIn(Identity identity) {
if (identity == null) {
throw new IllegalArgumentException("You must provide an Identity instance.");
}
return identity.isLoggedIn();
}
/**
* Checks if the user has permissions to a resource considering an operation.
*
* @param identity The {@link org.picketlink.Identity} instance representing an authenticated user.
* @param resource The resource.
* @param resourceClass The resource class if specified.
* @param resourceIdentifier The resource identifier, if specified.
* @param operation The operation.
*/
public static boolean hasPermission(Identity identity, Object resource, Class> resourceClass, Serializable resourceIdentifier, String operation) {
if (resource == null && resourceClass == null) {
throw new IllegalArgumentException("You must provide a resource or resourceClass in order to check a permission.");
}
if (isNullOrEmpty(operation)) {
throw new IllegalArgumentException("You must provide an operation in order to check a permission.");
}
if (!isLoggedIn(identity)) {
return false;
}
if (resource != null && !isNullOrEmpty(resource.toString())) {
return identity.hasPermission(resource, operation);
} else if (resourceClass != null) {
if (resourceIdentifier == null || isNullOrEmpty(resourceIdentifier.toString())) {
resourceIdentifier = null;
}
return identity.hasPermission(resourceClass, resourceIdentifier, operation);
}
return false;
}
/**
* Checks if an authenticated user is granted with a role with the given name.
*
* @param identity The {@link org.picketlink.Identity} instance representing an authenticated user.
* @param partitionManager
* @param roleName The role name.
*
* @return True if the user is granted with the role. Otherwise, returns false.
*/
public static boolean hasRole(Identity identity, PartitionManager partitionManager, String roleName) {
if (!isLoggedIn(identity)) {
return false;
}
List> roleTypes = new ArrayList>();
List> grantRelationshipTypes = new ArrayList>();
// let's get all role and grant relationship types supported by the configuration
for (IdentityConfiguration configuration : partitionManager.getConfigurations()) {
for (IdentityStoreConfiguration storeConfiguration : configuration.getStoreConfiguration()) {
for (Class extends AttributedType> attributedType : storeConfiguration.getSupportedTypes().keySet()) {
if (IdentityType.class.isAssignableFrom(attributedType)) {
IdentityStereotype identityStereotype = attributedType.getAnnotation(IdentityStereotype.class);
if (identityStereotype != null && ROLE.equals(identityStereotype.value())) {
roleTypes.add((Class extends IdentityType>) attributedType);
}
}
if (Relationship.class.isAssignableFrom(attributedType)) {
RelationshipStereotype relationshipStereotype = attributedType.getAnnotation(RelationshipStereotype.class);
if (relationshipStereotype != null && GRANT.equals(relationshipStereotype.value())) {
grantRelationshipTypes.add((Class extends Relationship>) attributedType);
}
}
}
}
}
List roles = new ArrayList();
// now we need to get the role instance by its name against all stored partitions
for (Class extends IdentityType> attributedType : roleTypes) {
List> identityStereotypeProperties = PropertyQueries
.createQuery(attributedType)
.addCriteria(new AnnotatedPropertyCriteria(StereotypeProperty.class))
.getResultList();
for (Property