use of org.opencastproject.security.impl.jpa.JpaUser in project opencast by opencast.
the class UserDirectoryPersistenceUtil method findUser.
/**
* Returns the persisted user by the user name and organization id
*
* @param userName
* the user name
* @param organizationId
* the organization id
* @param emf
* the entity manager factory
* @return the user or <code>null</code> if not found
*/
public static JpaUser findUser(String userName, String organizationId, EntityManagerFactory emf) {
EntityManager em = null;
try {
em = emf.createEntityManager();
Query q = em.createNamedQuery("User.findByUsername");
q.setParameter("u", userName);
q.setParameter("org", organizationId);
return (JpaUser) q.getSingleResult();
} catch (NoResultException e) {
return null;
} finally {
if (em != null)
em.close();
}
}
use of org.opencastproject.security.impl.jpa.JpaUser in project opencast by opencast.
the class JpaGroupRoleProviderTest method setUp.
@Before
public void setUp() throws Exception {
JpaUser adminUser = new JpaUser("admin", "pass1", org1, "Admin", "admin@localhost", "opencast", true, Collections.set(new JpaRole(SecurityConstants.GLOBAL_ADMIN_ROLE, org1)));
// Set the security sevice
SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
EasyMock.expect(securityService.getUser()).andReturn(adminUser).anyTimes();
EasyMock.expect(securityService.getOrganization()).andReturn(org1).anyTimes();
EasyMock.replay(securityService);
// Create the message sender service
MessageSender messageSender = EasyMock.createNiceMock(MessageSender.class);
messageSender.sendObjectMessage(EasyMock.anyObject(String.class), EasyMock.anyObject(MessageSender.DestinationType.class), EasyMock.anyObject(Serializable.class));
EasyMock.expectLastCall();
EasyMock.replay(messageSender);
provider = new JpaGroupRoleProvider();
provider.setSecurityService(securityService);
provider.setMessageSender(messageSender);
provider.setEntityManagerFactory(newTestEntityManagerFactory(JpaUserAndRoleProvider.PERSISTENCE_UNIT));
provider.activate(null);
}
use of org.opencastproject.security.impl.jpa.JpaUser in project opencast by opencast.
the class JpaGroupRoleProviderTest method testUpdateGroupNotAllowedAsNonAdminUser.
@Test
public void testUpdateGroupNotAllowedAsNonAdminUser() throws UnauthorizedException {
JpaGroup group = new JpaGroup("test", org1, "Test", "Test group", Collections.set(new JpaRole(SecurityConstants.GLOBAL_ADMIN_ROLE, org1)));
try {
provider.addGroup(group);
Group loadGroup = provider.loadGroup(group.getGroupId(), group.getOrganization().getId());
assertNotNull(loadGroup);
assertEquals(loadGroup.getGroupId(), loadGroup.getGroupId());
} catch (Exception e) {
fail("The group schould be added");
}
JpaUser user = new JpaUser("user", "pass1", org1, "User", "user@localhost", "opencast", true, Collections.set(new JpaRole("ROLE_USER", org1)));
// Set the security sevice
SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
EasyMock.expect(securityService.getUser()).andReturn(user).anyTimes();
EasyMock.expect(securityService.getOrganization()).andReturn(org1).anyTimes();
EasyMock.replay(securityService);
provider.setSecurityService(securityService);
try {
// try add ROLE_USER
Response updateGroupResponse = provider.updateGroup(group.getGroupId(), group.getName(), group.getDescription(), "ROLE_USER, " + SecurityConstants.GLOBAL_ADMIN_ROLE, null);
assertNotNull(updateGroupResponse);
assertEquals(HttpStatus.SC_FORBIDDEN, updateGroupResponse.getStatus());
// try remove ROLE_ADMIN
updateGroupResponse = provider.updateGroup(group.getGroupId(), group.getName(), group.getDescription(), "ROLE_USER", null);
assertNotNull(updateGroupResponse);
assertEquals(HttpStatus.SC_FORBIDDEN, updateGroupResponse.getStatus());
} catch (NotFoundException e) {
fail("The existing group isn't found");
}
}
use of org.opencastproject.security.impl.jpa.JpaUser in project opencast by opencast.
the class JpaUserProviderTest method testRoles.
@Test
public void testRoles() throws Exception {
JpaUser userOne = createUserWithRoles(org1, "user1", "ROLE_ONE");
provider.addUser(userOne);
Set<JpaRole> authoritiesTwo = new HashSet<JpaRole>();
authoritiesTwo.add(new JpaRole("ROLE_ONE", org1));
authoritiesTwo.add(new JpaRole("ROLE_TWO", org1));
JpaUser userTwo = createUserWithRoles(org1, "user2", "ROLE_ONE", "ROLE_TWO");
provider.addUser(userTwo);
assertEquals("There should be two roles", 2, IteratorUtils.toList(provider.getRoles()).size());
}
use of org.opencastproject.security.impl.jpa.JpaUser in project opencast by opencast.
the class JpaUserProviderTest method testRolesForUser.
@Test
public void testRolesForUser() {
JpaRole astroRole = new JpaRole("ROLE_ASTRO_105_SPRING_2013_STUDENT", org1, "Astro role");
provider.addRole(astroRole);
JpaUser userOne = createUserWithRoles(org1, "user1", "ROLE_ONE", "ROLE_TWO");
try {
provider.addUser(userOne);
} catch (UnauthorizedException e) {
fail("User should be created");
}
assertEquals("There should be three roles", 3, IteratorUtils.toList(provider.getRoles()).size());
List<Role> rolesForUser = provider.getRolesForUser("user1");
assertEquals("There should be two roles", 2, rolesForUser.size());
assertEquals("ROLE_ONE", rolesForUser.get(0).getName());
assertEquals("ROLE_TWO", rolesForUser.get(1).getName());
}
Aggregations