Search in sources :

Example 51 with JaxbRole

use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.

the class UserAndRoleDirectoryServiceImpl method mergeUsers.

/**
 * Merges two representations of a user, as returned by two different user providers. The set or roles from the
 * provided users will be merged into one set.
 *
 * @param user1
 *          the first user to merge
 * @param user2
 *          the second user to merge
 * @return a user with a merged set of roles
 */
protected User mergeUsers(User user1, User user2) {
    HashSet<JaxbRole> mergedRoles = new HashSet<JaxbRole>();
    for (Role role : user1.getRoles()) {
        mergedRoles.add(JaxbRole.fromRole(role));
    }
    for (Role role : user2.getRoles()) {
        mergedRoles.add(JaxbRole.fromRole(role));
    }
    String name = StringUtils.isNotBlank(user1.getName()) ? user1.getName() : user2.getName();
    String email = StringUtils.isNotBlank(user1.getEmail()) ? user1.getEmail() : user2.getEmail();
    String password = user1.getPassword() == null ? user2.getPassword() : user1.getPassword();
    boolean manageable = user1.isManageable() || user2.isManageable() ? true : false;
    JaxbOrganization organization = JaxbOrganization.fromOrganization(user1.getOrganization());
    String provider = StringUtils.join(Collections.nonNullList(user1.getProvider(), user2.getProvider()), ",");
    JaxbUser jaxbUser = new JaxbUser(user1.getUsername(), password, name, email, provider, organization, mergedRoles);
    jaxbUser.setManageable(manageable);
    return jaxbUser;
}
Also used : JaxbRole(org.opencastproject.security.api.JaxbRole) Role(org.opencastproject.security.api.Role) JaxbRole(org.opencastproject.security.api.JaxbRole) JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) JaxbUser(org.opencastproject.security.api.JaxbUser) HashSet(java.util.HashSet)

Example 52 with JaxbRole

use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.

the class OpencastLdapAuthoritiesPopulator method getGrantedAuthorities.

@Override
public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) {
    Set<GrantedAuthority> authorities = new HashSet<>();
    for (String attributeName : attributeNames) {
        try {
            String[] attributeValues = userData.getStringAttributes(attributeName);
            // Should the attribute not be defined, the returned array is null
            if (attributeValues != null) {
                for (String attributeValue : attributeValues) {
                    // The attribute value may be a single authority (a single role) or a list of roles
                    addAuthorities(authorities, attributeValue.split(","));
                }
            } else {
                debug("({}) Could not find any attribute named '{}' in user '{}'", attributeName, userData.getDn());
            }
        } catch (ClassCastException e) {
            error("Specified attribute containing user roles ('{}') was not of expected type String: {}", attributeName, e);
        }
    }
    // Add the list of additional roles
    addAuthorities(authorities, additionalAuthorities);
    if (logger.isDebugEnabled()) {
        debug("Returning user {} with authorities:", username);
        for (GrantedAuthority authority : authorities) {
            logger.error("\t{}", authority);
        }
    }
    // Update the user in the security service if it matches the user whose authorities are being returned
    if ((securityService.getOrganization().equals(organization)) && ((securityService.getUser() == null) || (securityService.getUser().getUsername().equals(username)))) {
        Set<JaxbRole> roles = new HashSet<>();
        // Get the current roles
        for (Role existingRole : securityService.getUser().getRoles()) {
            authorities.add(new SimpleGrantedAuthority(existingRole.getName()));
        }
        // Convert GrantedAuthority's into JaxbRole's
        for (GrantedAuthority authority : authorities) roles.add(new JaxbRole(authority.getAuthority(), JaxbOrganization.fromOrganization(organization)));
        JaxbUser user = new JaxbUser(username, LdapUserProviderInstance.PROVIDER_NAME, JaxbOrganization.fromOrganization(organization), roles.toArray(new JaxbRole[0]));
        securityService.setUser(user);
    }
    return authorities;
}
Also used : Role(org.opencastproject.security.api.Role) JaxbRole(org.opencastproject.security.api.JaxbRole) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) JaxbRole(org.opencastproject.security.api.JaxbRole) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) JaxbUser(org.opencastproject.security.api.JaxbUser) HashSet(java.util.HashSet)

Example 53 with JaxbRole

use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.

the class CustomRoleProvider method findRoles.

/**
 * @see org.opencastproject.security.api.RoleProvider#findRoles(String, Role.Target, int, int)
 */
@Override
public Iterator<Role> findRoles(String query, Role.Target target, int offset, int limit) {
    if (query == null)
        throw new IllegalArgumentException("Query must be set");
    Organization organization = securityService.getOrganization();
    // Match the custom regular expression first if this is an ACL role query
    if ((target == Role.Target.ACL) && (rolematch != null)) {
        String exactQuery = StringUtils.removeEnd(query, "%");
        Matcher m = rolematch.matcher(exactQuery);
        if (m.matches()) {
            List<Role> roles = new LinkedList<Role>();
            JaxbOrganization jaxbOrganization = JaxbOrganization.fromOrganization(organization);
            roles.add(new JaxbRole(exactQuery, jaxbOrganization, "Custom Role", Role.Type.EXTERNAL));
            return roles.iterator();
        }
    }
    // Otherwise match on the custom roles specified in a list
    return Stream.$(roles).filter(filterByName._2(query)).drop(offset).apply(limit > 0 ? StreamOp.<String>id().take(limit) : StreamOp.<String>id()).map(toRole._2(organization)).iterator();
}
Also used : JaxbRole(org.opencastproject.security.api.JaxbRole) Role(org.opencastproject.security.api.Role) Organization(org.opencastproject.security.api.Organization) JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) JaxbRole(org.opencastproject.security.api.JaxbRole) Matcher(java.util.regex.Matcher) JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) LinkedList(java.util.LinkedList)

Example 54 with JaxbRole

use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.

the class JpaGroupRoleProvider method getGroupsRoles.

/**
 * Returns all roles from a given group list
 *
 * @param groups
 *          the group list
 * @return the role list
 */
private List<Role> getGroupsRoles(List<JpaGroup> groups) {
    List<Role> roles = new ArrayList<Role>();
    for (Group group : groups) {
        roles.add(new JaxbRole(group.getRole(), JaxbOrganization.fromOrganization(group.getOrganization()), "", Role.Type.GROUP));
        for (Role role : group.getRoles()) {
            JaxbRole grouprole = new JaxbRole(role.getName(), JaxbOrganization.fromOrganization(role.getOrganization()), role.getDescription(), Role.Type.DERIVED);
            roles.add(grouprole);
        }
    }
    return roles;
}
Also used : JaxbRole(org.opencastproject.security.api.JaxbRole) Role(org.opencastproject.security.api.Role) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) JpaGroup(org.opencastproject.security.impl.jpa.JpaGroup) JaxbGroup(org.opencastproject.security.api.JaxbGroup) Group(org.opencastproject.security.api.Group) JaxbRole(org.opencastproject.security.api.JaxbRole) ArrayList(java.util.ArrayList)

Example 55 with JaxbRole

use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.

the class JpaGroupRoleProvider method getRolesForGroup.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.security.api.RoleProvider#getRolesForUser(String)
 */
@Override
public List<Role> getRolesForGroup(String groupName) {
    List<Role> roles = new ArrayList<Role>();
    String orgId = securityService.getOrganization().getId();
    Group group = UserDirectoryPersistenceUtil.findGroupByRole(groupName, orgId, emf);
    if (group != null) {
        for (Role role : group.getRoles()) {
            JaxbRole grouprole = new JaxbRole(role.getName(), JaxbOrganization.fromOrganization(role.getOrganization()), role.getDescription(), Role.Type.DERIVED);
            roles.add(grouprole);
        }
    } else {
        logger.warn("Group {} not found", groupName);
    }
    return roles;
}
Also used : JaxbRole(org.opencastproject.security.api.JaxbRole) Role(org.opencastproject.security.api.Role) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) JpaGroup(org.opencastproject.security.impl.jpa.JpaGroup) JaxbGroup(org.opencastproject.security.api.JaxbGroup) Group(org.opencastproject.security.api.Group) JaxbRole(org.opencastproject.security.api.JaxbRole) ArrayList(java.util.ArrayList)

Aggregations

JaxbRole (org.opencastproject.security.api.JaxbRole)66 JaxbUser (org.opencastproject.security.api.JaxbUser)53 User (org.opencastproject.security.api.User)45 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)39 SecurityService (org.opencastproject.security.api.SecurityService)39 Before (org.junit.Before)30 JaxbOrganization (org.opencastproject.security.api.JaxbOrganization)25 HashSet (java.util.HashSet)18 OrganizationDirectoryService (org.opencastproject.security.api.OrganizationDirectoryService)18 Organization (org.opencastproject.security.api.Organization)17 UserDirectoryService (org.opencastproject.security.api.UserDirectoryService)17 Test (org.junit.Test)14 Role (org.opencastproject.security.api.Role)13 Workspace (org.opencastproject.workspace.api.Workspace)13 ServiceRegistryInMemoryImpl (org.opencastproject.serviceregistry.api.ServiceRegistryInMemoryImpl)11 URI (java.net.URI)10 BundleContext (org.osgi.framework.BundleContext)10 ComponentContext (org.osgi.service.component.ComponentContext)10 File (java.io.File)9 ArrayList (java.util.ArrayList)9