use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.
the class UserIdRoleProvider method findRoles.
/**
* @see org.opencastproject.security.api.RoleProvider#findRoles(String,Role.Target, int, int)
*/
@Override
public Iterator<Role> findRoles(String query, Role.Target target, int offset, int limit) {
if (query == null)
throw new IllegalArgumentException("Query must be set");
// These roles are not meaningful for users/groups
if (target == Role.Target.USER) {
return Collections.emptyIterator();
}
logger.debug("findRoles(query={} offset={} limit={})", query, offset, limit);
HashSet<Role> foundRoles = new HashSet<Role>();
Organization organization = securityService.getOrganization();
// Return authenticated user role if it matches the query pattern
if (like(ROLE_USER, query)) {
foundRoles.add(new JaxbRole(ROLE_USER, JaxbOrganization.fromOrganization(organization), "The authenticated user role", Role.Type.SYSTEM));
}
// (iterating through users may be slow)
if (!"%".equals(query) && !query.startsWith(userRolePrefix)) {
return foundRoles.iterator();
}
String userQuery = "%";
if (query.startsWith(userRolePrefix)) {
userQuery = query.substring(userRolePrefix.length());
}
Iterator<User> users = userDirectoryService.findUsers(userQuery, offset, limit);
while (users.hasNext()) {
User u = users.next();
// We exclude the digest user, but then add the global ROLE_USER above
if (!"system".equals(u.getProvider())) {
foundRoles.add(new JaxbRole(getUserIdRole(u.getUsername()), JaxbOrganization.fromOrganization(u.getOrganization()), "User id role", Role.Type.SYSTEM));
}
}
return foundRoles.iterator();
}
use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.
the class UserIdRoleProvider method getRolesForUser.
/**
* @see org.opencastproject.security.api.RoleProvider#getRolesForUser(String)
*/
@Override
public List<Role> getRolesForUser(String userName) {
Organization organization = securityService.getOrganization();
List<Role> roles = new ArrayList<Role>();
roles.add(new JaxbRole(getUserIdRole(userName), JaxbOrganization.fromOrganization(organization), "The user id role", Role.Type.SYSTEM));
roles.add(new JaxbRole(ROLE_USER, JaxbOrganization.fromOrganization(organization), "The authenticated user role", Role.Type.SYSTEM));
return Collections.unmodifiableList(roles);
}
use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.
the class UserTrackingRestServiceTest method setUp.
@Before
public void setUp() throws UserTrackingException {
SecurityService security = EasyMock.createMock(SecurityService.class);
EasyMock.expect(security.getUser()).andReturn(new JaxbUser(MOCK_USER, "test", new DefaultOrganization(), new JaxbRole("ROLE_USER", new DefaultOrganization()))).anyTimes();
BundleContext bc = EasyMock.createMock(BundleContext.class);
EasyMock.expect(bc.getProperty(OpencastConstants.SERVER_URL_PROPERTY)).andReturn("http://www.example.org:8080").anyTimes();
@SuppressWarnings("rawtypes") Dictionary dict = EasyMock.createMock(Dictionary.class);
EasyMock.expect(dict.get(RestConstants.SERVICE_PATH_PROPERTY)).andReturn("/usertracking").anyTimes();
ComponentContext context = EasyMock.createMock(ComponentContext.class);
EasyMock.expect(context.getBundleContext()).andReturn(bc).anyTimes();
EasyMock.expect(context.getProperties()).andReturn(dict).anyTimes();
UserActionImpl ua = EasyMock.createMock(UserActionImpl.class);
EasyMock.expect(ua.getId()).andReturn(4L).anyTimes();
UserTrackingService usertracking = EasyMock.createMock(UserTrackingService.class);
EasyMock.expect(usertracking.addUserFootprint(EasyMock.isA(UserAction.class), EasyMock.isA(UserSession.class))).andReturn(ua).anyTimes();
EasyMock.replay(security, bc, dict, context, ua, usertracking);
service = new UserTrackingRestService();
service.setSecurityService(security);
service.setService(usertracking);
service.activate(context);
}
use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.
the class VideoEditorTest method setUp.
/**
* Setup for the video editor service, including creation of a mock workspace and all dependencies.
*
* @throws Exception
* if setup fails
*/
@Before
public void setUp() throws Exception {
File tmpDir = folder.newFolder(getClass().getName());
// output file
tempFile1 = new File(tmpDir, "testoutput.mp4");
/* mock the workspace for the input/output file */
// workspace.get(new URI(sourceTrackUri));
Workspace workspace = EasyMock.createMock(Workspace.class);
EasyMock.expect(workspace.rootDirectory()).andReturn(tmpDir.getAbsolutePath());
EasyMock.expect(workspace.get(track1.getURI())).andReturn(new File(track1.getURI())).anyTimes();
EasyMock.expect(workspace.get(track2.getURI())).andReturn(new File(track2.getURI())).anyTimes();
EasyMock.expect(workspace.putInCollection(EasyMock.anyString(), EasyMock.anyString(), EasyMock.anyObject(InputStream.class))).andAnswer(() -> {
InputStream in = (InputStream) EasyMock.getCurrentArguments()[2];
IOUtils.copy(in, new FileOutputStream(tempFile1));
return tempFile1.toURI();
});
/* mock the role/org/security dependencies */
User anonymous = new JaxbUser("anonymous", "test", new DefaultOrganization(), new JaxbRole(DefaultOrganization.DEFAULT_ORGANIZATION_ANONYMOUS, new DefaultOrganization()));
UserDirectoryService userDirectoryService = EasyMock.createMock(UserDirectoryService.class);
EasyMock.expect(userDirectoryService.loadUser((String) EasyMock.anyObject())).andReturn(anonymous).anyTimes();
Organization organization = new DefaultOrganization();
OrganizationDirectoryService organizationDirectoryService = EasyMock.createMock(OrganizationDirectoryService.class);
EasyMock.expect(organizationDirectoryService.getOrganization((String) EasyMock.anyObject())).andReturn(organization).anyTimes();
SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
EasyMock.expect(securityService.getUser()).andReturn(anonymous).anyTimes();
EasyMock.expect(securityService.getOrganization()).andReturn(organization).anyTimes();
/* mock the osgi init for the video editor itself */
BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
File storageDir = folder.newFolder();
logger.info("storageDir: {}", storageDir);
EasyMock.expect(bc.getProperty("org.opencastproject.storage.dir")).andReturn(storageDir.getPath()).anyTimes();
EasyMock.expect(bc.getProperty("org.opencastproject.composer.ffmpegpath")).andReturn(FFMPEG_BINARY).anyTimes();
EasyMock.expect(bc.getProperty(FFmpegAnalyzer.FFPROBE_BINARY_CONFIG)).andReturn("ffprobe").anyTimes();
ComponentContext cc = EasyMock.createNiceMock(ComponentContext.class);
EasyMock.expect(cc.getBundleContext()).andReturn(bc).anyTimes();
EasyMock.replay(bc, cc, workspace, userDirectoryService, organizationDirectoryService, securityService);
/* mock inspector output so that the job will alway pass */
String sourceTrackXml = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" + "<track xmlns=\"http://mediapackage.opencastproject.org\" type='presentation/source' id='deadbeef-a926-4ba9-96d9-2fafbcc30d2a'>" + "<audio id='audio-1'><encoder type='MP3 (MPEG audio layer 3)'/><channels>2</channels>" + "<bitrate>96000.0</bitrate></audio><video id='video-1'><device/>" + "<encoder type='FLV / Sorenson Spark / Sorenson H.263 (Flash Video)'/>" + "<bitrate>512000.0</bitrate><framerate>15.0</framerate>" + "<resolution>854x480</resolution></video>" + "<mimetype>video/mpeg</mimetype><url>video.mp4</url></track>";
inspectedTrack = (Track) MediaPackageElementParser.getFromXml(sourceTrackXml);
veditor = new VideoEditorServiceImpl() {
@Override
protected Job inspect(Job job, URI workspaceURI) throws MediaInspectionException, ProcessFailedException {
Job inspectionJob = EasyMock.createNiceMock(Job.class);
try {
EasyMock.expect(inspectionJob.getPayload()).andReturn(MediaPackageElementParser.getAsXml(inspectedTrack));
} catch (MediaPackageException e) {
throw new MediaInspectionException(e);
}
EasyMock.replay(inspectionJob);
return inspectionJob;
}
};
/* set up video editor */
veditor.activate(cc);
veditor.setWorkspace(workspace);
veditor.setSecurityService(securityService);
veditor.setUserDirectoryService(userDirectoryService);
veditor.setSmilService(smilService);
veditor.setOrganizationDirectoryService(organizationDirectoryService);
serviceRegistry = EasyMock.createMock(ServiceRegistry.class);
final Capture<String> type = EasyMock.newCapture();
final Capture<String> operation = EasyMock.newCapture();
final Capture<List<String>> args = EasyMock.newCapture();
EasyMock.expect(serviceRegistry.createJob(capture(type), capture(operation), capture(args), EasyMock.anyFloat())).andAnswer(() -> {
Job job = new JobImpl(0);
logger.error("type: {}", type.getValue());
job.setJobType(type.getValue());
job.setOperation(operation.getValue());
job.setArguments(args.getValue());
job.setPayload(veditor.process(job));
return job;
}).anyTimes();
EasyMock.replay(serviceRegistry);
veditor.setServiceRegistry(serviceRegistry);
}
use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.
the class LdapUserProviderInstance method loadUserFromLdap.
/**
* Loads a user from LDAP.
*
* @param userName
* the username
* @return the user
*/
protected User loadUserFromLdap(String userName) {
if (delegate == null || cache == null) {
throw new IllegalStateException("The LDAP user detail service has not yet been configured");
}
ldapLoads.incrementAndGet();
UserDetails userDetails = null;
Thread currentThread = Thread.currentThread();
ClassLoader originalClassloader = currentThread.getContextClassLoader();
try {
currentThread.setContextClassLoader(LdapUserProviderFactory.class.getClassLoader());
try {
userDetails = delegate.loadUserByUsername(userName);
} catch (UsernameNotFoundException e) {
cache.put(userName, nullToken);
return null;
}
JaxbOrganization jaxbOrganization = JaxbOrganization.fromOrganization(organization);
// Get the roles and add the extra roles
Collection<GrantedAuthority> authorities = new HashSet<>();
authorities.addAll(userDetails.getAuthorities());
authorities.addAll(setExtraRoles);
Set<JaxbRole> roles = new HashSet<>();
if (authorities != null) {
/*
* Please note the prefix logic for roles:
*
* - Roles that start with any of the "exclude prefixes" are left intact
* - In any other case, the "role prefix" is prepended to the roles read from LDAP
*
* This only applies to the prefix addition. The conversion to uppercase is independent from these
* considerations
*/
for (GrantedAuthority authority : authorities) {
String strAuthority = authority.getAuthority();
boolean hasExcludePrefix = false;
for (String excludePrefix : setExcludePrefixes) {
if (strAuthority.startsWith(excludePrefix)) {
hasExcludePrefix = true;
break;
}
}
if (!hasExcludePrefix) {
strAuthority = rolePrefix + strAuthority;
}
// Finally, add the role itself
roles.add(new JaxbRole(strAuthority, jaxbOrganization));
}
}
User user = new JaxbUser(userDetails.getUsername(), PROVIDER_NAME, jaxbOrganization, roles);
cache.put(userName, user);
return user;
} finally {
currentThread.setContextClassLoader(originalClassloader);
}
}
Aggregations