Dashboard > WG: Programmer's Cafe > ... > Sakai Framework Tips > Using the SessionManager Service
  WG: Programmer's Cafe Log In | Signup View a printable version of the current page.  
  Using the SessionManager Service
Added by Aaron Zeckoski, last edited by Aaron Zeckoski on Jul 10, 2007  (view change)
Labels: 
(None)

Information

This explains basic usage of the Sakai SessionManager Service. This service is used to register new Authz (permission) groups.

Accessing the SessionManager

  • You can use Spring Framework to inject the service or use the cover
  1. Using Spring to get the service for your class (e.g. YourAppClass) (recommended)
    1. Add the SessionManager bean to the bean for YourAppClass
      <bean id="org.sakaiproject.yourapp.logic.YourAppClass"
      		class="org.sakaiproject.yourapp.logic.impl.YourAppClassImpl">
      	<property name="sessionManager"
      		ref="org.sakaiproject.tool.api.SessionManager" />
      </bean>
    2. Add a variable and setter to YourAppClass to use the service in like so:
      private SessionManager sessionManager;
      public void setSessionManager(SessionManager sessionManager) {
      	this.sessionManager = sessionManager;
      }
  2. Using the cover 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
      private SessionManager sessionManager;
    2. Get access to the service using the cover
      sessionManager = org.sakaiproject.tool.cover.SessionManager.getInstance();
  3. Maven (project.xml) dependency
    • Add the following to the maven file for the code in which you are using the service
      <dependency>
      	<groupId>sakaiproject</groupId>
      	<artifactId>sakai-tool-api</artifactId>
      	<version>${sakai.version}</version>
      </dependency>

Getting the current user Session

  1. Use the SessionManager to get the current session
    Session s = sessionManager.getCurrentSession();
    if (s != null) {
    	// do something with the Session
    }

Changing the current user Session to another user

  • Note: This sets the current user Session to the Sakai admin
  1. Use the SessionManager to get the current session and then use the Session to set the userId
    Session s = sessionManager.getCurrentSession();
    if (s != null) {
    	s.setUserId("admin");
    } else {
    	log.warn("no CurrentSession, cannot set to admin user");
    }
    • Note: This could allow you to run something that requires the admin user permissions while there is no session with appropriate permissions (or while the session is a user with lower permissions)

Site running on a free Atlassian Confluence Open Source Project License granted to Sakai Foundation. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators