Search in sources :

Example 1 with RoleProvider

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

the class UserAndRoleDirectoryServiceImpl method getRoles.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.security.api.RoleDirectoryService#getRoles()
 */
@Override
@SuppressWarnings("unchecked")
public Iterator<Role> getRoles() {
    Organization org = securityService.getOrganization();
    if (org == null)
        throw new IllegalStateException("No organization is set");
    Stream<Role> roles = Stream.empty();
    for (RoleProvider roleProvider : roleProviders) {
        String providerOrgId = roleProvider.getOrganization();
        if (!ALL_ORGANIZATIONS.equals(providerOrgId) && !org.getId().equals(providerOrgId))
            continue;
        roles = roles.append(IteratorUtils.toList(roleProvider.getRoles())).sort(roleComparator);
    }
    return roles.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) RoleProvider(org.opencastproject.security.api.RoleProvider)

Example 2 with RoleProvider

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

the class UserAndRoleDirectoryServiceImpl method loadUserByUsername.

/**
 * {@inheritDoc}
 *
 * @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
 */
@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, org.springframework.dao.DataAccessException {
    User user = loadUser(userName);
    if (user == null)
        throw new UsernameNotFoundException(userName);
    // Store the user in the security service
    securityService.setUser(user);
    Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
    for (Role role : user.getRoles()) {
        authorities.add(new SimpleGrantedAuthority(role.getName()));
    }
    // Add additional roles from role providers
    if (!InMemoryUserAndRoleProvider.PROVIDER_NAME.equals(user.getProvider())) {
        for (RoleProvider roleProvider : roleProviders) {
            List<Role> rolesForUser = roleProvider.getRolesForUser(userName);
            for (Role role : rolesForUser) authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
    }
    authorities.add(new SimpleGrantedAuthority(securityService.getOrganization().getAnonymousRole()));
    // need a non null password to instantiate org.springframework.security.core.userdetails.User
    // but CAS authenticated users have no password
    String password = user.getPassword() == null ? DEFAULT_PASSWORD : user.getPassword();
    return new org.springframework.security.core.userdetails.User(user.getUsername(), password, user.canLogin(), true, true, true, authorities);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) JaxbRole(org.opencastproject.security.api.JaxbRole) Role(org.opencastproject.security.api.Role) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) RoleProvider(org.opencastproject.security.api.RoleProvider) HashSet(java.util.HashSet)

Example 3 with RoleProvider

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

the class UserAndRoleDirectoryServiceImpl method findRoles.

@Override
@SuppressWarnings("unchecked")
public Iterator<Role> findRoles(String query, Role.Target target, int offset, int limit) {
    if (query == null)
        throw new IllegalArgumentException("Query must be set");
    Organization org = securityService.getOrganization();
    if (org == null)
        throw new IllegalStateException("No organization is set");
    // Find all roles from the role providers
    Stream<Role> roles = Stream.empty();
    for (RoleProvider roleProvider : roleProviders) {
        String providerOrgId = roleProvider.getOrganization();
        if (!ALL_ORGANIZATIONS.equals(providerOrgId) && !org.getId().equals(providerOrgId))
            continue;
        roles = roles.append(IteratorUtils.toList(roleProvider.findRoles(query, target, 0, 0))).sort(roleComparator);
    }
    return roles.drop(offset).apply(limit > 0 ? StreamOp.<Role>id().take(limit) : StreamOp.<Role>id()).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) RoleProvider(org.opencastproject.security.api.RoleProvider)

Example 4 with RoleProvider

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

the class UserAndRoleDirectoryServiceImplTest method setUp.

@Before
public void setUp() throws Exception {
    org = new DefaultOrganization();
    userName = "sampleUser";
    role1 = new JaxbRole("role1", org);
    role2 = new JaxbRole("role2", org);
    role3 = new JaxbRole("role3", org);
    JaxbUser user1 = new JaxbUser(userName, "matterhorn", org, role1, role2);
    user1.setManageable(true);
    User user2 = new JaxbUser(userName, "secret", "test", true, org, role2, role3);
    User user3 = new JaxbUser("userSample", "test", org, role2, role3);
    List<User> users = new ArrayList<User>();
    users.add(user1);
    UserProvider provider1 = EasyMock.createNiceMock(UserProvider.class);
    EasyMock.expect(provider1.getOrganization()).andReturn(org.getId()).anyTimes();
    EasyMock.expect(provider1.loadUser((String) EasyMock.anyObject())).andReturn(user1).anyTimes();
    EasyMock.expect(provider1.findUsers("%mple%", 0, 0)).andReturn(users.iterator()).once();
    EasyMock.expect(provider1.findUsers("%mple%", 0, 0)).andReturn(users.iterator()).once();
    EasyMock.expect(provider1.getUsers()).andReturn(users.iterator()).once();
    EasyMock.expect(provider1.getName()).andReturn("test").once();
    List<User> users2 = new ArrayList<User>();
    users2.add(user3);
    UserProvider provider2 = EasyMock.createNiceMock(UserProvider.class);
    EasyMock.expect(provider2.getOrganization()).andReturn(org.getId()).anyTimes();
    EasyMock.expect(provider2.loadUser((String) EasyMock.anyObject())).andReturn(user2).anyTimes();
    EasyMock.expect(provider2.findUsers("%mple%", 0, 0)).andReturn(users2.iterator()).once();
    EasyMock.expect(provider2.findUsers("%mple%", 0, 0)).andReturn(users2.iterator()).once();
    EasyMock.expect(provider2.getUsers()).andReturn(users2.iterator()).once();
    EasyMock.expect(provider2.getName()).andReturn("matterhorn").once();
    List<Role> roles1 = new ArrayList<Role>();
    roles1.add(new JaxbRole("ROLE_ASTRO_2011", org));
    roles1.add(new JaxbRole("ROLE_ASTRO_2012", org));
    List<Role> rolesForUser1 = new ArrayList<Role>();
    rolesForUser1.add(new JaxbRole("ROLE_ASTRO_2012", org));
    List<Role> findRoles1 = new ArrayList<Role>();
    findRoles1.add(new JaxbRole("ROLE_ASTRO_2012", org));
    RoleProvider roleProvider1 = EasyMock.createNiceMock(RoleProvider.class);
    EasyMock.expect(roleProvider1.getOrganization()).andReturn(org.getId()).anyTimes();
    EasyMock.expect(roleProvider1.getRoles()).andReturn(roles1.iterator()).anyTimes();
    EasyMock.expect(roleProvider1.getRolesForUser((String) EasyMock.anyObject())).andReturn(rolesForUser1).anyTimes();
    EasyMock.expect(roleProvider1.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(findRoles1.iterator()).once();
    EasyMock.expect(roleProvider1.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(findRoles1.iterator()).once();
    List<Role> roles2 = new ArrayList<Role>();
    roles2.add(new JaxbRole("ROLE_MATH_2011", org));
    roles2.add(new JaxbRole("ROLE_MATH_2012", org));
    List<Role> rolesForUser2 = new ArrayList<Role>();
    rolesForUser2.add(new JaxbRole("ROLE_MATH_2012", org));
    List<Role> findRoles2 = new ArrayList<Role>();
    findRoles2.add(new JaxbRole("ROLE_MATH_2012", org));
    RoleProvider roleProvider2 = EasyMock.createNiceMock(RoleProvider.class);
    EasyMock.expect(roleProvider2.getOrganization()).andReturn(org.getId()).anyTimes();
    EasyMock.expect(roleProvider2.getRoles()).andReturn(roles2.iterator()).anyTimes();
    EasyMock.expect(roleProvider2.getRolesForUser((String) EasyMock.anyObject())).andReturn(rolesForUser2).anyTimes();
    EasyMock.expect(roleProvider2.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(findRoles2.iterator()).once();
    EasyMock.expect(roleProvider2.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(findRoles2.iterator()).once();
    RoleProvider otherOrgRoleProvider = EasyMock.createNiceMock(RoleProvider.class);
    EasyMock.expect(otherOrgRoleProvider.getOrganization()).andReturn("otherOrg").anyTimes();
    EasyMock.expect(otherOrgRoleProvider.getRoles()).andReturn(roles2.iterator()).anyTimes();
    EasyMock.expect(otherOrgRoleProvider.getRolesForUser((String) EasyMock.anyObject())).andReturn(rolesForUser2).anyTimes();
    EasyMock.expect(otherOrgRoleProvider.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(new ArrayList<Role>().iterator()).anyTimes();
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getOrganization()).andReturn(org).anyTimes();
    EasyMock.replay(provider1, provider2, roleProvider1, roleProvider2, otherOrgRoleProvider, securityService);
    directory = new UserAndRoleDirectoryServiceImpl();
    directory.activate(null);
    directory.setSecurityService(securityService);
    directory.addUserProvider(provider1);
    directory.addUserProvider(provider2);
    directory.addRoleProvider(roleProvider1);
    directory.addRoleProvider(roleProvider2);
    directory.addRoleProvider(otherOrgRoleProvider);
}
Also used : User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) ArrayList(java.util.ArrayList) JaxbUser(org.opencastproject.security.api.JaxbUser) RoleProvider(org.opencastproject.security.api.RoleProvider) Role(org.opencastproject.security.api.Role) JaxbRole(org.opencastproject.security.api.JaxbRole) JaxbRole(org.opencastproject.security.api.JaxbRole) UserProvider(org.opencastproject.security.api.UserProvider) SecurityService(org.opencastproject.security.api.SecurityService) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Before(org.junit.Before)

Aggregations

JaxbRole (org.opencastproject.security.api.JaxbRole)4 Role (org.opencastproject.security.api.Role)4 RoleProvider (org.opencastproject.security.api.RoleProvider)4 JaxbOrganization (org.opencastproject.security.api.JaxbOrganization)2 JaxbUser (org.opencastproject.security.api.JaxbUser)2 Organization (org.opencastproject.security.api.Organization)2 User (org.opencastproject.security.api.User)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Before (org.junit.Before)1 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)1 SecurityService (org.opencastproject.security.api.SecurityService)1 UserProvider (org.opencastproject.security.api.UserProvider)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1