Search in sources :

Example 36 with JaxbUser

use of org.opencastproject.security.api.JaxbUser in project opencast by opencast.

the class OrganizationPersistenceTest 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);
    organizationDatabase = new OrganizationDatabaseImpl();
    organizationDatabase.setEntityManagerFactory(newTestEntityManagerFactory(PERSISTENCE_UNIT));
    organizationDatabase.setSecurityService(securityService);
    organizationDatabase.activate(null);
}
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 37 with JaxbUser

use of org.opencastproject.security.api.JaxbUser in project opencast by opencast.

the class SecurityServiceSpringImpl method getUser.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.security.api.SecurityService#getUser()
 */
@Override
public User getUser() throws IllegalStateException {
    Organization org = getOrganization();
    if (org == null)
        throw new IllegalStateException("No organization is set in security context");
    User delegatedUser = delegatedUserHolder.get();
    if (delegatedUser != null) {
        return delegatedUser;
    }
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    JaxbOrganization jaxbOrganization = JaxbOrganization.fromOrganization(org);
    if (auth != null) {
        Object principal = auth.getPrincipal();
        if ((principal != null) && (principal instanceof UserDetails)) {
            UserDetails userDetails = (UserDetails) principal;
            User user = null;
            // If user exists, fetch it from the userDirectory
            if (userDirectory != null) {
                user = userDirectory.loadUser(userDetails.getUsername());
                if (user == null) {
                    logger.debug("Authenticated user '{}' could not be found in any of the current UserProviders. Continuing anyway...", userDetails.getUsername());
                }
            } else {
                logger.debug("No UserDirectory was found when trying to search for user '{}'", userDetails.getUsername());
            }
            // Add the roles (authorities) in the security context
            Set<JaxbRole> roles = new HashSet<JaxbRole>();
            Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();
            if (authorities != null) {
                for (GrantedAuthority ga : authorities) {
                    roles.add(new JaxbRole(ga.getAuthority(), jaxbOrganization));
                }
            }
            if (user == null) {
                // No user was found. Create one to hold the auth information from the security context
                user = new JaxbUser(userDetails.getUsername(), null, jaxbOrganization, roles);
            } else {
                // Combine the existing user with the roles in the security context
                user = JaxbUser.fromUser(user, roles);
            }
            // Save the user to retrieve it quicker the next time(s) this method is called (by this thread)
            delegatedUserHolder.set(user);
            return user;
        }
    }
    // Return the anonymous user by default
    return SecurityUtil.createAnonymousUser(jaxbOrganization);
}
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) GrantedAuthority(org.springframework.security.core.GrantedAuthority) JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) JaxbUser(org.opencastproject.security.api.JaxbUser) UserDetails(org.springframework.security.core.userdetails.UserDetails) JaxbRole(org.opencastproject.security.api.JaxbRole) Authentication(org.springframework.security.core.Authentication) HashSet(java.util.HashSet)

Example 38 with JaxbUser

use of org.opencastproject.security.api.JaxbUser in project opencast by opencast.

the class XACMLSecurityTest method setUp.

@Before
public void setUp() throws Exception {
    authzService = new XACMLAuthorizationService();
    // Mock security service
    securityService = EasyMock.createMock(SecurityService.class);
    EasyMock.expect(securityService.getUser()).andAnswer(() -> new JaxbUser(currentUser, "test", organization, currentRoles)).anyTimes();
    // Mock workspace
    Workspace workspace = EasyMock.createMock(Workspace.class);
    final Capture<InputStream> in = EasyMock.newCapture();
    final Capture<URI> uri = EasyMock.newCapture();
    EasyMock.expect(workspace.put(EasyMock.anyString(), EasyMock.anyString(), EasyMock.anyString(), EasyMock.capture(in))).andAnswer(() -> {
        final File file = testFolder.newFile();
        FileOutputStream out = new FileOutputStream(file);
        IOUtils.copyLarge(in.getValue(), out);
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in.getValue());
        return file.toURI();
    }).anyTimes();
    EasyMock.expect(workspace.get(EasyMock.capture(uri))).andAnswer(() -> new File(uri.getValue())).anyTimes();
    EasyMock.expect(workspace.read(EasyMock.capture(uri))).andAnswer(() -> new FileInputStream(uri.getValue().getPath())).anyTimes();
    workspace.delete(EasyMock.anyObject(URI.class));
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(securityService, workspace);
    authzService.setWorkspace(workspace);
    authzService.setSecurityService(securityService);
}
Also used : SecurityService(org.opencastproject.security.api.SecurityService) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(de.schlichtherle.io.FileOutputStream) JaxbUser(org.opencastproject.security.api.JaxbUser) URI(java.net.URI) File(java.io.File) FileInputStream(java.io.FileInputStream) Workspace(org.opencastproject.workspace.api.Workspace) Before(org.junit.Before)

Example 39 with JaxbUser

use of org.opencastproject.security.api.JaxbUser in project opencast by opencast.

the class TestSearchIndex method createSecurityService.

public static final SecurityService createSecurityService(DefaultOrganization organization) {
    JaxbUser creator = new JaxbUser("creator", "password", "Creator", null, "test", organization, new HashSet<JaxbRole>());
    SecurityService securityService = createNiceMock(SecurityService.class);
    expect(securityService.getOrganization()).andReturn(organization).anyTimes();
    expect(securityService.getUser()).andReturn(creator).anyTimes();
    replay(securityService);
    return securityService;
}
Also used : JaxbRole(org.opencastproject.security.api.JaxbRole) SecurityService(org.opencastproject.security.api.SecurityService) JaxbUser(org.opencastproject.security.api.JaxbUser)

Example 40 with JaxbUser

use of org.opencastproject.security.api.JaxbUser in project opencast by opencast.

the class SchedulerMigrationServiceTest method setUp.

@Before
public void setUp() throws Exception {
    OrganizationDirectoryService orgDirService = createNiceMock(OrganizationDirectoryService.class);
    expect(orgDirService.getOrganization(anyString())).andReturn(new DefaultOrganization()).anyTimes();
    replay(orgDirService);
    SecurityService securityService = createNiceMock(SecurityService.class);
    expect(securityService.getOrganization()).andReturn(new DefaultOrganization()).anyTimes();
    expect(securityService.getUser()).andReturn(new JaxbUser()).anyTimes();
    replay(securityService);
    SchedulerTransaction schedulerTransaction = createNiceMock(SchedulerTransaction.class);
    replay(schedulerTransaction);
    SchedulerService schedulerService = createNiceMock(SchedulerService.class);
    expect(schedulerService.createTransaction(anyString())).andReturn(schedulerTransaction).anyTimes();
    expect(schedulerService.search(anyObject(Opt.class), anyObject(Opt.class), anyObject(Opt.class), anyObject(Opt.class), anyObject(Opt.class))).andReturn(new ArrayList<>());
    replay(schedulerService);
    Workspace workspace = createNiceMock(Workspace.class);
    expect(workspace.put(anyString(), anyString(), anyString(), anyObject(InputStream.class))).andReturn(new URI("test")).anyTimes();
    replay(workspace);
    AuthorizationService authorizationService = createNiceMock(AuthorizationService.class);
    replay(authorizationService);
    schedulerMigrationService.setAuthorizationService(authorizationService);
    schedulerMigrationService.setOrganizationDirectoryService(orgDirService);
    schedulerMigrationService.setSchedulerService(schedulerService);
    schedulerMigrationService.setSecurityService(securityService);
    schedulerMigrationService.setWorkspace(workspace);
}
Also used : SchedulerService(org.opencastproject.scheduler.api.SchedulerService) Opt(com.entwinemedia.fn.data.Opt) AuthorizationService(org.opencastproject.security.api.AuthorizationService) SecurityService(org.opencastproject.security.api.SecurityService) JaxbUser(org.opencastproject.security.api.JaxbUser) SchedulerTransaction(org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction) URI(java.net.URI) OrganizationDirectoryService(org.opencastproject.security.api.OrganizationDirectoryService) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Workspace(org.opencastproject.workspace.api.Workspace) Before(org.junit.Before)

Aggregations

JaxbUser (org.opencastproject.security.api.JaxbUser)63 JaxbRole (org.opencastproject.security.api.JaxbRole)54 User (org.opencastproject.security.api.User)47 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)46 SecurityService (org.opencastproject.security.api.SecurityService)44 Before (org.junit.Before)34 JaxbOrganization (org.opencastproject.security.api.JaxbOrganization)21 OrganizationDirectoryService (org.opencastproject.security.api.OrganizationDirectoryService)19 UserDirectoryService (org.opencastproject.security.api.UserDirectoryService)19 Test (org.junit.Test)15 Organization (org.opencastproject.security.api.Organization)15 Workspace (org.opencastproject.workspace.api.Workspace)15 HashSet (java.util.HashSet)14 URI (java.net.URI)12 BundleContext (org.osgi.framework.BundleContext)12 ComponentContext (org.osgi.service.component.ComponentContext)12 ServiceRegistryInMemoryImpl (org.opencastproject.serviceregistry.api.ServiceRegistryInMemoryImpl)11 File (java.io.File)10 Job (org.opencastproject.job.api.Job)8 InputStream (java.io.InputStream)7