Java Code Examples for org.apache.catalina.Group#isInRole()

The following examples show how to use org.apache.catalina.Group#isInRole() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserDatabaseRealm.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Return <code>true</code> if the specified Principal has the specified
 * security role, within the context of this Realm; otherwise return
 * <code>false</code>. This implementation returns <code>true</code> if the
 * <code>User</code> has the role, or if any <code>Group</code> that the
 * <code>User</code> is a member of has the role.
 *
 * @param principal Principal for whom the role is to be checked
 * @param role Security role to be checked
 */
@Override
public boolean hasRole(Wrapper wrapper, Principal principal, String role) {
    // Check for a role alias defined in a <security-role-ref> element
    if (wrapper != null) {
        String realRole = wrapper.findSecurityReference(role);
        if (realRole != null)
            role = realRole;
    }
    if (principal instanceof GenericPrincipal) {
        GenericPrincipal gp = (GenericPrincipal) principal;
        if (gp.getUserPrincipal() instanceof User) {
            principal = gp.getUserPrincipal();
        }
    }
    if (!(principal instanceof User)) {
        // Play nice with SSO and mixed Realms
        // No need to pass the wrapper here because role mapping has been
        // performed already a few lines above
        return super.hasRole(null, principal, role);
    }
    if ("*".equals(role)) {
        return true;
    } else if (role == null) {
        return false;
    }
    User user = (User) principal;
    Role dbrole = database.findRole(role);
    if (dbrole == null) {
        return false;
    }
    if (user.isInRole(dbrole)) {
        return true;
    }
    Iterator<Group> groups = user.getGroups();
    while (groups.hasNext()) {
        Group group = groups.next();
        if (group.isInRole(dbrole)) {
            return true;
        }
    }
    return false;
}
 
Example 2
Source File: UserDatabaseRealm.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Return <code>true</code> if the specified Principal has the specified
 * security role, within the context of this Realm; otherwise return
 * <code>false</code>. This implementation returns <code>true</code>
 * if the <code>User</code> has the role, or if any <code>Group</code>
 * that the <code>User</code> is a member of has the role. 
 *
 * @param principal Principal for whom the role is to be checked
 * @param role Security role to be checked
 */
@Override
public boolean hasRole(Wrapper wrapper, Principal principal, String role) {
    // Check for a role alias defined in a <security-role-ref> element
    if (wrapper != null) {
        String realRole = wrapper.findSecurityReference(role);
        if (realRole != null)
            role = realRole;
    }
    if( principal instanceof GenericPrincipal) {
        GenericPrincipal gp = (GenericPrincipal)principal;
        if(gp.getUserPrincipal() instanceof User) {
            principal = gp.getUserPrincipal();
        }
    }
    if(! (principal instanceof User) ) {
        //Play nice with SSO and mixed Realms
        return super.hasRole(null, principal, role);
    }
    if("*".equals(role)) {
        return true;
    } else if(role == null) {
        return false;
    }
    User user = (User)principal;
    Role dbrole = database.findRole(role);
    if(dbrole == null) {
        return false; 
    }
    if(user.isInRole(dbrole)) {
        return true;
    }
    Iterator<Group> groups = user.getGroups();
    while(groups.hasNext()) {
        Group group = groups.next();
        if(group.isInRole(dbrole)) {
            return true;
        }
    }
    return false;
}
 
Example 3
Source File: UserDatabaseRealm.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Return <code>true</code> if the specified Principal has the specified
 * security role, within the context of this Realm; otherwise return
 * <code>false</code>. This implementation returns <code>true</code>
 * if the <code>User</code> has the role, or if any <code>Group</code>
 * that the <code>User</code> is a member of has the role. 
 *
 * @param principal Principal for whom the role is to be checked
 * @param role Security role to be checked
 */
@Override
public boolean hasRole(Wrapper wrapper, Principal principal, String role) {
    // Check for a role alias defined in a <security-role-ref> element
    if (wrapper != null) {
        String realRole = wrapper.findSecurityReference(role);
        if (realRole != null)
            role = realRole;
    }
    if( principal instanceof GenericPrincipal) {
        GenericPrincipal gp = (GenericPrincipal)principal;
        if(gp.getUserPrincipal() instanceof User) {
            principal = gp.getUserPrincipal();
        }
    }
    if(! (principal instanceof User) ) {
        //Play nice with SSO and mixed Realms
        return super.hasRole(null, principal, role);
    }
    if("*".equals(role)) {
        return true;
    } else if(role == null) {
        return false;
    }
    User user = (User)principal;
    Role dbrole = database.findRole(role);
    if(dbrole == null) {
        return false; 
    }
    if(user.isInRole(dbrole)) {
        return true;
    }
    Iterator<Group> groups = user.getGroups();
    while(groups.hasNext()) {
        Group group = groups.next();
        if(group.isInRole(dbrole)) {
            return true;
        }
    }
    return false;
}