Search in sources :

Example 71 with User

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);
    }
}
Also used : Organization(org.opencastproject.security.api.Organization) User(org.opencastproject.security.api.User)

Example 72 with User

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);
    }
}
Also used : JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) Organization(org.opencastproject.security.api.Organization) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) NotFoundException(org.opencastproject.util.NotFoundException) Tuple(org.opencastproject.util.data.Tuple)

Example 73 with User

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);
    }
}
Also used : JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) Organization(org.opencastproject.security.api.Organization) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser)

Example 74 with User

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);
}
Also used : User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) JaxbRole(org.opencastproject.security.api.JaxbRole) SecurityService(org.opencastproject.security.api.SecurityService) JaxbUser(org.opencastproject.security.api.JaxbUser) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Before(org.junit.Before)

Example 75 with User

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) JaxbRole(org.opencastproject.security.api.JaxbRole) SecurityService(org.opencastproject.security.api.SecurityService) HttpServletResponse(javax.servlet.http.HttpServletResponse) JaxbUser(org.opencastproject.security.api.JaxbUser) IOException(java.io.IOException) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Test(org.junit.Test)

Aggregations

User (org.opencastproject.security.api.User)156 Organization (org.opencastproject.security.api.Organization)61 JaxbUser (org.opencastproject.security.api.JaxbUser)60 JaxbRole (org.opencastproject.security.api.JaxbRole)49 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)44 SecurityService (org.opencastproject.security.api.SecurityService)43 NotFoundException (org.opencastproject.util.NotFoundException)32 Before (org.junit.Before)31 Test (org.junit.Test)27 ArrayList (java.util.ArrayList)26 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)24 JaxbOrganization (org.opencastproject.security.api.JaxbOrganization)23 AccessControlList (org.opencastproject.security.api.AccessControlList)21 Role (org.opencastproject.security.api.Role)21 UserDirectoryService (org.opencastproject.security.api.UserDirectoryService)21 HashSet (java.util.HashSet)20 OrganizationDirectoryService (org.opencastproject.security.api.OrganizationDirectoryService)18 JpaUser (org.opencastproject.security.impl.jpa.JpaUser)17 IOException (java.io.IOException)16 MediaPackage (org.opencastproject.mediapackage.MediaPackage)16