use of org.opencastproject.security.api.User in project opencast by opencast.
the class SecurityContext method runInContext.
/**
* Run function <code>f</code> within the context.
*/
public <A> A runInContext(Function0<A> f) {
final Organization prevOrg = sec.getOrganization();
// workaround: if no organization is bound to the current thread sec.getUser() will throw a NPE
final User prevUser = prevOrg != null ? sec.getUser() : null;
sec.setOrganization(org);
sec.setUser(user);
try {
return f.apply();
} finally {
sec.setOrganization(prevOrg);
sec.setUser(prevUser);
}
}
use of org.opencastproject.security.api.User in project opencast by opencast.
the class SecurityUtil method getUserAndOrganization.
/**
* Get a user and an organization. Only returns something if both elements can be determined.
*/
public static Option<Tuple<User, Organization>> getUserAndOrganization(SecurityService sec, OrganizationDirectoryService orgDir, String orgId, UserDirectoryService userDir, String userId) {
final Organization prevOrg = sec.getOrganization();
try {
final Organization org = orgDir.getOrganization(orgId);
sec.setOrganization(org);
return option(userDir.loadUser(userId)).fmap(new Function<User, Tuple<User, Organization>>() {
@Override
public Tuple<User, Organization> apply(User user) {
return tuple(user, org);
}
});
} catch (NotFoundException e) {
return none();
} finally {
sec.setOrganization(prevOrg);
}
}
use of org.opencastproject.security.api.User in project opencast by opencast.
the class SecurityUtil method runAs.
/**
* Run function <code>f</code> in the context described by the given organization and user.
*
* @return the function's outcome.
*/
public static <A> A runAs(SecurityService sec, Organization org, User user, Function0<A> f) {
final Organization prevOrg = sec.getOrganization();
final User prevUser = prevOrg != null ? sec.getUser() : null;
sec.setOrganization(org);
sec.setUser(user);
try {
return f.apply();
} finally {
sec.setOrganization(prevOrg);
sec.setUser(prevUser);
}
}
use of org.opencastproject.security.api.User in project opencast by opencast.
the class TrustedAnonymousAthenticationFilterTest method setUp.
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
securityService = EasyMock.createNiceMock(SecurityService.class);
User user = new JaxbUser("admin", "test", new DefaultOrganization(), new JaxbRole(SecurityConstants.GLOBAL_ADMIN_ROLE, new DefaultOrganization()));
EasyMock.expect(securityService.getOrganization()).andReturn(new DefaultOrganization()).anyTimes();
EasyMock.expect(securityService.getUser()).andReturn(user).anyTimes();
EasyMock.replay(securityService);
}
use of org.opencastproject.security.api.User in project opencast by opencast.
the class RemoteUserAndOrganizationFilterTest method testRolesSwitching.
@Test
public void testRolesSwitching() throws IOException {
SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
filter.setSecurityService(securityService);
EasyMock.expect(securityService.getOrganization()).andReturn(new DefaultOrganization()).anyTimes();
EasyMock.expect(securityService.getUser()).andAnswer(userResponder).anyTimes();
securityService.setUser(EasyMock.anyObject(User.class));
EasyMock.expectLastCall().times(2);
EasyMock.replay(securityService);
User defaultUser = new JaxbUser("admin", "test", new DefaultOrganization(), new JaxbRole(SecurityConstants.GLOBAL_SUDO_ROLE, new DefaultOrganization()));
userResponder.setResponse(defaultUser);
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(request.getHeader(SecurityConstants.ROLES_HEADER)).andReturn("ROLE_TEST,ROLE_USER").anyTimes();
EasyMock.replay(request);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.replay(response);
try {
filter.doFilter(request, response, chain);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
EasyMock.verify(securityService);
}
Aggregations