To dynamically show links to individual Google Sites based on a user's group membership, you can leverage Google Apps Script combined with Google Groups and Google Sites. Here's a step-by-step approach to achieve this:
Solution Overview
- Google Groups: Each Google Site is controlled by a specific Google Group.
- Google Apps Script: Use a script to check the user's group membership and dynamically generate links to the sites they have access to.
- Google Sites (Landing Page): Embed the script's output (dynamic links) into your main landing page.
Steps to Implement
1. Enable Google Apps Script API
- Go to the Google Cloud Console.
- Create a new project or select an existing one.
- Enable the Google Apps Script API and Admin SDK API (for group membership checks).
2. Create a Google Apps Script
Write a script to check the user's group membership and generate links to the sites they can access.
function doGet(e) {
const userEmail = Session.getActiveUser().getEmail();
const groups = getGroupsForUser(userEmail);
const sites = getSitesForGroups(groups);
return ContentService.createTextOutput(JSON.stringify(sites)).setMimeType(ContentService.MimeType.JSON);
}
function getGroupsForUser(userEmail) {
const groups = AdminDirectory.Groups.list({ userKey: userEmail, maxResults: 100 });
return groups.groups.map(group => group.email);
}
function getSitesForGroups(groups) {
// Map of group emails to site URLs
const groupToSiteMap = {
"group1@example.com": "https://sites.google.com/a/example.com/site1",
"group2@example.com": "https://sites.google.com/a/example.com/site2",
// Add more groups and sites as needed
};
const accessibleSites = [];
for (const group of groups) {
if (groupToSiteMap[group]) {
accessibleSites.push({ name: `Site for ${group}`, url: groupToSiteMap[group] });
}
}
return accessibleSites;
}
Explanation:
doGet: Handles the request and returns a JSON response with accessible sites.
getGroupsForUser: Fetches the groups the user belongs to using the Admin SDK.
getSitesForGroups: Maps group emails to site URLs and filters accessible sites.
3. Deploy the Script as a Web App
- In the Google Apps Script editor, click on Deploy > New Deployment.
- Select Web App as the deployment type.
- Set the execution access to "Anyone" or "Users in your domain" (depending on your needs).
- Deploy and note the web app URL (e.g.,
https://script.google.com/macros/s/YOUR_SCRIPT_ID/exec).
4. Embed Dynamic Links in Google Sites
On your main landing page:
- Insert a HTML embed box.
- Add the following JavaScript code to fetch and display the links:
<script>
function loadAccessibleSites() {
const apiUrl = 'https://script.google.com/macros/s/YOUR_SCRIPT_ID/exec';
fetch(apiUrl)
.then(response => response.json())
.then(sites => {
const linksContainer = document.getElementById('links-container');
linksContainer.innerHTML = sites.map(site => `<a href="${site.url}">${site.name}</a><br>`).join('');
});
}
window.onload = loadAccessibleSites;
</script>
<div id="links-container"></div>
Note: Replace YOUR_SCRIPT_ID with the actual ID of your deployed script.
Alternative: Using Google Site's Built-In Features
If you prefer not to use Apps Script, you can manually manage links and rely on Google Groups for access control:
- Create separate sections or pages in your main site for each group.
- Share each section/page with the corresponding Google Group.
- Users will only see the sections/pages they have access to.
However, this approach is less dynamic and requires manual updates.
Security Considerations
- Ensure the script's web app deployment is secure and only accessible to intended users.
- Regularly audit Google Group memberships to maintain access control.
By using Google Apps Script, you can automate the process and provide a seamless experience for users accessing the main landing page.