/* * SakaiScript.jws - updated for Sakai 2.1 * */ import java.util.Date; import java.util.ArrayList; import java.util.List; import java.util.Iterator; import java.util.Set; import java.util.Collection; import org.sakaiproject.tool.api.Session; import org.sakaiproject.tool.cover.SessionManager; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.sakaiproject.authz.api.AuthzGroup; import org.sakaiproject.authz.api.Role; import org.sakaiproject.user.cover.UserDirectoryService; import org.sakaiproject.tool.api.Tool; import org.sakaiproject.site.api.ToolConfiguration; import org.sakaiproject.site.api.Site; import org.sakaiproject.site.api.SitePage; import org.sakaiproject.user.api.UserEdit; import org.sakaiproject.authz.cover.AuthzGroupService; import org.sakaiproject.user.api.User; import org.sakaiproject.tool.cover.ToolManager; import org.sakaiproject.site.cover.SiteService; import org.sakaiproject.authz.cover.SecurityService; import org.sakaiproject.site.api.SiteService.SelectionType; import org.sakaiproject.site.api.SiteService.SortType; import java.util.Properties; import org.apache.axis.AxisFault; import org.sakaiproject.util.Xml; import org.w3c.dom.Document; import org.w3c.dom.Node; public class SakaiScript { private static final Log LOG = LogFactory.getLog(SakaiScript.class); private Session establishSession(String id) throws AxisFault { Session s = SessionManager.getSession(id); if (s == null) { throw new AxisFault("Session "+id+" is not active"); } s.setActive(); SessionManager.setCurrentSession(s); return s; } public String checkSession(String id) { Session s = SessionManager.getSession(id); if (s == null) { return "null"; } else { return id; } } public String addNewUser( String sessionid, String eid, String firstname, String lastname, String email, String type, String password) throws AxisFault { Session session = establishSession(sessionid); if (!SecurityService.isSuperUser()) { LOG.warn("NonSuperUser trying to add accounts: " + session.getUserId()); throw new AxisFault("NonSuperUser trying to add accounts: " + session.getUserId()); } try { User addeduser = null; addeduser = UserDirectoryService.addUser(null, eid, firstname, lastname, email, password, type, null); } catch (Exception e) { return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String addNewUser( String sessionid, String id ,String eid, String firstname, String lastname, String email, String type, String password) throws AxisFault { Session session = establishSession(sessionid); if (!SecurityService.isSuperUser()) { LOG.warn("NonSuperUser trying to add accounts: " + session.getUserId()); throw new AxisFault("NonSuperUser trying to add accounts: " + session.getUserId()); } try { User addeduser = null; addeduser = UserDirectoryService.addUser(id, eid, firstname, lastname, email, password, type, null); } catch (Exception e) { return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String removeUser( String sessionid, String eid) throws AxisFault { Session session = establishSession(sessionid); try { UserEdit userEdit = null; String userid = UserDirectoryService.getUserByEid(eid).getId(); userEdit = UserDirectoryService.editUser(userid); UserDirectoryService.removeUser(userEdit); } catch (Exception e) { <<<<<<< .courant return e.getClass().getName() + " : " + e.getMessage(); ======= LOG.error("WS removeUser(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); >>>>>>> .fusion-droit.r56083 } return "success"; } public String changeUserInfo( String sessionid, String eid, String firstname, String lastname, String email, String type, String password) throws AxisFault { Session session = establishSession(sessionid); try { UserEdit userEdit = null; String userid = UserDirectoryService.getUserByEid(eid).getId(); userEdit = UserDirectoryService.editUser(userid); userEdit.setFirstName(firstname); userEdit.setLastName(lastname); userEdit.setEmail(email); userEdit.setType(type); userEdit.setPassword(password); UserDirectoryService.commitEdit(userEdit); } <<<<<<< .courant catch (Exception e) { return e.getClass().getName() + " : " + e.getMessage(); ======= catch (Exception e) { LOG.error("WS removeUser(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); >>>>>>> .fusion-droit.r56083 } return "success"; } public String changeUserName( String sessionid, String eid, String firstname, String lastname) throws AxisFault { Session session = establishSession(sessionid); try { UserEdit userEdit = null; String userid = UserDirectoryService.getUserByEid(eid).getId(); userEdit = UserDirectoryService.editUser(userid); userEdit.setFirstName(firstname); userEdit.setLastName(lastname); UserDirectoryService.commitEdit(userEdit); } catch (Exception e) { LOG.error("WS changeUserName(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String changeUserEmail( String sessionid, String eid, String email) throws AxisFault { Session session = establishSession(sessionid); try { UserEdit userEdit = null; String userid = UserDirectoryService.getUserByEid(eid).getId(); userEdit = UserDirectoryService.editUser(userid); userEdit.setEmail(email); UserDirectoryService.commitEdit(userEdit); } catch (Exception e) { LOG.error("WS changeUserEmail(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String changeUserType( String sessionid, String eid, String type) throws AxisFault { Session session = establishSession(sessionid); try { UserEdit userEdit = null; String userid = UserDirectoryService.getUserByEid(eid).getId(); userEdit = UserDirectoryService.editUser(userid); userEdit.setType(type); UserDirectoryService.commitEdit(userEdit); } catch (Exception e) { LOG.error("WS changeUserType(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String changeUserPassword( String sessionid, String eid, String password) throws AxisFault { Session session = establishSession(sessionid); try { UserEdit userEdit = null; String userid = UserDirectoryService.getUserByEid(eid).getId(); userEdit = UserDirectoryService.editUser(userid); userEdit.setPassword(password); UserDirectoryService.commitEdit(userEdit); } catch (Exception e) { LOG.error("WS changeUserPassword(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String getUserEmail( String sessionid ) throws AxisFault { Session session = establishSession(sessionid); User user = UserDirectoryService.getCurrentUser(); return user.getEmail(); } <<<<<<< .courant ======= /** * Gets the email address for a given user * * Differs from original above as that one uses the session to get the displayname hence you must know this in advance or be logged in to the web services * with that user. This uses a userid as well so we could be logged in as admin and retrieve the email address for any user. * * @param sessionid the id of a valid session * @param userid the login username (ie jsmith26) of the user you want the email address for * @return the display name for the user * @throws AxisFault * */ public String getUserEmail( String sessionid, String userid ) throws AxisFault { Session session = establishSession(sessionid); try { User user = UserDirectoryService.getUserByEid(userid); return user.getEmail(); } catch (Exception e) { LOG.error("WS getUserEmail() failed for user: " + userid + " : " + e.getClass().getName() + " : " + e.getMessage()); return ""; } } /** * Get a user's display name based on their session id * * @param sessionid the session id of the user who's display name you wish to retrieve * @return success or exception message * @throws AxisFault * */ >>>>>>> .fusion-droit.r56083 public String getUserDisplayName( String sessionid ) throws AxisFault { Session session = establishSession(sessionid); User user = UserDirectoryService.getCurrentUser(); return user.getDisplayName(); } //addNewRealm public String addNewAuthzGroup(String sessionid, String groupid) throws AxisFault { Session session = establishSession(sessionid); <<<<<<< .courant ======= try { User user = UserDirectoryService.getUserByEid(userid); return user.getDisplayName(); } catch (Exception e) { LOG.error("WS getUserDisplayName() failed for user: " + userid + " : " + e.getClass().getName() + " : " + e.getMessage()); return ""; } } /** * Create user-group to specified worksite (as if it had been added in Worksite Setup) * * @param sessionid the id of a valid session * @param siteid the id of the site you want the group created in * @param grouptitle the name of the new group * @param groupdesc the description of the new group * @return groupid if successful/exception * */ private static final String GROUP_PROP_WSETUP_CREATED = "group_prop_wsetup_created"; public String addGroupToSite( String sessionid, String siteid, String grouptitle, String groupdesc ) throws AxisFault { Session session = establishSession(sessionid); try { Site site = SiteService.getSite(siteid); Group group = site.addGroup(); group.setTitle(grouptitle); group.setDescription(groupdesc); group.getProperties().addProperty(GROUP_PROP_WSETUP_CREATED, Boolean.TRUE.toString()); SiteService.save(site); return group.getId(); } catch (Exception e) { LOG.error("WS addGroupToSite(): " + e.getClass().getName() + " : " + e.getMessage()); return ""; } } /** * Add member to specified worksite group * * @param sessionid the id of a valid session * @param siteid the id of the site that the group is in * @param groupid the id of the group you want to add the user to * @param userid the internal userid of the member to add * @return true if successful/exception * * TODO: This is not returning false if it fails (ie if user isn't in site to begin with). SAK-15334 */ public boolean addMemberToGroup( String sessionid, String siteid, String groupid, String userid ) throws AxisFault { Session session = establishSession(sessionid); try { Site site = SiteService.getSite(siteid); Group group = site.getGroup(groupid); if ( group == null ) return false; >>>>>>> .fusion-droit.r56083 try { AuthzGroup authzgroup = null; authzgroup = AuthzGroupService.addAuthzGroup(groupid); AuthzGroupService.save(authzgroup); } catch (Exception e) { LOG.error("WS addNewAuthzGroup(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } //removeRealm public String removeAuthzGroup( String sessionid, String authzgroupid) throws AxisFault { Session session = establishSession(sessionid); try { AuthzGroup authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); AuthzGroupService.removeAuthzGroup(authzgroup); } catch (Exception e) { LOG.error("WS removeAuthzGroup(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } //addNewRoleToRealm public String addNewRoleToAuthzGroup( String sessionid, String authzgroupid, String roleid, String description) throws AxisFault { Session session = establishSession(sessionid); try { AuthzGroup authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); Role role = authzgroup.addRole(roleid); role.setDescription(description); AuthzGroupService.save(authzgroup); } catch (Exception e) { LOG.error("WS addNewRoleToAuthzGroup(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } //removeAllRolesFromRealm public String removeAllRolesFromAuthzGroup( String sessionid, String authzgroupid) throws AxisFault { Session session = establishSession(sessionid); try { AuthzGroup authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); authzgroup.removeRoles(); AuthzGroupService.save(authzgroup); } catch (Exception e) { LOG.error("WS removeAllRolesFromAuthzGroup(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } //removeRolefromRealm public String removeRoleFromAuthzGroup( String sessionid, String authzgroupid, String roleid) throws AxisFault { Session session = establishSession(sessionid); try { AuthzGroup authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); authzgroup.removeRole(roleid); AuthzGroupService.save(authzgroup); } catch (Exception e) { LOG.error("WS removeRoleFromAuthzGroup(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } //addLockToRole public String allowFunctionForRole( String sessionid, String authzgroupid, String roleid, String functionname) throws AxisFault { Session session = establishSession(sessionid); try { AuthzGroup authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); Role role = authzgroup.getRole(roleid); role.allowFunction(functionname); AuthzGroupService.save(authzgroup); } catch (Exception e) { LOG.error("WS allowFunctionForRole(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } //removeAllLocksFromRole public String disallowAllFunctionsForRole( String sessionid, String authzgroupid, String roleid) throws AxisFault { Session session = establishSession(sessionid); try { AuthzGroup authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); Role role = authzgroup.getRole(roleid); role.disallowAll(); AuthzGroupService.save(authzgroup); } catch (Exception e) { LOG.error("WS disallowAllFunctionsForRole(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } //removeLockFromRole public String disallowFunctionForRole( String sessionid, String authzgroupid, String roleid, String functionname) throws AxisFault { Session session = establishSession(sessionid); try { AuthzGroup authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); Role role = authzgroup.getRole(roleid); role.disallowFunction(functionname); AuthzGroupService.save(authzgroup); } catch (Exception e) { LOG.error("WS disallowFunctionForRole(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String setRoleDescription( String sessionid, String authzgroupid, String roleid, String description) throws AxisFault { Session session = establishSession(sessionid); try { AuthzGroup authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); Role role = authzgroup.getRole(roleid); role.setDescription(description); AuthzGroupService.save(authzgroup); } catch (Exception e) { LOG.error("WS setRoleDescription(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } //addUserToRealmWithRole public String addMemberToAuthzGroupWithRole( String sessionid, String eid, String authzgroupid, String roleid) throws AxisFault { Session session = establishSession(sessionid); try { AuthzGroup authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); String userid = UserDirectoryService.getUserByEid(eid).getId(); authzgroup.addMember(userid,roleid,true,false); AuthzGroupService.save(authzgroup); } catch (Exception e) { LOG.error("WS addMemberToAuthzGroupWithRole(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } //removeUserFromRealm public String removeMemberFromAuthzGroup( String sessionid, String eid, String authzgroupid) throws AxisFault { Session session = establishSession(sessionid); try { AuthzGroup authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); String userid = UserDirectoryService.getUserByEid(eid).getId(); authzgroup.removeMember(userid); AuthzGroupService.save(authzgroup); } catch (Exception e) { LOG.error("WS removeMemberFromAuthzGroup(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } //removeAllUsersFromRealm public String removeAllMembersFromAuthzGroup( String sessionid, String authzgroupid) throws AxisFault { Session session = establishSession(sessionid); try { AuthzGroup realmEdit = AuthzGroupService.getAuthzGroup(authzgroupid); realmEdit.removeMembers(); AuthzGroupService.save(realmEdit); } catch (Exception e) { LOG.error("WS removeAllMembersFromAuthzGroup(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } //setRoleForRealmMaintenance public String setRoleForAuthzGroupMaintenance( String sessionid, String authzgroupid, String roleid) throws AxisFault { Session session = establishSession(sessionid); try { AuthzGroup authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); authzgroup.setMaintainRole(roleid); AuthzGroupService.save(authzgroup); } catch (Exception e) { LOG.error("WS setRoleForAuthzGroupMaintenance(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String addMemberToSiteWithRole(String sessionid, String siteid, String eid, String roleid) throws AxisFault { Session session = establishSession(sessionid); try { Site site = SiteService.getSite(siteid); String userid = UserDirectoryService.getUserByEid(eid).getId(); site.addMember(userid,roleid,true,false); SiteService.save(site); } catch (Exception e) { LOG.error("WS addMemberToSiteWithRole(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String addNewSite( String sessionid, String siteid, String title, String description, String shortdesc, String iconurl, String infourl, boolean joinable, String joinerrole, boolean published, boolean publicview, String skin, String type) throws AxisFault { Session session = establishSession(sessionid); try { Site siteEdit = null; siteEdit = SiteService.addSite(siteid, type); siteEdit.setTitle(title); siteEdit.setDescription(description); siteEdit.setShortDescription(shortdesc); siteEdit.setIconUrl(iconurl); siteEdit.setInfoUrl(infourl); siteEdit.setJoinable(joinable); siteEdit.setJoinerRole(joinerrole); siteEdit.setPublished(published); siteEdit.setPubView(publicview); siteEdit.setSkin(skin); siteEdit.setType(type); SiteService.save(siteEdit); } catch (Exception e) { LOG.error("WS addNewSite(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String removeSite( String sessionid, String siteid) throws AxisFault { Session session = establishSession(sessionid); try { Site siteEdit = null; siteEdit = SiteService.getSite(siteid); SiteService.removeSite(siteEdit); } catch (Exception e) { LOG.error("WS removeSite(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String copySite( String sessionid, String siteidtocopy, String newsiteid, String title, String description, String shortdesc, String iconurl, String infourl, boolean joinable, String joinerrole, boolean published, boolean publicview, String skin, String type) throws AxisFault { Session session = establishSession(sessionid); try { Site site = SiteService.getSite(siteidtocopy); Site siteEdit = SiteService.addSite(newsiteid, site); siteEdit.setTitle(title); siteEdit.setDescription(description); siteEdit.setShortDescription(shortdesc); siteEdit.setIconUrl(iconurl); siteEdit.setInfoUrl(infourl); siteEdit.setJoinable(joinable); siteEdit.setJoinerRole(joinerrole); siteEdit.setPublished(published); siteEdit.setPubView(publicview); siteEdit.setSkin(skin); siteEdit.setType(type); SiteService.save(siteEdit); } catch (Exception e) { LOG.error("WS copySite(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String addNewPageToSite( String sessionid, String siteid, String pagetitle, int pagelayout) throws AxisFault { Session session = establishSession(sessionid); try { Site siteEdit = null; SitePage sitePageEdit = null; siteEdit = SiteService.getSite(siteid); sitePageEdit = siteEdit.addPage(); sitePageEdit.setTitle(pagetitle); sitePageEdit.setLayout(pagelayout); SiteService.save(siteEdit); } catch (Exception e) { LOG.error("WS addNewPageToSite(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String removePageFromSite( String sessionid, String siteid, String pagetitle) throws AxisFault { Session session = establishSession(sessionid); try { Site siteEdit = null; siteEdit = SiteService.getSite(siteid); List pageEdits = siteEdit.getPages(); for (Iterator i = pageEdits.iterator(); i.hasNext();) { SitePage pageEdit = (SitePage) i.next(); if (pageEdit.getTitle().equals(pagetitle)) siteEdit.removePage(pageEdit); } SiteService.save(siteEdit); } catch (Exception e) { LOG.error("WS removePageFromSite(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String addNewToolToPage( String sessionid, String siteid, String pagetitle, String tooltitle, String toolid, String layouthints) throws AxisFault { Session session = establishSession(sessionid); try { Site siteEdit = SiteService.getSite(siteid); List pageEdits = siteEdit.getPages(); for (Iterator i = pageEdits.iterator(); i.hasNext();) { SitePage pageEdit = (SitePage) i.next(); if (pageEdit.getTitle().equals(pagetitle)) { ToolConfiguration tool = pageEdit.addTool(); Tool t = tool.getTool(); tool.setTool(toolid, ToolManager.getTool(toolid)); tool.setTitle(tooltitle); //toolEdit.setTitle(tooltitle); //toolEdit.setToolId(toolid); //toolEdit.setLayoutHints(layouthints); } } SiteService.save(siteEdit); } catch (Exception e) { LOG.error("WS addNewToolToPage(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } public String addConfigPropertyToTool( String sessionid, String siteid, String pagetitle, String tooltitle, String propname, String propvalue) throws AxisFault { Session session = establishSession(sessionid); try { Site siteEdit = SiteService.getSite(siteid); List pageEdits = siteEdit.getPages(); for (Iterator i = pageEdits.iterator(); i.hasNext();) { SitePage pageEdit = (SitePage) i.next(); if (pageEdit.getTitle().equals(pagetitle)) { List toolEdits = pageEdit.getTools(); for (Iterator j = toolEdits.iterator(); j.hasNext();) { ToolConfiguration tool = (ToolConfiguration) j.next(); Tool t = tool.getTool(); if (tool.getTitle().equals(tooltitle)) { Properties propsedit = tool.getPlacementConfig(); propsedit.setProperty(propname, propvalue); } } } } SiteService.save(siteEdit); } catch (Exception e) { LOG.error("WS addConfigPropertyToTool(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } //checkForUser(): a call to check for an existing user public boolean checkForUser(String sessionid, String eid) throws AxisFault { Session s = establishSession(sessionid); try { User u = null; String userid = UserDirectoryService.getUserByEid(eid).getId(); u = UserDirectoryService.getUser(userid); if (u != null) { return true; } else { return false; } } catch (Exception e) { LOG.error("WS checkForUser(): " + e.getClass().getName() + " : " + e.getMessage()); return false; } } // checkForSite(): a call to check for an existing site public boolean checkForSite(String sessionid, String siteid) throws AxisFault { Session s = establishSession(sessionid); try { Site site = null; site = SiteService.getSite(siteid); if (site != null) { return true; } else { return false; } } catch (Exception e) { LOG.error("WS checkForSite(): " + e.getClass().getName() + " : " + e.getMessage()); return false; } } //checkForUserInRealmWithRole public boolean checkForMemberInAuthzGroupWithRole(String sessionid, String eid, String authzgroupid, String role) throws AxisFault { Session s = establishSession(sessionid); try { AuthzGroup authzgroup = null; authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); if (authzgroup == null) { return false; } else { String userid = UserDirectoryService.getUserByEid(eid).getId(); return authzgroup.hasRole(userid, role); } } catch (Exception e) { LOG.error("WS checkForMemberInAuthzGroupWithRole(): " + e.getClass().getName() + " : " + e.getMessage()); return false; } } // Return XML document listing all sites user has read or write access public String getSitesUserCanAccess(String sessionid) throws AxisFault { Session s = establishSession(sessionid); try { List allSites = SiteService.getSites(SelectionType.ACCESS, null, null, null, SortType.TITLE_ASC, null); List moreSites = SiteService.getSites(SelectionType.UPDATE, null, null, null, SortType.TITLE_ASC, null); if ((allSites == null || moreSites == null) || (allSites.size() == 0 && moreSites.size() == 0)) { return ""; } // Remove duplicates and combine two lists allSites.removeAll( moreSites ); allSites.addAll( moreSites ); Document dom = Xml.createDocument(); Node list = dom.createElement("list"); dom.appendChild(list); for (Iterator i = allSites.iterator(); i.hasNext();) { Site site = (Site)i.next(); Node item = dom.createElement("item"); Node siteId = dom.createElement("siteId"); siteId.appendChild( dom.createTextNode(site.getId()) ); Node siteTitle = dom.createElement("siteTitle"); siteTitle.appendChild( dom.createTextNode(site.getTitle()) ); item.appendChild(siteId); item.appendChild(siteTitle); list.appendChild(item); } return Xml.writeDocumentToString(dom); } catch (Exception e) { LOG.error("WS getSitesUserCanAccess(): " + e.getClass().getName() + " : " + e.getMessage()); return ""; } } } <<<<<<< .courant ======= /** * Get a site's description * * @param sessionid the id of a valid session * @param siteid the id of the site you want the description of * @return description of the site or string containing error * @throws AxisFault * */ public String getSiteDescription(String sessionid, String siteid) throws AxisFault { Session s = establishSession(sessionid); String siteDescription = ""; try { Site site = SiteService.getSite(siteid); siteDescription = site.getDescription(); } catch (Exception e) { LOG.error("WS getSiteDescription(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return siteDescription; } /** * Get a site's skin * * @param sessionid the id of a valid session * @param siteid the id of the site you want the skin of * @return description of the site or string containing error * @throws AxisFault * */ public String getSiteSkin(String sessionid, String siteid) throws AxisFault { Session s = establishSession(sessionid); String siteSkin = ""; try { Site site = SiteService.getSite(siteid); siteSkin = site.getSkin(); } catch (Exception e) { LOG.error("WS getSiteSkin(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return siteSkin; } /** * Get a site's joinable status * * @param sessionid the id of a valid session * @param siteid the id of the site you want the joinable status of * @return true if joinable, false if not or error (and logs any errors) * @throws AxisFault * */ public boolean isSiteJoinable(String sessionid, String siteid) throws AxisFault { Session s = establishSession(sessionid); try { Site site = SiteService.getSite(siteid); if(site.isJoinable()) { return true; } else { return false; } } catch (Exception e) { LOG.error("WS isSiteJoinable(): " + e.getClass().getName() + " : " + e.getMessage()); return false; } } /** * Change the title of a site * * @param sessionid the id of a valid session * @param siteid the id of the site you want to change the title of * @param title the new title * @return success or string containing error * @throws AxisFault * */ public String changeSiteTitle(String sessionid, String siteid, String title) throws AxisFault { Session session = establishSession(sessionid); try { Site siteEdit = null; siteEdit = SiteService.getSite(siteid); siteEdit.setTitle(title); SiteService.save(siteEdit); } catch (Exception e) { LOG.error("WS changeSiteTitle(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } /** * Change the skin of a site * * @param sessionid the id of a valid session * @param siteid the id of the site you want to change the skin of * @param title the new skin value (make sure its in /library/skin/) * @return success or string containing error * @throws AxisFault * */ public String changeSiteSkin(String sessionid, String siteid, String skin) throws AxisFault { Session session = establishSession(sessionid); try { Site siteEdit = null; siteEdit = SiteService.getSite(siteid); siteEdit.setSkin(skin); SiteService.save(siteEdit); } catch (Exception e) { LOG.error("WS changeSiteSkin(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } /** * Make a site joinable or not, depending on the params sent * * @param sessionid the id of a valid session * @param siteid the id of the site you want to change the status of * @param joinable boolean if its joinable or not * @param joinerrole the role that users who join the site will be given * @param publicview boolean if the site is to be public or not. if its joinable it should probably be public so people can find it, but if its public it doesnt necessarily need to be joinable. * @return success or string containing error * @throws AxisFault * */ public String changeSiteJoinable(String sessionid, String siteid, boolean joinable, String joinerrole, boolean publicview) throws AxisFault { Session session = establishSession(sessionid); try { Site siteEdit = null; siteEdit = SiteService.getSite(siteid); siteEdit.setJoinable(joinable); siteEdit.setJoinerRole(joinerrole); siteEdit.setPubView(publicview); SiteService.save(siteEdit); } catch (Exception e) { LOG.error("WS changeSiteJoinable(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } /** * Change the icon of a site (top left hand corner of site) * * @param sessionid the id of a valid session * @param siteid the id of the site you want to change the icon of * @param title the new icon value (publically accessible url - suggest its located in Resources for the site or another public location) * @return success or string containing error * @throws AxisFault * */ public String changeSiteIconUrl(String sessionid, String siteid, String iconurl) throws AxisFault { Session session = establishSession(sessionid); try { Site siteEdit = null; siteEdit = SiteService.getSite(siteid); siteEdit.setIconUrl(iconurl); SiteService.save(siteEdit); } catch (Exception e) { LOG.error("WS changeSiteIconUrl(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } /** * Change the description of a site * * @param sessionid the id of a valid session * @param siteid the id of the site you want to change the title of * @param description the new description * @return success or string containing error * @throws AxisFault * */ public String changeSiteDescription( String sessionid, String siteid, String description) throws AxisFault { Session session = establishSession(sessionid); try { Site siteEdit = null; siteEdit = SiteService.getSite(siteid); siteEdit.setDescription(description); SiteService.save(siteEdit); } catch (Exception e) { LOG.error("WS changeSiteDescription(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } /** * Get a custom property of a site * * @param sessionid the id of a valid session * @param siteid the id of the site you want to get the property from * @param propname the name of the property you want * @return the property or blank if not found/property is blank * @throws AxisFault * */ public String getSiteProperty(String sessionid, String siteid, String propname) throws AxisFault { Session session = establishSession(sessionid); try { //get site handle Site site = SiteService.getSite(siteid); //get list of properties for this site ResourceProperties props = site.getProperties(); //get the property that we wanted, as a string. this wont return multi valued ones //would need to use getPropertyList() for that, but then need to return XML since its a list. String propvalue = props.getProperty(propname); return propvalue; } catch (Exception e) { LOG.error("WS getSiteProperty(): " + e.getClass().getName() + " : " + e.getMessage()); return ""; } } /** * Set a custom property for a site * * @param sessionid the id of a valid session * @param siteid the id of the site you want to set the property for * @param propname the name of the property you want to set * @param propvalue the name of the property you want to set * @return success if true or exception * @throws AxisFault * */ public String setSiteProperty(String sessionid, String siteid, String propname, String propvalue) throws AxisFault { Session session = establishSession(sessionid); try { //get site handle Site site = SiteService.getSite(siteid); //get properties in edit mode ResourcePropertiesEdit props = site.getPropertiesEdit(); //add property props.addProperty(propname, propvalue); //save site SiteService.save(site); } catch (Exception e) { LOG.error("WS setSiteProperty(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } /** * Remove a custom property for a site * * @param sessionid the id of a valid session * @param siteid the id of the site you want to remove the property from * @param propname the name of the property you want to remove * @return success if true or exception * @throws AxisFault * */ public String removeSiteProperty( String sessionid, String siteid, String propname) throws AxisFault { Session session = establishSession(sessionid); try { //get site handle Site site = SiteService.getSite(siteid); //get properties in edit mode ResourcePropertiesEdit props = site.getPropertiesEdit(); //remove property //if the property doesn't exist it will still return success. this is fine. props.removeProperty(propname); //save site SiteService.save(site); } catch (Exception e) { LOG.error("WS removeSiteProperty(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } /** * Check if a role exists in a given authzgroup * * @param sessionid the id of a valid session * @param authzgroupid the id of the authzgroup you want to check * @param roleid the id of the role you want to check for * @return true/false * @throws AxisFault * */ public boolean checkForRoleInAuthzGroup(String sessionid, String authzgroupid, String roleid) throws AxisFault { Session session = establishSession(sessionid); try { //open authzgroup AuthzGroup authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); //see if we can get the role in this authzgroup. will either return the Role, or null Role role = authzgroup.getRole(roleid); if(role != null) { return true; } return false; } catch (Exception e) { LOG.error("WS checkForRoleInAuthzGroup(): " + e.getClass().getName() + " : " + e.getMessage()); return false; } } /** * Search all the users that match this criteria in id or email, first or last name, returning * a subset of records within the record range given (sorted by sort name). * As of September 2008, the Sakai API that does the searching is not searching provided users * nor limiting the returned results (ie first and last are ignored) * * This web service is returning everything it receives correctly though, so when * UserDirectoryService.searchUsers() is amended, this will be even more complete. * * See: SAK-6792 and SAK-14268 for the releavnt Jira's tickets. * * @param sessionid the id of a valid session * @param criteria the search criteria. * @param first the first record position to return. * @param last the last record position to return. * @return xml doc of list of records * @throws AxisFault * */ public String searchForUsers(String sessionid, String criteria, int first, int last) throws AxisFault { Session session = establishSession(sessionid); try { //validate input if(("").equals(criteria)) { LOG.warn("WS searchForUsers(): no search criteria"); return ""; } if(first == 0 || last == 0) { LOG.warn("WS searchForUsers(): invalid ranges"); return ""; } List users = UserDirectoryService.searchUsers(criteria, first, last); Document dom = Xml.createDocument(); Node list = dom.createElement("list"); dom.appendChild(list); for (Iterator i = users.iterator(); i.hasNext();) { User user = (User) i.next(); try { Node userNode = dom.createElement("user"); Node userId = dom.createElement("id"); userId.appendChild(dom.createTextNode(user.getId())); Node userEid = dom.createElement("eid"); userEid.appendChild(dom.createTextNode(user.getEid())); Node userName = dom.createElement("name"); userName.appendChild(dom.createTextNode(user.getDisplayName())); Node userEmail = dom.createElement("email"); userEmail.appendChild(dom.createTextNode(user.getEmail())); userNode.appendChild(userId); userNode.appendChild(userEid); userNode.appendChild(userName); userNode.appendChild(userEmail); list.appendChild(userNode); } catch (Exception e) { //log this error and continue to the next user, otherwise we get nothing LOG.warn("WS searchForUsers(): " + e.getClass().getName() + " : " + e.getMessage()); } } //add total size node (nice attribute to save the end user doing an XSLT count every time) Node total = dom.createElement("total"); total.appendChild(dom.createTextNode(Integer.toString(users.size()))); list.appendChild(total); return Xml.writeDocumentToString(dom); } catch (Exception e) { LOG.error("WS searchForUsers(): " + e.getClass().getName() + " : " + e.getMessage()); return ""; } } /** * Check if an authzgroup exists, similar to checkForSite, but does authzgroup instead * (e.g. might be used to check if !site.template exists which checkForSite() cannot do.) * * @param sessionid the id of a valid session * @param authzgroupid the id of the authzgroup you want to check * @return true if exists, false if not or error. * @throws AxisFault * */ public boolean checkForAuthzGroup(String sessionid, String authzgroupid) throws AxisFault { Session s = establishSession(sessionid); try { AuthzGroup authzgroup = null; authzgroup = AuthzGroupService.getAuthzGroup(authzgroupid); if (authzgroup != null) { return true; } else { return false; } } catch (Exception e) { LOG.error("WS checkForAuthzGroup(): " + e.getClass().getName() + " : " + e.getMessage()); return false; } } /** * Removes a member from a given site, similar to removeMembeForAuthzGroup but acts on Site directly * * @param sessionid the id of a valid session * @param siteid the id of the site you want to remove the user from * @return success or string containing error * @throws AxisFault * */ public String removeMemberFromSite(String sessionid, String siteid, String eid) throws AxisFault { Session session = establishSession(sessionid); try { Site site = SiteService.getSite(siteid); String userid = UserDirectoryService.getUserByEid(eid).getId(); site.removeMember(userid); SiteService.save(site); } catch (Exception e) { LOG.error("WS removeMemberFromSite(): " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } /** * Check if a user is in a particular authzgroup * * @param sessionid the id of a valid session, generally the admin user * @param authzgroupid the id of the authzgroup or site you want to check (if site: /site/SITEID) * @param eid the userid of the person you want to check * @return true if in site, false if not or error. * @throws AxisFault * */ public boolean checkForUserInAuthzGroup(String sessionid, String authzgroupid, String eid) throws AxisFault { Session s = establishSession(sessionid); try { AuthzGroup azg = AuthzGroupService.getAuthzGroup(authzgroupid); for (Iterator i = azg.getUsers().iterator(); i.hasNext(); ) { String id = (String) i.next(); User user = UserDirectoryService.getUser(id); if(user.getEid().equals(eid)) { return true; } } return false; } catch (Exception e) { LOG.error("WS checkForUserInAuthzGroup(): " + e.getClass().getName() + " : " + e.getMessage()); return false; } } /** * Get list of users in an authzgroup with the given role(s) * * @param sessionid the id of a valid session * @param authzgroupid the id of the authzgroup or site you want to get the users in (if site: /site/SITEID) * @param authzgrouproles the roles that you want to filter on (string with spaces as delimiters) * @return xml doc of the list of users, display name and roleid * @throws AxisFault returns string if exception encountered and logs it * */ public String getUsersInAuthzGroupWithRole(String sessionid, String authzgroupid, String authzgrouproles) throws AxisFault { Session s = establishSession(sessionid); try { AuthzGroup azg = AuthzGroupService.getAuthzGroup(authzgroupid); Document dom = Xml.createDocument(); Node list = dom.createElement("list"); dom.appendChild(list); //split the authzgroup roles into a string[] then add into arraylist. //Done this way so we dont need any extra imports (ie using List authzgrouprolesList = Arrays.asList(authzgrouprolesArr)); String[] authzgrouprolesArr = authzgrouproles.split(" "); ArrayList authzgrouprolesList = new ArrayList(); for(int i=0; i< authzgrouprolesArr.length; i++) { authzgrouprolesList.add(authzgrouprolesArr[i]); } //iterate over each role in the list... for (Iterator j = authzgrouprolesList.iterator(); j.hasNext(); ) { String role = (String) j.next(); //now get all the users in the authzgroup with this role and add to xml doc for (Iterator k = azg.getUsersHasRole(role).iterator(); k.hasNext(); ) { String id = (String) k.next(); try { User user = UserDirectoryService.getUser(id); Node userNode = dom.createElement("user"); Node userId = dom.createElement("id"); userId.appendChild(dom.createTextNode(user.getEid())); Node userName = dom.createElement("name"); userName.appendChild(dom.createTextNode(user.getDisplayName())); Node userRole = dom.createElement("role"); userRole.appendChild(dom.createTextNode(role)); userNode.appendChild(userId); userNode.appendChild(userName); userNode.appendChild(userRole); list.appendChild(userNode); } catch (Exception e) { //Exception with this user, log the error, skip this user and continue to the next LOG.warn("WS getUsersInAuthzGroupWithRole(): error processing user " + id + " : " + e.getClass().getName() + " : " + e.getMessage()); } } } return Xml.writeDocumentToString(dom); } catch (Exception e) { LOG.error("WS getUsersInAuthzGroupWithRole(): " + e.getClass().getName() + " : " + e.getMessage()); return ""; } } /** * Gets list of ALL users in an authzgroup * * * @param sessionid the id of a valid session * @param authzgroupid the id of the authzgroup or site you want to get the users in (if site: /site/SITEID) * @return xml doc of the list of users, display name and roleid * @throws AxisFault returns string if exception encountered * * */ public String getUsersInAuthzGroup(String sessionid, String authzgroupid) throws AxisFault { Session s = establishSession(sessionid); try { AuthzGroup azg = AuthzGroupService.getAuthzGroup(authzgroupid); Document dom = Xml.createDocument(); Node list = dom.createElement("list"); dom.appendChild(list); for (Iterator i = azg.getUsers().iterator(); i.hasNext(); ) { String id = (String) i.next(); try { User user = UserDirectoryService.getUser(id); //wrapping user node Node userNode = dom.createElement("user"); //id child node Node userId = dom.createElement("id"); userId.appendChild(dom.createTextNode(user.getEid())); //name child node Node userName = dom.createElement("name"); userName.appendChild(dom.createTextNode(user.getDisplayName())); //role child node Node userRole = dom.createElement("role"); String role = azg.getUserRole(id).getId(); userRole.appendChild(dom.createTextNode(role)); //add all clicd nodes into the parent node userNode.appendChild(userId); userNode.appendChild(userName); userNode.appendChild(userRole); list.appendChild(userNode); } catch (Exception e) { //Exception with this user, log the error, skip this user and continue to the next LOG.warn("WS getUsersInAuthzGroup(): error processing user " + id + " : " + e.getClass().getName() + " : " + e.getMessage()); } } return Xml.writeDocumentToString(dom); } catch (Exception e) { LOG.error("WS getUsersInAuthzGroup(): " + e.getClass().getName() + " : " + e.getMessage()); return ""; } } /** * Copy the calendar events from one site to another * * @param sessionid the id of a valid session * @param sourceSiteId the id of the site containing the calendar entries you want copied * @param targetSiteId the roles that you want to filter on (string with spaces as delimiters) * @return success or exception * @throws AxisFault * */ public String copyCalendarEvents(String sessionid, String sourceSiteId, String targetSiteId) throws AxisFault { Session session = establishSession(sessionid); //setup source and target calendar strings String calId1 = "/calendar/calendar/"+sourceSiteId+"/main"; String calId2 = "/calendar/calendar/"+targetSiteId+"/main"; try { //get calendars Calendar calendar1 = CalendarService.getCalendar(calId1); CalendarEdit calendar2 = CalendarService.editCalendar(calId2); //for every event in calendar1, add it to calendar2 List eventsList = calendar1.getEvents(null, null); for (Iterator i = eventsList.iterator(); i.hasNext();) { CalendarEvent cEvent = (CalendarEvent) i.next(); CalendarEventEdit cedit = calendar2.addEvent(); cedit.setRange(cEvent.getRange()); cedit.setDisplayName(cEvent.getDisplayName()); cedit.setDescription(cEvent.getDescription()); cedit.setType(cEvent.getType()); cedit.setLocation(cEvent.getLocation()); cedit.setDescriptionFormatted(cEvent.getDescriptionFormatted()); cedit.setRecurrenceRule(cEvent.getRecurrenceRule()); calendar2.commitEvent(cedit); //LOG.warn(cEvent.getDisplayName()); } //save calendar 2 CalendarService.commitCalendar(calendar2); } catch (Exception e) { LOG.error("WS copyCalendarEvents(): error " + e.getClass().getName() + " : " + e.getMessage()); return e.getClass().getName() + " : " + e.getMessage(); } return "success"; } } >>>>>>> .fusion-droit.r56083