Search in sources :

Example 36 with User

use of org.opencastproject.security.api.User 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 37 with User

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

the class UserAndRoleDirectoryServiceImpl method loadUser.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.security.api.UserDirectoryService#loadUser(java.lang.String)
 */
@Override
public User loadUser(String userName) throws IllegalStateException {
    Organization org = securityService.getOrganization();
    if (org == null) {
        throw new IllegalStateException("No organization is set");
    }
    Object user = cache.getUnchecked(tuple(org.getId(), userName));
    if (user == nullToken) {
        cache.invalidate(tuple(org.getId(), userName));
        return null;
    } else {
        return (User) user;
    }
}
Also used : Organization(org.opencastproject.security.api.Organization) JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser)

Example 38 with User

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

the class UserAndRoleDirectoryServiceImpl method findUsers.

@Override
@SuppressWarnings("unchecked")
public Iterator<User> findUsers(String query, 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 users from the user providers
    Stream<User> users = Stream.empty();
    for (final UserProvider userProvider : userProviders) {
        String providerOrgId = userProvider.getOrganization();
        if (!ALL_ORGANIZATIONS.equals(providerOrgId) && !org.getId().equals(providerOrgId))
            continue;
        users = users.append(IteratorUtils.toList(userProvider.findUsers(query, 0, 0))).sort(userComparator);
    }
    return users.drop(offset).apply(limit > 0 ? StreamOp.<User>id().take(limit) : StreamOp.<User>id()).iterator();
}
Also used : Organization(org.opencastproject.security.api.Organization) JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) UserProvider(org.opencastproject.security.api.UserProvider)

Example 39 with User

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

the class UserEndpoint method getUsersAsXml.

@GET
@Path("users.xml")
@Produces(MediaType.APPLICATION_XML)
@RestQuery(name = "allusersasxml", description = "Returns a list of users", returnDescription = "Returns a XML representation of the list of user accounts", restParameters = { @RestParameter(description = "The search query, must be at lest 3 characters long.", isRequired = false, name = "query", type = RestParameter.Type.STRING), @RestParameter(defaultValue = "100", description = "The maximum number of items to return per page.", isRequired = false, name = "limit", type = RestParameter.Type.INTEGER), @RestParameter(defaultValue = "0", description = "The page number.", isRequired = false, name = "offset", type = RestParameter.Type.INTEGER) }, reponses = { @RestResponse(responseCode = SC_OK, description = "The user accounts.") })
public Response getUsersAsXml(@QueryParam("query") String queryString, @QueryParam("limit") int limit, @QueryParam("offset") int offset) throws IOException {
    if (limit < 1)
        limit = 100;
    String query = "%";
    if (StringUtils.isNotBlank(queryString)) {
        if (queryString.trim().length() < 3)
            return Response.status(Status.BAD_REQUEST).build();
        query = queryString;
    }
    JaxbUserList userList = new JaxbUserList();
    for (Iterator<User> i = userDirectoryService.findUsers(query, offset, limit); i.hasNext(); ) {
        userList.add(i.next());
    }
    return Response.ok(userList).build();
}
Also used : User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) JaxbUserList(org.opencastproject.security.api.JaxbUserList) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 40 with User

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

the class OpencastLdapAuthoritiesPopulatorTest method setUp.

@Before
public void setUp() {
    mappings = new HashMap<>();
    org = EasyMock.createNiceMock(Organization.class);
    EasyMock.expect(org.getId()).andReturn(ORG_NAME).anyTimes();
    Set<Role> groupRoles = new HashSet<>();
    for (int i = 1; i <= N_GROUP_ROLES; i++) {
        Role r = EasyMock.createNiceMock(Role.class);
        EasyMock.expect(r.getOrganization()).andReturn(org).anyTimes();
        EasyMock.expect(r.getName()).andReturn(format("group_role_%d", i)).anyTimes();
        EasyMock.replay(r);
        groupRoles.add(r);
    }
    User mockUser = EasyMock.createNiceMock(User.class);
    EasyMock.expect(mockUser.getUsername()).andReturn(USERNAME).anyTimes();
    EasyMock.expect(mockUser.getRoles()).andReturn(DEFAULT_INTERNAL_ROLES).anyTimes();
    securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getOrganization()).andReturn(org).anyTimes();
    EasyMock.expect(securityService.getUser()).andReturn(mockUser).anyTimes();
    groupRoleProvider = EasyMock.createNiceMock(JpaGroupRoleProvider.class);
    EasyMock.expect(groupRoleProvider.getRolesForGroup(GROUP_ROLE)).andReturn(new ArrayList<>(groupRoles)).anyTimes();
    EasyMock.replay(org, securityService, groupRoleProvider, mockUser);
}
Also used : Role(org.opencastproject.security.api.Role) Organization(org.opencastproject.security.api.Organization) User(org.opencastproject.security.api.User) JpaGroupRoleProvider(org.opencastproject.userdirectory.JpaGroupRoleProvider) SecurityService(org.opencastproject.security.api.SecurityService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

User (org.opencastproject.security.api.User)156 Organization (org.opencastproject.security.api.Organization)61 JaxbUser (org.opencastproject.security.api.JaxbUser)60 JaxbRole (org.opencastproject.security.api.JaxbRole)49 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)44 SecurityService (org.opencastproject.security.api.SecurityService)43 NotFoundException (org.opencastproject.util.NotFoundException)32 Before (org.junit.Before)31 Test (org.junit.Test)27 ArrayList (java.util.ArrayList)26 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)24 JaxbOrganization (org.opencastproject.security.api.JaxbOrganization)23 AccessControlList (org.opencastproject.security.api.AccessControlList)21 Role (org.opencastproject.security.api.Role)21 UserDirectoryService (org.opencastproject.security.api.UserDirectoryService)21 HashSet (java.util.HashSet)20 OrganizationDirectoryService (org.opencastproject.security.api.OrganizationDirectoryService)18 JpaUser (org.opencastproject.security.impl.jpa.JpaUser)17 IOException (java.io.IOException)16 MediaPackage (org.opencastproject.mediapackage.MediaPackage)16