/**
* checkIfAuthorizationRequired
* From
*
* @return {Boolean} whether auth required
*/
checkIfAuthorizationRequired: function() {
Log_.functionEntryPoint()
var authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL)
var authRequired = false
// Check if the actions of the trigger requires authorization that has not
// been granted yet; if so, warn the user via email. This check is required
// when using triggers with add-ons to maintain functional triggers.
if (authInfo.getAuthorizationStatus() === ScriptApp.AuthorizationStatus.REQUIRED) {
authRequired = true
// Re-authorization is required. In this case, the user needs to be alerted
// that they need to re-authorize; the normal trigger action is not
// conducted, since it requires authorization first. Send at most one
// "Authorization Required" email per day to avoid spamming users
var properties = PropertiesService.getUserProperties()
var lastAuthEmailDate = properties.getProperty(PROPERTY_LAST_AUTH_EMAIL_DATE)
var today = new Date().toDateString()
if (lastAuthEmailDate !== today) {
if (MailApp.getRemainingDailyQuota() > 0) {
var html = HtmlService.createTemplateFromFile('Authorization')
html.url = authInfo.getAuthorizationUrl()
html.addonTitle = SCRIPT_NAME
var subject = 'Authorization Required'
var message = html.evaluate()
var options = {
name: SCRIPT_NAME,
htmlBody: message.getContent()
}
var recipient = Session.getEffectiveUser().getEmail()
MailApp.sendEmail(recipient, subject, message.getContent(), options)
Log_.warning('Sent re-auth email to ' + recipient)
} else {
Log_.warning('Unable to send "auth needed" email as run out of quota')
}
// Try again in a days time
properties.setProperty(PROPERTY_LAST_AUTH_EMAIL_DATE, today)
}
}
return authRequired
}, // Utils_.checkIfAuthorizationRequired()