Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

  1. Using Spring to get the service for your class (e.g. YourAppClass) (recommended)
    1. 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>
      
    2. 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;
      }
      
  2. 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
    1. Setup a variable to hold the instance from the cover Use the CM cover to get the service
      Code Block
      java
      java
      
      import org.sakaiproject.component.cover.ComponentManager;
      import org.sakaiproject.authz.api.AuthzGroupService;
      ...
        private AuthzGroupService authzGroupService;
      
      Get access to the service using the cover
      Code Block
      javajava
      
      ...
          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
java
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
         }
      }
   }
}

...