Search in sources :

Example 1 with Role

use of org.apache.catalina.Role in project tomcat by apache.

the class UserDatabaseRealm method hasRole.

// --------------------------------------------------------- Public Methods
/**
     * 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;
}
Also used : Role(org.apache.catalina.Role) Group(org.apache.catalina.Group) User(org.apache.catalina.User)

Example 2 with Role

use of org.apache.catalina.Role in project tomcat70 by apache.

the class MemoryUserDatabaseMBean method removeRole.

/**
 * Remove an existing role and destroy the corresponding MBean.
 *
 * @param rolename Role name to remove
 */
public void removeRole(String rolename) {
    UserDatabase database = (UserDatabase) this.resource;
    Role role = database.findRole(rolename);
    if (role == null) {
        return;
    }
    try {
        MBeanUtils.destroyMBean(role);
        database.removeRole(role);
    } catch (Exception e) {
        IllegalArgumentException iae = new IllegalArgumentException("Exception destroying role [" + rolename + "] MBean");
        iae.initCause(e);
        throw iae;
    }
}
Also used : Role(org.apache.catalina.Role) UserDatabase(org.apache.catalina.UserDatabase) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 3 with Role

use of org.apache.catalina.Role in project tomcat70 by apache.

the class UserMBean method addRole.

/**
 * Add a new {@link Role} to those this user belongs to.
 *
 * @param rolename Role name of the new role
 */
public void addRole(String rolename) {
    User user = (User) this.resource;
    if (user == null) {
        return;
    }
    Role role = user.getUserDatabase().findRole(rolename);
    if (role == null) {
        throw new IllegalArgumentException("Invalid role name '" + rolename + "'");
    }
    user.addRole(role);
}
Also used : Role(org.apache.catalina.Role) User(org.apache.catalina.User)

Example 4 with Role

use of org.apache.catalina.Role in project tomcat70 by apache.

the class UserMBean method removeRole.

/**
 * Remove a {@link Role} from those this user belongs to.
 *
 * @param rolename Role name of the old role
 */
public void removeRole(String rolename) {
    User user = (User) this.resource;
    if (user == null) {
        return;
    }
    Role role = user.getUserDatabase().findRole(rolename);
    if (role == null) {
        throw new IllegalArgumentException("Invalid role name '" + rolename + "'");
    }
    user.removeRole(role);
}
Also used : Role(org.apache.catalina.Role) User(org.apache.catalina.User)

Example 5 with Role

use of org.apache.catalina.Role in project tomcat70 by apache.

the class UserMBean method getRoles.

/**
 * Return the MBean Names of all roles assigned to this user.
 */
public String[] getRoles() {
    User user = (User) this.resource;
    ArrayList<String> results = new ArrayList<String>();
    Iterator<Role> roles = user.getRoles();
    while (roles.hasNext()) {
        Role role = null;
        try {
            role = roles.next();
            ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), role);
            results.add(oname.toString());
        } catch (MalformedObjectNameException e) {
            IllegalArgumentException iae = new IllegalArgumentException("Cannot create object name for role " + role);
            iae.initCause(e);
            throw iae;
        }
    }
    return results.toArray(new String[results.size()]);
}
Also used : Role(org.apache.catalina.Role) MalformedObjectNameException(javax.management.MalformedObjectNameException) User(org.apache.catalina.User) ArrayList(java.util.ArrayList) ObjectName(javax.management.ObjectName)

Aggregations

Role (org.apache.catalina.Role)50 UserDatabase (org.apache.catalina.UserDatabase)21 Group (org.apache.catalina.Group)20 User (org.apache.catalina.User)18 ArrayList (java.util.ArrayList)13 MalformedObjectNameException (javax.management.MalformedObjectNameException)10 ObjectName (javax.management.ObjectName)7 PreparedStatement (java.sql.PreparedStatement)5 SQLException (java.sql.SQLException)5 Connection (java.sql.Connection)4 ResultSet (java.sql.ResultSet)4 MBeanException (javax.management.MBeanException)4 RuntimeOperationsException (javax.management.RuntimeOperationsException)4 Statement (java.sql.Statement)2 NamingException (javax.naming.NamingException)2 OperationNotSupportedException (javax.naming.OperationNotSupportedException)2 LoggingBaseTest (org.apache.catalina.startup.LoggingBaseTest)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1