use of org.opencastproject.security.impl.jpa.JpaUser in project opencast by opencast.
the class UserEndpoint method createUser.
@POST
@Path("/")
@RestQuery(name = "createUser", description = "Create a new user", returnDescription = "Location of the new ressource", restParameters = { @RestParameter(name = "username", description = "The username.", isRequired = true, type = STRING), @RestParameter(name = "password", description = "The password.", isRequired = true, type = STRING), @RestParameter(name = "name", description = "The name.", isRequired = false, type = STRING), @RestParameter(name = "email", description = "The email.", isRequired = false, type = STRING), @RestParameter(name = "roles", description = "The user roles as a json array, for example: [\"ROLE_USER\", \"ROLE_ADMIN\"]", isRequired = false, type = STRING) }, reponses = { @RestResponse(responseCode = SC_BAD_REQUEST, description = "Malformed request syntax."), @RestResponse(responseCode = SC_CREATED, description = "User has been created."), @RestResponse(responseCode = SC_CONFLICT, description = "An user with this username already exist."), @RestResponse(responseCode = SC_FORBIDDEN, description = "Not enough permissions to create a user with the admin role.") })
public Response createUser(@FormParam("username") String username, @FormParam("password") String password, @FormParam("name") String name, @FormParam("email") String email, @FormParam("roles") String roles) {
if (jpaUserAndRoleProvider.loadUser(username) != null) {
return Response.status(SC_CONFLICT).build();
}
try {
Set<JpaRole> rolesSet = parseRoles(roles);
/* Add new user */
logger.debug("Updating user {}", username);
JpaOrganization organization = (JpaOrganization) securityService.getOrganization();
JpaUser user = new JpaUser(username, password, organization, name, email, jpaUserAndRoleProvider.getName(), true, rolesSet);
try {
jpaUserAndRoleProvider.addUser(user);
return Response.created(uri(endpointBaseUrl, user.getUsername() + ".json")).build();
} catch (UnauthorizedException ex) {
logger.debug("Create user failed", ex);
return Response.status(Response.Status.FORBIDDEN).build();
}
} catch (IllegalArgumentException e) {
logger.debug("Request with malformed ROLE data: {}", roles);
return Response.status(SC_BAD_REQUEST).build();
}
}
use of org.opencastproject.security.impl.jpa.JpaUser in project opencast by opencast.
the class UserEndpoint method setUser.
@PUT
@Path("{username}.json")
@RestQuery(name = "updateUser", description = "Update an user", returnDescription = "Status ok", restParameters = { @RestParameter(name = "password", description = "The password.", isRequired = true, type = STRING), @RestParameter(name = "name", description = "The name.", isRequired = false, type = STRING), @RestParameter(name = "email", description = "The email.", isRequired = false, type = STRING), @RestParameter(name = "roles", description = "The user roles as a json array, for example: [\"ROLE_USER\", \"ROLE_ADMIN\"]", isRequired = false, type = STRING) }, pathParameters = @RestParameter(name = "username", description = "The username", isRequired = true, type = STRING), reponses = { @RestResponse(responseCode = SC_BAD_REQUEST, description = "Malformed request syntax."), @RestResponse(responseCode = SC_FORBIDDEN, description = "Not enough permissions to update a user with the admin role."), @RestResponse(responseCode = SC_OK, description = "User has been updated.") })
public Response setUser(@PathParam("username") String username, @FormParam("password") String password, @FormParam("name") String name, @FormParam("email") String email, @FormParam("roles") String roles) {
try {
User user = jpaUserAndRoleProvider.loadUser(username);
if (user == null) {
return createUser(username, password, name, email, roles);
}
Set<JpaRole> rolesSet = parseRoles(roles);
logger.debug("Updating user {}", username);
JpaOrganization organization = (JpaOrganization) securityService.getOrganization();
jpaUserAndRoleProvider.updateUser(new JpaUser(username, password, organization, name, email, jpaUserAndRoleProvider.getName(), true, rolesSet));
return Response.status(SC_OK).build();
} catch (NotFoundException e) {
logger.debug("User {} not found.", username);
return Response.status(SC_NOT_FOUND).build();
} catch (UnauthorizedException e) {
logger.debug("Update user failed", e);
return Response.status(Response.Status.FORBIDDEN).build();
} catch (IllegalArgumentException e) {
logger.debug("Request with malformed ROLE data: {}", roles);
return Response.status(SC_BAD_REQUEST).build();
}
}
use of org.opencastproject.security.impl.jpa.JpaUser in project opencast by opencast.
the class JpaUserProviderTest method testFindUsers.
@Test
public void testFindUsers() throws UnauthorizedException {
JpaUser userOne = createUserWithRoles(org1, "user_test_1", "ROLE_COOL_ONE");
JpaUser userTwo = createUserWithRoles(org1, "user2", "ROLE_COOL_ONE");
JpaUser userThree = createUserWithRoles(org1, "user3", "ROLE_COOL_ONE");
JpaUser userFour = createUserWithRoles(org1, "user_test_4", "ROLE_COOL_ONE");
provider.addUser(userOne);
provider.addUser(userTwo);
provider.addUser(userThree);
provider.addUser(userFour);
assertEquals(2, IteratorUtils.toList(provider.findUsers("%tEsT%", 0, 0)).size());
assertEquals(1, IteratorUtils.toList(provider.findUsers("%tEsT%", 0, 1)).size());
User user = provider.findUsers("%tEsT%", 1, 1).next();
assertEquals(userFour, user);
}
use of org.opencastproject.security.impl.jpa.JpaUser in project opencast by opencast.
the class JpaUserProviderTest method testDeleteUser.
@Test
public void testDeleteUser() throws Exception {
JpaUser user1 = createUserWithRoles(org1, "user1", "ROLE_ASTRO_101_SPRING_2011_STUDENT");
JpaUser user2 = createUserWithRoles(org1, "user2", "ROLE_ASTRO_101_SPRING_2011_STUDENT");
JpaUser user3 = createUserWithRoles(org1, "user3", "ROLE_ASTRO_101_SPRING_2011_STUDENT");
JpaUser user4 = createUserWithRoles(org1, "user4", "ROLE_ASTRO_101_SPRING_2011_STUDENT");
provider.addUser(user1);
provider.addUser(user2);
provider.addUser(user3);
provider.addUser(user4);
User loadUser = provider.loadUser("user1");
assertNotNull(loadUser);
provider.deleteUser("user1", user1.getOrganization().getId());
provider.deleteUser("user2", user1.getOrganization().getId());
provider.deleteUser("user3", user1.getOrganization().getId());
assertNull(provider.loadUser("user1", org1.getId()));
assertNull(provider.loadUser("user2", org1.getId()));
assertNull(provider.loadUser("user3", org1.getId()));
assertNotNull(provider.loadUser("user4", org1.getId()));
try {
provider.deleteUser("user1", user1.getOrganization().getId());
fail("Should throw a NotFoundException");
} catch (NotFoundException e) {
assertTrue("User not found.", true);
}
}
use of org.opencastproject.security.impl.jpa.JpaUser in project opencast by opencast.
the class JpaUserProviderTest method testAddUserWithGlobalAdminRole.
@Test
public void testAddUserWithGlobalAdminRole() throws Exception {
JpaUser adminUser = createUserWithRoles(org1, "admin1", SecurityConstants.GLOBAL_ADMIN_ROLE);
provider.addUser(adminUser);
User loadedUser = provider.loadUser(adminUser.getUsername());
assertNotNull("The currently added user isn't loaded as expected", loadedUser);
assertEquals(adminUser.getUsername(), loadedUser.getUsername());
assertEquals(adminUser.getRoles(), loadedUser.getRoles());
}
Aggregations