[Building Sakai] How to get a user's sites

Steve Swinsburg steve.swinsburg at gmail.com
Sat Mar 10 03:49:09 PST 2012


Hi Joshua,

The way we've handled this before is to just generate a session for the user you are after (you need to be a super user to do that), switch, get the sites, switch back, then clear the session if need be. In fact this is how a lot of external integrations work via the web services (wrapper method below to illustrate):

https://source.sakaiproject.org/svn//webservices/trunk/axis/src/webapp/SakaiScript.jws
/**
 * Return XML document listing all sites the given user has read or write access to. 
 *
 * @param 	sessionid 		the session id of a super user
 * @param   userid			eid (eg jsmith26) if the user you want the list for
 * @return
 * @throws AxisFault		if not super user or any other error occurs from main method
 */
public String getSitesUserCanAccess(String sessionid, String userid) throws AxisFault
{
	
	//get a session for the other user, reuse if possible
	String newsessionid = getSessionForUser(sessionid, userid, true);
	
	//might be an exception that was returned, so check session is valid.
	Session session = establishSession(newsessionid);
	
	//ok, so hand over to main method to get the list for us
	return getSitesUserCanAccess(newsessionid);
	
}
cheers,
Steve


On 10/03/2012, at 5:27 AM, Joshua Swink wrote:

> This is a programming question. How can you get a list of a user's sites? The catch: this is not a logged-in user.
> 
> When looking for examples of this in the current Sakai code, I come across this (in UserPrefsTool.java):
> 
> SiteService.getSites(org.sakaiproject.site.api.SiteService.SelectionType.ACCESS, null, null, null, org.sakaiproject.site.api.SiteService.SortType.TITLE_ASC, null);
> 
> The above function relies on the user being logged in. I checked the code of SiteService.getSites, and the assumption that it's searching for the current user's sites is buried deeply within it.
> 
> The best I can could up with, using the current API, is this:
> 
> 
>         public List<String> getUserSites(String userId) {
>             List<String> userSites = new ArrayList<String>();
> 
>             Map<String, String> userRoles = authzGroupService.getUserRoles(userId, null);
>             for (String realmId: userRoles.keySet()) {
>                 Reference r = entityManager.newReference(realmId);
>                 if (siteService.SITE_SUBTYPE.equals(r.getSubType())) {
>                     userSites.add(r.getId());
>                 }
>             }
>             return userSites;
>         }
> 
> Is there a better way?
> 
> Another question. Would my function work properly for people who are "provided", e.g. who are members of course sites due to being in a roster that is associated with a site?
> 
> Josh
> _______________________________________________
> sakai-dev mailing list
> sakai-dev at collab.sakaiproject.org
> http://collab.sakaiproject.org/mailman/listinfo/sakai-dev
> 
> TO UNSUBSCRIBE: send email to sakai-dev-unsubscribe at collab.sakaiproject.org with a subject of "unsubscribe"

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


More information about the sakai-dev mailing list