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);
}
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;
}
}
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();
}
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();
}
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);
}
Aggregations