Search in sources :

Example 26 with JpaOrganization

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();
    }
}
Also used : User(org.opencastproject.security.api.User) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) JaxbUser(org.opencastproject.security.api.JaxbUser) JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) Path(javax.ws.rs.Path) RestQuery(org.opencastproject.util.doc.rest.RestQuery) PUT(javax.ws.rs.PUT)

Example 27 with JpaOrganization

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;
}
Also used : JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) JSONArray(org.json.simple.JSONArray) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 28 with JpaOrganization

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);
}
Also used : JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) SecurityService(org.opencastproject.security.api.SecurityService) Before(org.junit.Before)

Example 29 with JpaOrganization

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;
}
Also used : JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) HashSet(java.util.HashSet)

Aggregations

JpaOrganization (org.opencastproject.security.impl.jpa.JpaOrganization)29 JpaRole (org.opencastproject.security.impl.jpa.JpaRole)13 NotFoundException (org.opencastproject.util.NotFoundException)13 EntityManager (javax.persistence.EntityManager)10 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)10 JpaUser (org.opencastproject.security.impl.jpa.JpaUser)8 HashSet (java.util.HashSet)7 EntityTransaction (javax.persistence.EntityTransaction)6 Path (javax.ws.rs.Path)6 RestQuery (org.opencastproject.util.doc.rest.RestQuery)6 HashMap (java.util.HashMap)5 NoResultException (javax.persistence.NoResultException)5 User (org.opencastproject.security.api.User)4 Query (javax.persistence.Query)3 POST (javax.ws.rs.POST)3 PUT (javax.ws.rs.PUT)3 JSONArray (org.json.simple.JSONArray)3 Before (org.junit.Before)3 Test (org.junit.Test)3 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)3