Search in sources :

Example 11 with IPermissionActivity

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));
}
Also used : IPermissionActivity(org.apereo.portal.permission.IPermissionActivity) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) IEntityGroup(org.apereo.portal.groups.IEntityGroup) IPermissionTargetProvider(org.apereo.portal.permission.target.IPermissionTargetProvider) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) IPermissionOwner(org.apereo.portal.permission.IPermissionOwner)

Example 12 with IPermissionActivity

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());
}
Also used : IPermissionActivity(org.apereo.portal.permission.IPermissionActivity) IPermissionTarget(org.apereo.portal.permission.target.IPermissionTarget) IPermissionTargetProvider(org.apereo.portal.permission.target.IPermissionTargetProvider) ModelAndView(org.springframework.web.servlet.ModelAndView) Collection(java.util.Collection) Test(org.junit.Test)

Example 13 with IPermissionActivity

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;
}
Also used : IPermissionActivity(org.apereo.portal.permission.IPermissionActivity) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) IPermissionOwner(org.apereo.portal.permission.IPermissionOwner) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with IPermissionActivity

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;
}
Also used : IPermissionActivity(org.apereo.portal.permission.IPermissionActivity) IPermissionTarget(org.apereo.portal.permission.target.IPermissionTarget) TreeSet(java.util.TreeSet) IPermissionTargetProvider(org.apereo.portal.permission.target.IPermissionTargetProvider) ModelAndView(org.springframework.web.servlet.ModelAndView) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with IPermissionActivity

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;
}
Also used : IPermissionActivity(org.apereo.portal.permission.IPermissionActivity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonEntityBean(org.apereo.portal.layout.dlm.remoting.JsonEntityBean) IPermission(org.apereo.portal.security.IPermission) IPermissionTarget(org.apereo.portal.permission.target.IPermissionTarget) IPermissionTargetProvider(org.apereo.portal.permission.target.IPermissionTargetProvider) HashMap(java.util.HashMap) Map(java.util.Map) IPermissionOwner(org.apereo.portal.permission.IPermissionOwner)

Aggregations

IPermissionActivity (org.apereo.portal.permission.IPermissionActivity)19 IPermissionOwner (org.apereo.portal.permission.IPermissionOwner)11 IPermissionTarget (org.apereo.portal.permission.target.IPermissionTarget)9 IPermissionTargetProvider (org.apereo.portal.permission.target.IPermissionTargetProvider)9 ModelAndView (org.springframework.web.servlet.ModelAndView)8 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 TreeSet (java.util.TreeSet)2 Element (net.sf.ehcache.Element)2 IEntityGroup (org.apereo.portal.groups.IEntityGroup)2 IGroupMember (org.apereo.portal.groups.IGroupMember)2 JsonEntityBean (org.apereo.portal.layout.dlm.remoting.JsonEntityBean)2 IPermission (org.apereo.portal.security.IPermission)2