Google Apps Script WebApp with simultaneous users overrides user recognition

Google Apps Script WebApp with Simultaneous Users Overrides User Recognition

我有一个项目可以让用户发送仅包含少量详细信息的表单。我曾经使用 google 的 Session class,但是当登录多个帐户或其他一些我几周都无法修复的原因时,这会把一切搞得一团糟。因此,我决定尝试另一种方法并完全削减 google 身份验证,并从 LMS 的 POST 数据中获取用户验证并将其设置为 UserProperty。没有 Google Auth 一切正常,我今天做了一个质量测试。结果,由于我的脚本必须 运行 作为“我”,所以该用户是脚本所有者帐户,这反过来使所有用户的 UserProperty 成为 ScriptProperty 类型,并且他们开始覆盖彼此的用户详细信息。

例如,从他们的 LMS 打开 WebApp 的第二个用户覆盖了第一个用户的用户详细信息,因为第一个用户的详细信息被第二个用户覆盖了,所有提交似乎都是由第二个用户发送的。下面的代码;

  function doPost(e) {
  var postContents = e.postData.contents.split("&");
  var postSplit = postContents[16].split("=");
  var activeUserEmail = postSplit[1].replace("%40", "@");
  userProperties.setProperty('activeUserEmail', activeUserEmail);

  if (activeUserEmail[2] == "s" || activeUserEmail[2] == ".") {
    console.log("Redirecting to Student: " + activeUserEmail);
    return HtmlService.createTemplateFromFile('student_form')
      .evaluate()
      .addMetaTag('viewport', 'width=device-width, initial-scale=1')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
  } 

此函数由 html 页面上的用户通过 google.script.run 使用 successhandler 调用,并在对电子表格和云端硬盘进行更多处理后传回页面。

function getData() {
  var activeUserEmail = userProperties.getProperty('activeUserEmail');
  console.log("getData User: " + activeUserEmail);
  try {
    if (activeUserEmail != "") {
      var userResults = teachersSheet.filter(function (row) { return row[0] == activeUserEmail; });
    } else {
      var userResults = "No User Email"
    }
    var userDepartment = userResults[0][2];

    if (userDepartment == "Admin") {
      var data = questionsSheet.getDisplayValues().filter(function (row) { return row[1] != "" && row[0] != "ID"; });
    }
    else {
      var data = questionsSheet.getDisplayValues().filter(function (row) { return row[3] == userDepartment && row[9] == "FALSE" && row[1] != ""; });
    };

    var data = [userResults, data];
    console.log(data);
    return data;
  } catch (e) {
    console.log(e);
  }
}

代码可能有点乱,但希望足够了。

我无法弄清楚如何将持久的用户详细信息保存在变量中或 属性 不会被第二个脚本 运行 覆盖。有什么想法吗?

状态可以通过在

中存储一个unique id来维护
  • 用户属性和
  • 客户端Javascript变量
const id = Utilities.getUuid()
userProperties.setProperty(String(id), activeUserEmail);

student_form html 也需要 evaluated 与此 idid,一次在客户端应该随每个请求发送到服务器。

google.script.run.getData(id)
function getData(id) {
  var activeUserEmail = userProperties.getProperty(String(id));
  console.log("getData User: " + activeUserEmail);
  //stuff