Search in sources :

Example 6 with JpaOrganization

use of org.opencastproject.security.impl.jpa.JpaOrganization in project opencast by opencast.

the class OrganizationPersistenceTest method testList.

@Test
public void testList() throws Exception {
    Map<String, String> orgProperties = new HashMap<String, String>();
    orgProperties.put("test", "one");
    JpaOrganization org1 = new JpaOrganization("newOrg", "test organization", "test.org", 8080, "ROLE_TEST_ADMIN", "ROLE_TEST_ANONYMOUS", orgProperties);
    organizationDatabase.storeOrganization(org1);
    orgProperties.put("test", "one");
    orgProperties.put("test2", "two");
    JpaOrganization org2 = new JpaOrganization("newOrg2", "test organization 2", "test2.org", 8081, "ROLE_TEST2_ADMIN", "ROLE_TEST2_ANONYMOUS", orgProperties);
    organizationDatabase.storeOrganization(org2);
    Assert.assertEquals(2, organizationDatabase.countOrganizations());
    List<Organization> organizations = organizationDatabase.getOrganizations();
    Assert.assertEquals(2, organizations.size());
    Assert.assertEquals(org1, organizations.get(0));
    Assert.assertEquals(org2, organizations.get(1));
}
Also used : DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Organization(org.opencastproject.security.api.Organization) JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 7 with JpaOrganization

use of org.opencastproject.security.impl.jpa.JpaOrganization in project opencast by opencast.

the class OrganizationPersistenceTest method testDeleting.

@Test
public void testDeleting() throws Exception {
    Map<String, String> orgProperties = new HashMap<String, String>();
    orgProperties.put("test", "one");
    JpaOrganization org = new JpaOrganization("newOrg", "test organization", "test.org", 8080, "ROLE_TEST_ADMIN", "ROLE_TEST_ANONYMOUS", orgProperties);
    organizationDatabase.storeOrganization(org);
    Assert.assertTrue(organizationDatabase.containsOrganization("newOrg"));
    try {
        organizationDatabase.getOrganization("newOrg");
    } catch (NotFoundException e) {
        Assert.fail("Organization not found");
    }
    organizationDatabase.deleteOrganization("newOrg");
    Assert.assertFalse(organizationDatabase.containsOrganization("newOrg"));
    try {
        organizationDatabase.getOrganization("newOrg");
        Assert.fail("Organization found");
    } catch (NotFoundException e) {
        Assert.assertNotNull(e);
    }
}
Also used : JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) HashMap(java.util.HashMap) NotFoundException(org.opencastproject.util.NotFoundException) Test(org.junit.Test)

Example 8 with JpaOrganization

use of org.opencastproject.security.impl.jpa.JpaOrganization in project opencast by opencast.

the class OrganizationPersistenceTest method testAdding.

@Test
public void testAdding() throws Exception {
    Map<String, String> orgProperties = new HashMap<String, String>();
    orgProperties.put("test", "one");
    JpaOrganization org = new JpaOrganization("newOrg", "test organization", "test.org", 8080, "ROLE_TEST_ADMIN", "ROLE_TEST_ANONYMOUS", orgProperties);
    organizationDatabase.storeOrganization(org);
    Assert.assertTrue(organizationDatabase.containsOrganization("newOrg"));
    Organization orgById = organizationDatabase.getOrganization("newOrg");
    try {
        organizationDatabase.getOrganizationByHost("test.org", 8081);
        Assert.fail();
    } catch (NotFoundException e) {
        Assert.assertNotNull(e);
    }
    Organization orgByHost = organizationDatabase.getOrganizationByHost("test.org", 8080);
    Assert.assertEquals(orgById, orgByHost);
    Assert.assertEquals("newOrg", orgById.getId());
    Assert.assertEquals("test organization", orgById.getName());
    Assert.assertEquals("ROLE_TEST_ADMIN", orgById.getAdminRole());
    Assert.assertEquals("ROLE_TEST_ANONYMOUS", orgById.getAnonymousRole());
    Map<String, Integer> servers = orgById.getServers();
    Assert.assertEquals(1, servers.size());
    Assert.assertTrue(servers.containsKey("test.org"));
    Assert.assertTrue(servers.containsValue(8080));
    Map<String, String> properties = orgById.getProperties();
    Assert.assertEquals(1, properties.size());
    Assert.assertTrue(properties.containsKey("test"));
    Assert.assertTrue(properties.containsValue("one"));
}
Also used : DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Organization(org.opencastproject.security.api.Organization) JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) HashMap(java.util.HashMap) NotFoundException(org.opencastproject.util.NotFoundException) Test(org.junit.Test)

Example 9 with JpaOrganization

use of org.opencastproject.security.impl.jpa.JpaOrganization in project opencast by opencast.

the class UsersEndpoint method updateUser.

@PUT
@Path("{username}.json")
@RestQuery(name = "updateUser", description = "Update an user", returnDescription = "Status ok", restParameters = { @RestParameter(description = "The password.", isRequired = false, name = "password", type = STRING), @RestParameter(description = "The name.", isRequired = false, name = "name", type = STRING), @RestParameter(description = "The email.", isRequired = false, name = "email", type = STRING), @RestParameter(name = "roles", type = STRING, isRequired = false, description = "The user roles as a json array") }, pathParameters = @RestParameter(name = "username", type = STRING, isRequired = true, description = "The username"), reponses = { @RestResponse(responseCode = SC_OK, description = "User has been updated."), @RestResponse(responseCode = SC_FORBIDDEN, description = "Not enough permissions to update a user with admin role."), @RestResponse(responseCode = SC_NOT_FOUND, description = "User not found.") })
public Response updateUser(@PathParam("username") String username, @FormParam("password") String password, @FormParam("name") String name, @FormParam("email") String email, @FormParam("roles") String roles) throws NotFoundException {
    User user = jpaUserAndRoleProvider.loadUser(username);
    if (user == null) {
        throw new NotFoundException("User " + username + " does not exist.");
    }
    JpaOrganization organization = (JpaOrganization) securityService.getOrganization();
    Set<JpaRole> rolesSet = new HashSet<>();
    Option<JSONArray> rolesArray = Option.none();
    if (StringUtils.isNotBlank(roles)) {
        rolesArray = Option.some((JSONArray) JSONValue.parse(roles));
    }
    if (rolesArray.isSome()) {
        // Add the roles given
        for (Object roleObj : rolesArray.get()) {
            JSONObject role = (JSONObject) roleObj;
            String rolename = (String) role.get("id");
            Role.Type roletype = Role.Type.valueOf((String) role.get("type"));
            rolesSet.add(new JpaRole(rolename, organization, null, roletype));
        }
    } else {
        // Or the use the one from the user if no one is given
        for (Role role : user.getRoles()) {
            rolesSet.add(new JpaRole(role.getName(), organization, role.getDescription(), role.getType()));
        }
    }
    try {
        jpaUserAndRoleProvider.updateUser(new JpaUser(username, password, organization, name, email, jpaUserAndRoleProvider.getName(), true, rolesSet));
        userDirectoryService.invalidate(username);
        return Response.status(SC_OK).build();
    } catch (UnauthorizedException ex) {
        return Response.status(Response.Status.FORBIDDEN).build();
    }
}
Also used : JpaUser(org.opencastproject.security.impl.jpa.JpaUser) User(org.opencastproject.security.api.User) JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) JSONArray(org.json.simple.JSONArray) NotFoundException(org.opencastproject.util.NotFoundException) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) Role(org.opencastproject.security.api.Role) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) JSONObject(org.json.simple.JSONObject) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) JSONObject(org.json.simple.JSONObject) JObject(com.entwinemedia.fn.data.json.JObject) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) RestQuery(org.opencastproject.util.doc.rest.RestQuery) PUT(javax.ws.rs.PUT)

Example 10 with JpaOrganization

use of org.opencastproject.security.impl.jpa.JpaOrganization in project opencast by opencast.

the class OrganizationDatabaseImpl method deleteOrganization.

/**
 * @see org.opencastproject.kernel.security.persistence.OrganizationDatabase#deleteOrganization(java.lang.String)
 */
@Override
public void deleteOrganization(String orgId) throws OrganizationDatabaseException, NotFoundException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        JpaOrganization organization = getOrganizationEntity(orgId, em);
        if (organization == null)
            throw new NotFoundException("Organization " + orgId + " does not exist");
        em.remove(organization);
        tx.commit();
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Could not delete organization: {}", e.getMessage());
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new OrganizationDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) NotFoundException(org.opencastproject.util.NotFoundException) NotFoundException(org.opencastproject.util.NotFoundException) NoResultException(javax.persistence.NoResultException)

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