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