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.
*
* @param query optional search query
* @param request
* @param response
* @return
* @throws Exception
*/
@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, HttpServletRequest request, HttpServletResponse response) throws Exception {
if (StringUtils.isNotBlank(query)) {
query = query.toLowerCase();
}
List<IPermissionActivity> activities = new ArrayList<IPermissionActivity>();
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.
*
* @param activityId
* @param query
* @param req
* @param response
* @return
* @throws Exception
*/
@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, HttpServletRequest req, HttpServletResponse response) throws Exception {
IPermissionActivity activity = permissionOwnerDao.getPermissionActivity(activityId);
Collection<IPermissionTarget> targets = Collections.EMPTY_LIST;
if (activity != null) {
IPermissionTargetProvider provider = targetProviderRegistry.getTargetProvider(activity.getTargetProviderKey());
SortedSet<IPermissionTarget> matchingTargets = new TreeSet<IPermissionTarget>();
// 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 ApiPermissionsService method createAssignment.
/*
* Implementation
*/
private Assignment createAssignment(IPermission permission, IAuthorizationPrincipal authP, boolean inherited) {
Assignment rslt = null;
try {
// Owner
IPermissionOwner owner = permissionOwnerDao.getPermissionOwner(permission.getOwner());
Owner ownerImpl = new OwnerImpl(permission.getOwner(), owner.getName());
// Activity
IPermissionActivity activity = permissionOwnerDao.getPermissionActivity(permission.getOwner(), permission.getActivity());
Activity activityImpl = new ActivityImpl(permission.getActivity(), activity.getName());
// Principal
Principal principalImpl = new PrincipalImpl(authP.getKey(), authP.getPrincipalString());
// Target
// default
Target targetImpl = null;
IPermissionTargetProvider targetProvider = targetProviderRegistry.getTargetProvider(activity.getTargetProviderKey());
IPermissionTarget target = targetProvider.getTarget(permission.getTarget());
if (target != null) {
targetImpl = new TargetImpl(permission.getTarget(), target.getName());
}
rslt = new AssignmentImpl(ownerImpl, activityImpl, principalImpl, targetImpl, inherited);
} catch (Exception e) {
log.warn("Exception while adding permission", e);
}
return rslt;
}
use of org.apereo.portal.permission.IPermissionActivity in project uPortal by Jasig.
the class PermissionsRESTControllerTest method testGetTargetsNullActivity.
@Test
public void testGetTargetsNullActivity() {
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(null);
Mockito.when(targetProviderRegistry.getTargetProvider(activity.getTargetProviderKey())).thenReturn(null);
ModelAndView modelAndView = permissionsRESTController.getTargets(activityId, query);
Collection<IPermissionTarget> targets = (Collection<IPermissionTarget>) modelAndView.getModel().get("targets");
Assert.assertTrue(targets.isEmpty());
}
use of org.apereo.portal.permission.IPermissionActivity in project uPortal by Jasig.
the class PermissionsRESTControllerTest method testGetActivities.
@Test
public void testGetActivities() {
String query = "activity1";
IPermissionOwner owner = Mockito.mock(IPermissionOwner.class);
owner.setFname("john");
owner.setDescription("testing description");
owner.setName("john doe");
IPermissionActivity activity = Mockito.mock(IPermissionActivity.class);
activity.setDescription("Course Activity");
activity.setFname("john");
activity.setName("activity1");
owner.getActivities().add(activity);
List<IPermissionOwner> owners = new ArrayList<>();
owners.add(owner);
Mockito.when(permissionOwnerDao.getAllPermissionOwners()).thenReturn(owners);
ModelAndView modelAndView = permissionsRESTController.getActivities(query);
List<IPermissionActivity> activities = (List<IPermissionActivity>) modelAndView.getModel().get("activities");
Assert.assertEquals(200, res.getStatus());
}
Aggregations