use of org.opencastproject.security.api.AccessControlEntry in project opencast by opencast.
the class EventHttpServletRequest method deserializeJsonToAcl.
/**
* De-serialize an JSON into an {@link AccessControlList}.
*
* @param json
* The {@link AccessControlList} to serialize.
* @param assumeAllow
* Assume that all entries are allows.
* @return An {@link AccessControlList} representation of the Json
* @throws ParseException
*/
protected static AccessControlList deserializeJsonToAcl(String json, boolean assumeAllow) throws ParseException {
JSONParser parser = new JSONParser();
JSONArray aclJson = (JSONArray) parser.parse(json);
@SuppressWarnings("unchecked") ListIterator<Object> iterator = aclJson.listIterator();
JSONObject aceJson;
List<AccessControlEntry> entries = new ArrayList<AccessControlEntry>();
while (iterator.hasNext()) {
aceJson = (JSONObject) iterator.next();
String action = aceJson.get(ACTION_JSON_KEY) != null ? aceJson.get(ACTION_JSON_KEY).toString() : "";
String allow;
if (assumeAllow) {
allow = "true";
} else {
allow = aceJson.get(ALLOW_JSON_KEY) != null ? aceJson.get(ALLOW_JSON_KEY).toString() : "";
}
String role = aceJson.get(ROLE_JSON_KEY) != null ? aceJson.get(ROLE_JSON_KEY).toString() : "";
if (StringUtils.trimToNull(action) != null && StringUtils.trimToNull(allow) != null && StringUtils.trimToNull(role) != null) {
AccessControlEntry ace = new AccessControlEntry(role, action, Boolean.parseBoolean(allow));
entries.add(ace);
} else {
throw new IllegalArgumentException(String.format("One of the access control elements is missing a property. The action was '%s', allow was '%s' and the role was '%s'", action, allow, role));
}
}
return new AccessControlList(entries);
}
use of org.opencastproject.security.api.AccessControlEntry in project opencast by opencast.
the class EventIndexUtils method addAuthorization.
/**
* Adds authorization fields to the input document.
*
* @param doc
* the input document
* @param aclString
* the access control list string
*/
private static void addAuthorization(SearchMetadataCollection doc, String aclString) {
Map<String, List<String>> permissions = new HashMap<String, List<String>>();
// Define containers for common permissions
for (Action action : Permissions.Action.values()) {
permissions.put(action.toString(), new ArrayList<String>());
}
AccessControlList acl = AccessControlParser.parseAclSilent(aclString);
for (AccessControlEntry entry : acl.getEntries()) {
if (!entry.isAllow()) {
logger.info("Event index does not support denial via ACL, ignoring {}", entry);
continue;
}
List<String> actionPermissions = permissions.get(entry.getAction());
if (actionPermissions == null) {
actionPermissions = new ArrayList<String>();
permissions.put(entry.getAction(), actionPermissions);
}
actionPermissions.add(entry.getRole());
}
// Write the permissions to the input document
for (Map.Entry<String, List<String>> entry : permissions.entrySet()) {
String fieldName = EventIndexSchema.ACL_PERMISSION_PREFIX.concat(entry.getKey());
doc.addField(fieldName, entry.getValue(), false);
}
}
use of org.opencastproject.security.api.AccessControlEntry in project opencast by opencast.
the class SeriesIndexUtils method addAuthorization.
/**
* Adds authorization fields to the input document.
*
* @param doc
* the input document
* @param aclString
* the access control list string
*/
private static void addAuthorization(SearchMetadataCollection doc, String aclString) {
Map<String, List<String>> permissions = new HashMap<String, List<String>>();
// Define containers for common permissions
for (Action action : Permissions.Action.values()) {
permissions.put(action.toString(), new ArrayList<String>());
}
AccessControlList acl = AccessControlParser.parseAclSilent(aclString);
for (AccessControlEntry entry : acl.getEntries()) {
if (!entry.isAllow()) {
logger.info("Series index does not support denial via ACL, ignoring {}", entry);
continue;
}
List<String> actionPermissions = permissions.get(entry.getAction());
if (actionPermissions == null) {
actionPermissions = new ArrayList<String>();
permissions.put(entry.getAction(), actionPermissions);
}
actionPermissions.add(entry.getRole());
}
// Write the permissions to the input document
for (Map.Entry<String, List<String>> entry : permissions.entrySet()) {
String fieldName = SeriesIndexSchema.ACL_PERMISSION_PREFIX.concat(entry.getKey());
doc.addField(fieldName, entry.getValue(), false);
}
}
use of org.opencastproject.security.api.AccessControlEntry in project opencast by opencast.
the class SeriesServicePersistenceTest method testAccessControlManagment.
@Test
public void testAccessControlManagment() throws Exception {
// sample access control list
AccessControlList accessControlList = new AccessControlList();
List<AccessControlEntry> acl = accessControlList.getEntries();
acl.add(new AccessControlEntry("admin", "delete", true));
seriesDatabase.storeSeries(testCatalog);
String seriesID = testCatalog.getFirst(DublinCore.PROPERTY_IDENTIFIER);
seriesDatabase.storeSeriesAccessControl(seriesID, accessControlList);
AccessControlList retrievedACL = seriesDatabase.getAccessControlList(seriesID);
assertNotNull(retrievedACL);
acl = retrievedACL.getEntries();
assertEquals(acl.size(), 1);
assertEquals(acl.get(0).getRole(), "admin");
try {
seriesDatabase.storeSeriesAccessControl("failid", accessControlList);
fail("Should fail when adding ACL to nonexistent series");
} catch (NotFoundException e) {
// expected
}
}
use of org.opencastproject.security.api.AccessControlEntry in project opencast by opencast.
the class SearchServiceImplTest method setUp.
@Before
public void setUp() throws Exception {
// workspace
Workspace workspace = EasyMock.createNiceMock(Workspace.class);
EasyMock.expect(workspace.get((URI) EasyMock.anyObject())).andAnswer(new IAnswer<File>() {
@Override
public File answer() throws Throwable {
return new File(new URI(EasyMock.getCurrentArguments()[0].toString()));
}
}).anyTimes();
EasyMock.replay(workspace);
// User, organization and service registry
userResponder = new Responder<User>(defaultUser);
organizationResponder = new Responder<Organization>(defaultOrganization);
SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
EasyMock.expect(securityService.getUser()).andAnswer(userResponder).anyTimes();
EasyMock.expect(securityService.getOrganization()).andAnswer(organizationResponder).anyTimes();
EasyMock.replay(securityService);
User anonymous = new JaxbUser("anonymous", "test", defaultOrganization, new JaxbRole(DefaultOrganization.DEFAULT_ORGANIZATION_ANONYMOUS, defaultOrganization));
UserDirectoryService userDirectoryService = EasyMock.createMock(UserDirectoryService.class);
EasyMock.expect(userDirectoryService.loadUser((String) EasyMock.anyObject())).andReturn(anonymous).anyTimes();
EasyMock.replay(userDirectoryService);
Organization organization = new DefaultOrganization();
OrganizationDirectoryService organizationDirectoryService = EasyMock.createMock(OrganizationDirectoryService.class);
EasyMock.expect(organizationDirectoryService.getOrganization((String) EasyMock.anyObject())).andReturn(organization).anyTimes();
EasyMock.replay(organizationDirectoryService);
// mpeg7 service
Mpeg7CatalogService mpeg7CatalogService = new Mpeg7CatalogService();
// Persistence storage
searchDatabase = new SearchServiceDatabaseImpl();
searchDatabase.setEntityManagerFactory(newTestEntityManagerFactory(SearchServiceDatabaseImpl.PERSISTENCE_UNIT));
searchDatabase.activate(null);
searchDatabase.setSecurityService(securityService);
// search service
service = new SearchServiceImpl();
serviceRegistry = new ServiceRegistryInMemoryImpl(service, securityService, userDirectoryService, organizationDirectoryService, EasyMock.createNiceMock(IncidentService.class));
StaticMetadataService mdService = newStaticMetadataService(workspace);
SeriesService seriesService = EasyMock.createNiceMock(SeriesService.class);
DublinCoreCatalog seriesCatalog = getSeriesDublinCoreCatalog("/series-dublincore.xml");
AccessControlList seriesAcl = new AccessControlList();
EasyMock.expect(seriesService.getSeries((String) EasyMock.anyObject())).andReturn(seriesCatalog).anyTimes();
EasyMock.expect(seriesService.getSeriesAccessControl((String) EasyMock.anyObject())).andReturn(seriesAcl).anyTimes();
EasyMock.replay(seriesService);
service.setStaticMetadataService(mdService);
service.setWorkspace(workspace);
service.setMpeg7CatalogService(mpeg7CatalogService);
service.setSecurityService(securityService);
service.setOrganizationDirectoryService(organizationDirectoryService);
service.setUserDirectoryService(userDirectoryService);
service.setServiceRegistry(serviceRegistry);
service.setPersistence(searchDatabase);
SolrServer solrServer = SearchServiceImpl.setupSolr(new File(solrRoot));
service.testSetup(solrServer, new SolrRequester(solrServer, securityService), new SolrIndexManager(solrServer, workspace, Arrays.asList(mdService), seriesService, mpeg7CatalogService, securityService));
// acl
String anonymousRole = securityService.getOrganization().getAnonymousRole();
acl = new AccessControlList(new AccessControlEntry(anonymousRole, Permissions.Action.READ.toString(), true));
authorizationService = EasyMock.createNiceMock(AuthorizationService.class);
EasyMock.expect(authorizationService.getActiveAcl((MediaPackage) EasyMock.anyObject())).andReturn(Tuple.tuple(acl, AclScope.Series)).anyTimes();
EasyMock.expect(authorizationService.hasPermission((MediaPackage) EasyMock.anyObject(), (String) EasyMock.anyObject())).andReturn(true).anyTimes();
service.setAuthorizationService(authorizationService);
EasyMock.replay(authorizationService);
}
Aggregations