Reputation: 283
I have created a GMail Add-on and I am currently using PropertiesService.getUserProperties() to save some configuration data. Recently however, we have a need to share configuration data for all users in the same domain. And I was thinking of using the PropertiesService.getScriptProperties() to do so.
I would just like to ask a question related to the following documentation on Google's apps script :
https://developers.google.com/apps-script/reference/properties/properties-service
For properties set using the scriptProperties.setProperty() API, will the property set be shared with all users regardless of domain? Or only users under the same domain will share the same value?
So for example, 2 unrelated domains (domain1.com and domain5.com) installed my GMail Add-on. [email protected] sets the add-on scriptproperty "url" to "google.com", will [email protected] see the url as "google.com" as well? Or only users under domain1.com will be able to see "url" as "google.com".
Thank you.
Upvotes: 2
Views: 756
Reputation: 31310
You can't use Script Properties to restrict access to Properties Service data to only one domain. You'll probably need to get the domain name of the user when the code runs, and if the user is in the domain, then run special code to access settings somewhere for that domain.
The settings specific to the domain could be put in various places, including your Script Properties. But the problem with using Script Properties could be storage quota depending upon how much data you need to store. If it's small amount of data, like whether that domain has paid there subscription or not and the date, then definitely put it in Script Properties. But if you had lots of subscribers, then you'd want to have an external storage somewhere.
The domain name can be retrieved with the following code:
In order to use the below code, you'll need 3 scopes in your appsscript.json manifest file.
"oauthScopes": ["https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"]
CODE:
var users = Session.getEffectiveUser().getEmail();
var urls = "https://www.googleapis.com/oauth2/v2/userinfo";
var params = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
};
var response = UrlFetchApp.fetch(urls, params);
if (response && response.getResponseCode() !== 200) {//There should always be a response unless the
//Fetch call failed and if that happens then the outer try - catch can handle it
return false;
}
var userinfo = JSON.parse(response.getContentText());
Logger.log(userinfo)
var domainName = userinfo.hd ? userinfo.hd : false;
return domainName;
//userinfo["given_name"];//First Name
//userinfo["name"];//Full Name
//userinfo["id"];
//userinfo["family_name"];
//userinfo["email"];
//userinfo["picture"];//Link to profile image
//userinfo["locale"];//ID of country de = Germany
Upvotes: 2
Reputation: 6072
When you are setting a property using the setProperties(properties)
method, the property is set for the script itself.
According to the documentation:
setProperties(properties)
Sets all key-value pairs from the given object in the current Properties store.
Therefore, if your concern is related to which users can access these properties, this is win fact related to the sharing permissions you have set for the script itself.
For instance, when sharing a script, you have the following options:
restricted
sharing only with your organization
anyone who was the link can access the script
As well as:
viewer
editor
You can also fiddle with these permissions programmatically by checking the Access Enum
and the Permission Enum
Upvotes: 1