Skip to content

Instantly share code, notes, and snippets.

@Yagisanatode
Last active August 18, 2021 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yagisanatode/7d4f11de19c9a2e1bdc7ea09d4016645 to your computer and use it in GitHub Desktop.
Save Yagisanatode/7d4f11de19c9a2e1bdc7ea09d4016645 to your computer and use it in GitHub Desktop.
Added link to tutorial
{
"timeZone": "Australia/Hobart",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"webapp": {
"executeAs": "USER_ACCESSING",
"access": "ANYONE"
}
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1 style="color:blue">You are worthy!</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1 style="color:red">You are not worthy!</h1>
</body>
</html>
// ################################## VALIDATE USERS ON A WEB APP ##################################
// Allows users with a Gmail or Domain Google Workspace account to be validated and given access to
// a WebApp.
// Can be easily be adapted for Google Workspace Editor Add-on Side-bars and Dialogue Boxes
// @author Yagisanatode
// You can check out the full write up here: {@link https://yagisanatode.com/2021/08/18/how-to-validate-specific-users-on-a-web-app-in-google-apps-scripts}
// #################################################################################################
//##### GLOBALS #####
FILE_ID = '1JYq_HX_zCqyGYJzWtAkpIoVruPVAqYKDfKYW9F0A92o'
//####################### Create Webpage for Webapp ############################
/**Creates webpage for web app.
* First calls validators to check if user has edit access to selected file as
* either an individual user or a group.
* Builds 'Index' file if user has access otherwise builds 'NonUser' file.
*/
function doGet() {
const userEmail = Session.getActiveUser().getEmail()
const fileID = FILE_ID;
const isValid = validate(userEmail, fileID)
let htmlfile = (isValid)? 'Index':'NonUser';
const html = HtmlService.createTemplateFromFile(htmlfile)
.evaluate()
.setTitle("The Worthy")
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
.addMetaTag('viewport', 'width=device-width, initial-scale=1');
return html;
};
/**
* Checks if user is an editor of a file.
* First checks email, then check if member of group.
* @param {string} email - email address of active user.
* @param {string} fileID - the connected file ID with the shared editors.
* @returns {boolean} true if user is and editor of file. Otherwise, false.
*/
function validate(email, fileID){
let isValid = false; //Indicates if email is an editor.
/**
* Get's list of file editors by email.
* @returns {array} list of emails of all editors in file
*/
const emailList = (()=>{
var file;
try{
file = DriveApp.getFileById(fileID);
}catch(e){
return false; // If user has no access.
}
return file.getEditors().map(editor => {
return editor.getEmail();
})
})();
if(!emailList){
isValid = false;
return isValid;
}
/**
* Get's list any group email with edit permission that
* the user is a memeber of.
* @returns {boolean} true if match.
*/
const groupEmailList = (()=>{
let isMemberOfGroup = false;
GroupsApp.getGroups().map(group => {
return group.getEmail()
}).forEach(group => {
if (emailList.includes(group)) {
isMemberOfGroup = true
}
})
return isMemberOfGroup;
})()
//Check if email in editor list.
if(emailList.includes(email)){
isValid = true;
}else if(groupEmailList){
isValid = true;
}
return isValid;
}
@Yagisanatode
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment