[Building Sakai] Question: Getting the user from the current session

Sergio Muriel sergioame at hotmail.com
Fri Jul 18 14:29:28 PDT 2014




Dear All,I've been trying to obtain the user from the current session (if there is a user logged in), or from somewhere else using the API or a web service, but I've been having difficulties finding an example that works. 
All I need from the user is his/her type (table sakai_user, field type). 
This is what I have in my web service:
import org.sakaiproject.tool.api.Session;import org.sakaiproject.tool.api.SessionManager;import org.sakaiproject.user.api.User;import org.sakaiproject.user.api.UserDirectoryService;
	private UserDirectoryService userDirectoryService;	......		userDirectoryService = (UserDirectoryService) ComponentManager.get(UserDirectoryService.class.getName());........		Session session  = sessionManager.getCurrentSession();				// check if session is active		if (session == null) {			System.out.println("*** Current session is null.");			LOG.warn("WS RemoteReportCourses.establishSession(): Session " + sessionid + " is not active.");				throw new AxisFault("WS RemoteReportCourses.establishSession(): Session " + sessionid + " is not active");		} else {			System.out.println("*** Session: " + session);			System.out.println("*** session id: " + session.getId());			System.out.println("*** user id: " + session.getUserId());			User currentUser = null;			try {				currentUser = userDirectoryService.getUser(session.getUserId());			} catch (UserNotDefinedException unde) {				System.out.println("*** An error occurred: " + unde.getMessage());				unde.printStackTrace();			}			if (currentUser == null) {				System.out.println("*** Current user is null.");			} else {				System.out.println("*** User: " + currentUser);				System.out.println("*** User.getId(): " + currentUser.getId());				System.out.println("*** User.getEid(): " + currentUser.getEid());				System.out.println("*** User.getType(): " + currentUser.getType());			}			}
According to this page:https://confluence.sakaiproject.org/display/BOOT/Using+the+UserDirectoryServiceit should work, but this is all I get no matter if I'm logged into Sakai or not (I'm doing local development/testing):
2014-07-17 14:23:21,470  INFO http-bio-8080-exec-6 org.sakaiproject.util.RemoteHostFilter - Access granted (.+): 127.0.0.1/127.0.0.1*** Session: org.sakaiproject.tool.impl.MySession at 5414ede*** session id: 8bd4e3bb-0733-47f6-b383-6e855435fa5c*** user id: null*** An error occurred: null*** Current user is null.

There's another option to try but looks a little bit more complicated since it uses Spring framework injection and I don't really know how to configure it for a web service.
Can anybody help me with this issue? Or with any web service that can give me the information that I want?Please note that I don't want the user give me his/her login/password again in a customized form, I want to take it from the current session. My Sakai version: 2.9.1
Thank you in advance. I really appreciate any help.



Best Regards,

Sergio MurielPhone: 641-451-7186
Email: sergioame at hotmail.com

 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://collab.sakaiproject.org/pipermail/sakai-dev/attachments/20140718/f1566a7a/attachment.html 


More information about the sakai-dev mailing list