...
- Using Spring to get the service for your class (e.g. YourAppClass) (recommended)
- Add the AuthzGroupService bean to the bean for YourAppClass
Code Block xml xml <bean id="org.sakaiproject.yourapp.logic.YourAppClass" class="org.sakaiproject.yourapp.logic.impl.YourAppClassImpl"> <property name="authzGroupService" ref="org.sakaiproject.authz.api.AuthzGroupService" /> </bean>
- Add a variable and setter to YourAppClass to use the service in like so:
Code Block java java private AuthzGroupService authzGroupService; public void setAuthzGroupService(AuthzGroupService authzGroupService) { this.authzGroupService = authzGroupService; }
- Add the AuthzGroupService bean to the bean for YourAppClass
- Using the cover Component Manager to get the service
- Note: This is not the recommended method, you should be using Spring to inject the service
- Setup a variable to hold the instance from the cover Use the CM cover to get the service
Get access to the service using the coverCode Block java java import org.sakaiproject.component.cover.ComponentManager; import org.sakaiproject.authz.api.AuthzGroupService; ... private AuthzGroupService authzGroupService;
Code Block java java ... authzGroupService = (AuthzGroupService) org.sakaiproject.authz.cover.ComponentManager.get(AuthzGroupService.getInstance(class);
Getting the users associated with a site which have a specific permission
...
Code Block | ||||
---|---|---|---|---|
| ||||
java.util.Set authzGroupIds = authzGroupService.getAuthzGroupsIsAllowed(userId, "tool.permission", null); // (1) java.util.Iterator it = authzGroupIds.iterator(); // (2) while (it.hasNext()) { String authzGroupId = (String) it.next(); Reference r = entityManager.newReference(authzGroupId); // (3) if(r.isKnownType()) { if(r.getType().equals(SiteService.SITEAPPLICATION_SUBTYPEID)) { // (4) // do something since this is a site String siteId = r.getId(); String context = r.getId(); try { Site site = siteService.getSite(siteId); } catch (IdUnusedException e) { // invalid site Id returned throw new RuntimeException("Could not get site from siteId:" + siteId); } } else if (r.getType().equals(SiteService.GROUP_SUBTYPE)) { // (5) // do something since this is a site group String groupId = r.getId(); String context = r.getId(); Group group = siteService.findGroup(groupId); if (group != null) { // found a valid group so do something } } } } |
...