Sites are the fundamental organizational unit in Sakai. A site is a workspace that contains pages, tools, and users with specific roles and permissions.
In Sakai terminology, “site” and “workspace” are often used interchangeably. A site is essentially a container for organizing tools, users, and content.
package org.sakaiproject.site.api;/** * Site is the object that knows the information, tools and * layouts for a Sakai Site. */public interface Site extends Edit, Comparable, Serializable, AuthzGroup { // Property names public static final String PROP_SITE_CONTACT_EMAIL = "contact-email"; public static final String PROP_SITE_CONTACT_NAME = "contact-name"; public static final String PROP_SITE_TERM = "term"; public static final String PROP_SITE_TERM_EID = "term_eid"; public static final String PROP_SITE_LANGUAGE = "locale_string"; /** * @return the user who created this. */ User getCreatedBy(); /** * @return the user who last modified this. */ User getModifiedBy();}
Groups organize subsets of users within a site. Groups can have their own permissions and can be used to restrict access to tools or resources.
// Get site groupsCollection<Group> groups = site.getGroups();for (Group group : groups) { String groupId = group.getId(); String groupTitle = group.getTitle(); String groupDescription = group.getDescription(); // Get group members Set<Member> members = group.getMembers(); // Get group properties ResourceProperties props = group.getProperties();}// Get a specific groupGroup group = site.getGroup(groupId);// Create a new groupGroup newGroup = site.addGroup();newGroup.setTitle("Study Group A");newGroup.setDescription("Monday study session");
The SiteService provides comprehensive site management:
SiteService siteService = ComponentManager.get(SiteService.class);// Get current siteString currentSiteId = ToolManager.getCurrentPlacement().getContext();Site currentSite = siteService.getSite(currentSiteId);// Check if site existsboolean exists = siteService.siteExists(siteId);// Get user's sitesList<Site> userSites = siteService.getUserSites();// Get sites of a specific typeList<Site> courseSites = siteService.getSites( SelectionType.ANY, // selection type "course", // site type null, // search term null, // properties SortType.TITLE_ASC, // sort order null // paging);
Sites implement the AuthzGroup interface, which means they define:
Roles: Named sets of permissions (e.g., “Instructor”, “Student”)
Users: Members with assigned roles
Permissions: What each role can do
// Site implements AuthzGroupAuthzGroup siteRealm = site;// Get user's role in siteRole userRole = siteRealm.getUserRole(userId);// Check if user has permissionboolean canEdit = siteRealm.isAllowed(userId, "site.upd");// Get all membersSet<Member> members = siteRealm.getMembers();// Get users with a specific roleSet<Member> instructors = siteRealm.getUsersHasRole("Instructor");
Always use AuthzGroupService or the site’s authorization methods to check permissions. Never implement your own authorization logic.
Sites can have custom properties stored as name-value pairs:
// Get site propertiesResourceProperties props = site.getProperties();// Get standard propertiesString contactName = props.getProperty(Site.PROP_SITE_CONTACT_NAME);String contactEmail = props.getProperty(Site.PROP_SITE_CONTACT_EMAIL);String term = props.getProperty(Site.PROP_SITE_TERM);String language = props.getProperty(Site.PROP_SITE_LANGUAGE);// Get custom propertiesString customProp = props.getProperty("institution.dept.code");// Set properties (when editing)ResourcePropertiesEdit propsEdit = site.getPropertiesEdit();propsEdit.addProperty("custom.property", "value");
Each user has a personal workspace (My Workspace) with site ID in the format ~{userId}:
// Get current user's workspaceString userId = SessionManager.getCurrentSessionUserId();String workspaceId = siteService.getUserSiteId(userId);// workspaceId will be "~userid"Site workspace = siteService.getSite(workspaceId);// Alternative: Get user site directlySite userSite = siteService.getSiteUserId(userId);
Before accessing a site, verify the user has permission:
// Check if user can access siteif (!siteService.allowAccessSite(siteId)) { throw new PermissionException(userId, "site.visit", siteId);}Site site = siteService.getSite(siteId);
Use Site Context in Tools
Tools should always operate within the context of the current site:
// Get current site contextString siteId = ToolManager.getCurrentPlacement().getContext();Site site = siteService.getSite(siteId);// Don't hardcode site IDs// Site site = siteService.getSite("abc123"); // WRONG!
Save Sites After Modifications
Always save sites after making changes:
Site site = siteService.getSite(siteId);site.setTitle("New Title");// Don't forget to save!siteService.save(site);
Use Groups for Organization
Leverage groups for organizing users and controlling access:
// Create groups for different sectionsGroup section1 = site.addGroup();section1.setTitle("Section 1 - Morning");section1.getProperties().addProperty("sections_category", "Lecture");// Add users to groupsection1.addMember(userId, "Student", true, false);
public class MyToolController { public String getCurrentSiteTitle() { try { // Get current placement's context (site ID) String siteId = ToolManager.getCurrentPlacement().getContext(); // Get the site SiteService siteService = ComponentManager.get(SiteService.class); Site site = siteService.getSite(siteId); return site.getTitle(); } catch (IdUnusedException e) { log.error("Site not found", e); return "Unknown Site"; } }}
public boolean isUserInstructor(String siteId, String userId) { try { Site site = siteService.getSite(siteId); Member member = site.getMember(userId); if (member == null) { return false; } Role role = member.getRole(); return role != null && "Instructor".equals(role.getId()); } catch (IdUnusedException e) { return false; }}