use of org.apereo.portal.permission.IPermissionActivity in project uPortal by Jasig.
the class PortletPermissionsCachePrimer method primeCache.
public void primeCache() {
if (executor.getActiveCount() != 0) {
log.warn("Skipping this run becasue there are active threads in the executor, signifying the previous run is not complete");
return;
}
log.info("STARTING PortletPermissionsCachePrimer.primeCache()...");
final long timestamp = System.currentTimeMillis();
/*
* This task is pretty effort-intensive and may take in excess of a
* minute to run in a single thread. Going to use a divide-and-conquer
* approach.
*/
final Map<NodeWalker, Future<NodeWalkerReport>> futures = new HashMap<>();
final IEntityGroup rootGroup = GroupService.getRootGroup(IPerson.class);
for (Map.Entry<String, Set<String>> y : permissionsMap.entrySet()) {
final IPermissionOwner owner = permissionOwnerDao.getPermissionOwner(y.getKey());
for (String s : y.getValue()) {
final IPermissionActivity activity = permissionOwnerDao.getPermissionActivity(y.getKey(), s);
final IPermissionTargetProvider targetProvider = targetProviderRegistry.getTargetProvider(activity.getTargetProviderKey());
final NodeWalker walker = new NodeWalker(rootGroup, owner, activity, targetProvider);
final Future<NodeWalkerReport> future = this.executor.submit(walker);
futures.put(walker, future);
}
}
int totalCombinations = 0;
for (Map.Entry<NodeWalker, Future<NodeWalkerReport>> y : futures.entrySet()) {
try {
final NodeWalkerReport report = y.getValue().get();
totalCombinations += report.getCombinationCount();
log.debug("NodeWalker '{}' processed {} combinations in {}ms", y.getKey(), report.getCombinationCount(), report.getDuration());
} catch (InterruptedException | ExecutionException e) {
log.error("NodeWalker '{}' failed", y.getKey());
}
}
log.info("COMPLETED PortletPermissionsCachePrimer.primeCache(); processed {} total combinations in {}ms", totalCombinations, Long.toString(System.currentTimeMillis() - timestamp));
}
use of org.apereo.portal.permission.IPermissionActivity in project uPortal by Jasig.
the class PermissionsRESTControllerTest method testGetTargetsEmpty.
@Test
public void testGetTargetsEmpty() {
Long activityId = 2L;
String query = "activity1";
IPermissionActivity activity = Mockito.mock(IPermissionActivity.class);
activity.setDescription("Course Activity");
activity.setFname("john");
activity.setName("activity1");
IPermissionTargetProvider provider = Mockito.mock(IPermissionTargetProvider.class);
activity.setTargetProviderKey("providerKey");
Mockito.when(permissionOwnerDao.getPermissionActivity(activityId)).thenReturn(activity);
Mockito.when(targetProviderRegistry.getTargetProvider(activity.getTargetProviderKey())).thenReturn(provider);
ModelAndView modelAndView = permissionsRESTController.getTargets(activityId, query);
Collection<IPermissionTarget> targets = (Collection<IPermissionTarget>) modelAndView.getModel().get("targets");
Assert.assertEquals(200, res.getStatus());
Assert.assertTrue(targets.isEmpty());
}
use of org.apereo.portal.permission.IPermissionActivity in project uPortal by Jasig.
the class PermissionsRESTController method getActivities.
/**
* Provide a list of all registered IPermissionActivities. If an optional search string is
* provided, the returned list will be restricted to activities matching the query.
*/
@PreAuthorize("hasPermission('ALL', 'java.lang.String', new org.apereo.portal.spring.security.evaluator.AuthorizableActivity('UP_PERMISSIONS', 'VIEW_PERMISSIONS'))")
@RequestMapping(value = "/permissions/activities.json", method = RequestMethod.GET)
public ModelAndView getActivities(@RequestParam(value = "q", required = false) String query) {
if (StringUtils.isNotBlank(query)) {
query = query.toLowerCase();
}
List<IPermissionActivity> activities = new ArrayList<>();
Collection<IPermissionOwner> owners = permissionOwnerDao.getAllPermissionOwners();
for (IPermissionOwner owner : owners) {
for (IPermissionActivity activity : owner.getActivities()) {
if (StringUtils.isBlank(query) || activity.getName().toLowerCase().contains(query)) {
activities.add(activity);
}
}
}
Collections.sort(activities);
ModelAndView mv = new ModelAndView();
mv.addObject("activities", activities);
mv.setViewName("json");
return mv;
}
use of org.apereo.portal.permission.IPermissionActivity in project uPortal by Jasig.
the class PermissionsRESTController method getTargets.
/**
* Return a list of targets defined for a particular IPermissionActivity matching the specified
* search query.
*/
@PreAuthorize("hasPermission('ALL', 'java.lang.String', new org.apereo.portal.spring.security.evaluator.AuthorizableActivity('UP_PERMISSIONS', 'VIEW_PERMISSIONS'))")
@RequestMapping(value = "/permissions/{activity}/targets.json", method = RequestMethod.GET)
public ModelAndView getTargets(@PathVariable("activity") Long activityId, @RequestParam("q") String query) {
IPermissionActivity activity = permissionOwnerDao.getPermissionActivity(activityId);
Collection<IPermissionTarget> targets = Collections.emptyList();
if (activity != null) {
IPermissionTargetProvider provider = targetProviderRegistry.getTargetProvider(activity.getTargetProviderKey());
SortedSet<IPermissionTarget> matchingTargets = new TreeSet<>();
// add matching results for this identifier provider to the set
targets = provider.searchTargets(query);
for (IPermissionTarget target : targets) {
if ((StringUtils.isNotBlank(target.getName()) && target.getName().toLowerCase().contains(query)) || target.getKey().toLowerCase().contains(query)) {
matchingTargets.addAll(targets);
}
}
}
ModelAndView mv = new ModelAndView();
mv.addObject("targets", targets);
mv.setViewName("json");
return mv;
}
use of org.apereo.portal.permission.IPermissionActivity in project uPortal by Jasig.
the class PermissionsListController method marshall.
/*
* Private Stuff.
*/
private List<Map<String, String>> marshall(IPermission[] data) {
// Assertions.
if (data == null) {
String msg = "Argument 'data' cannot be null";
throw new IllegalArgumentException(msg);
}
List<Map<String, String>> rslt = new ArrayList<Map<String, String>>(data.length);
for (IPermission p : data) {
JsonEntityBean bean = getEntityBean(p.getPrincipal());
Map<String, String> entry = new HashMap<String, String>();
entry.put("owner", p.getOwner());
entry.put("principalType", bean.getEntityTypeAsString());
entry.put("principalName", bean.getName());
entry.put("principalKey", p.getPrincipal());
entry.put("activity", p.getActivity());
entry.put("target", p.getTarget());
entry.put("permissionType", p.getType());
/*
* Attempt to find a name for this target through the permission
* target provider registry. If none can be found, just use
* the target key.
*/
String targetName = null;
try {
// attempt to get the target provider for this activity
IPermissionActivity activity = permissionOwnerDao.getPermissionActivity(p.getOwner(), p.getActivity());
entry.put("activityName", activity.getName());
IPermissionOwner owner = permissionOwnerDao.getPermissionOwner(p.getOwner());
entry.put("ownerName", owner.getName());
String providerKey = activity.getTargetProviderKey();
IPermissionTargetProvider provider = targetProviderRegistry.getTargetProvider(providerKey);
// get the target from the provider
IPermissionTarget target = provider.getTarget(p.getTarget());
targetName = target.getName();
} catch (RuntimeException e) {
// likely a result of a null activity or provider
log.trace("Failed to resolve target name", e);
}
if (targetName == null) {
targetName = p.getTarget();
}
entry.put("targetName", targetName);
rslt.add(entry);
}
return rslt;
}
Aggregations