use of org.opencastproject.security.impl.jpa.JpaOrganization 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.JpaOrganization in project opencast by opencast.
the class UserEndpoint method parseRoles.
/**
* Parse JSON roles array.
*
* @param roles
* String representation of JSON array containing roles
*/
private Set<JpaRole> parseRoles(String roles) throws IllegalArgumentException {
JSONArray rolesArray = null;
/* Try parsing JSON. Return Bad Request if malformed. */
try {
rolesArray = (JSONArray) JSONValue.parseWithException(StringUtils.isEmpty(roles) ? "[]" : roles);
} catch (Exception e) {
throw new IllegalArgumentException("Error parsing JSON array", e);
}
Set<JpaRole> rolesSet = new HashSet<JpaRole>();
/* Add given roles */
for (Object role : rolesArray) {
try {
rolesSet.add(new JpaRole((String) role, (JpaOrganization) securityService.getOrganization()));
} catch (ClassCastException e) {
throw new IllegalArgumentException("Error parsing array vales as String", e);
}
}
return rolesSet;
}
use of org.opencastproject.security.impl.jpa.JpaOrganization in project opencast by opencast.
the class JpaUserProviderTest method setUp.
@Before
public void setUp() throws Exception {
org1 = new JpaOrganization("org1", "org1", "localhost", 80, "admin", "anon", null);
org2 = new JpaOrganization("org2", "org2", "127.0.0.1", 80, "admin", "anon", null);
SecurityService securityService = mockSecurityServiceWithUser(createUserWithRoles(org1, "admin", SecurityConstants.GLOBAL_SYSTEM_ROLES));
JpaGroupRoleProvider groupRoleProvider = EasyMock.createNiceMock(JpaGroupRoleProvider.class);
provider = new JpaUserAndRoleProvider();
provider.setSecurityService(securityService);
provider.setEntityManagerFactory(newTestEntityManagerFactory(JpaUserAndRoleProvider.PERSISTENCE_UNIT));
provider.setGroupRoleProvider(groupRoleProvider);
provider.activate(null);
}
use of org.opencastproject.security.impl.jpa.JpaOrganization in project opencast by opencast.
the class ConfigurableLoginHandler method extractRoles.
/**
* Extracts the roles from the request.
*
* @param request
* the request
* @return the roles
*/
private Set<JpaRole> extractRoles(String id, HttpServletRequest request) {
JpaOrganization organization = fromOrganization(securityService.getOrganization());
Set<JpaRole> roles = new HashSet<JpaRole>();
roles.add(new JpaRole(roleFederationMember, organization));
roles.add(new JpaRole(roleUserPrefix + id, organization));
roles.add(new JpaRole(organization.getAnonymousRole(), organization));
if (headerHomeOrganization != null) {
String homeOrganization = request.getHeader(headerHomeOrganization);
roles.add(new JpaRole(roleOrganizationPrefix + homeOrganization + roleOrganizationSuffix, organization));
}
if (StringUtils.equals(id, bootstrapUserId)) {
roles.add(new JpaRole(GLOBAL_ADMIN_ROLE, organization));
}
return roles;
}
Aggregations