有没有更直接的方法来使用云函数删除嵌套路径?

huangapple go评论63阅读模式
英文:

Is there a more straightforward way to delete a nested path using cloud-functions?

问题

I have the following code that proves to be really stable in deleting nested data from Firestore. However, I have a concern since it is using the Firebase token. I mean, is there a more straightforward way to eradicate nested collections using a cloud function? or would you consider this to be a good solution. I am using this code when a user is deleting their account, and due to some GPDR regulations, I need to make sure that no data is left, and I don't want to use any extensions.

// recursive delete function..
export const recursiveDeleteClients = functions
  .runWith({
    timeoutSeconds: 300,
    memory: '512MB',
  })
  .https.onCall(async (data, context) => {
    await firebase_tools.firestore.delete('data-personal-data/${context.auth?.uid}/nested-data/', {
      project: process.env.GCLOUD_PROJECT,
      recursive: true,
      yes: true,
      token: functions.config().fb.token, // firebase tools generated token
      force: true,
    });
    return null;
  });

Please note that this code is not translated, as requested.

英文:

I have the following code that proves to be really stable in deleting nested data from Firestore. However, I have a concern since it is using the Firebase token. I mean, is there a more straightforward way to eradicate nested collections using a cloud function ? or would you consider this to be a good solution. I am using this code when I user is deleting his account and due to some GPDR regulations, I need to make sure that no data are left and I don't want to use any extinctions.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// recursive delete function..

export const recursiveDeleteClients = functions
.runWith({
timeoutSeconds: 300,
memory: '512MB',
})
.https.onCall(async (data, context) => {
await firebase_tools.firestore.delete('data-personal-data/${context.auth?.uid}/nested-data/', {
project: process.env.GCLOUD_PROJECT,
recursive: true,
yes: true,
token: functions.config().fb.token, // firebase tools generated token
force: true,
});
return null;
});
<!-- end snippet -->

答案1

得分: 1

如果您担心谁可以调用此云函数来删除数据,请考虑实施在文档中的安全措施,该文档位于 删除集合 上,您使用的代码基于该文档:

exports.recursiveDelete = functions
  .runWith({
    timeoutSeconds: 540,
    memory: '2GB'
  })
  .https.onCall(async (data, context) => {
    // &#128071; 此部分在您的代码中缺失
    // 仅允许管理员用户执行此函数。
    if (!(context.auth && context.auth.token && context.auth.token.admin)) {
      throw new functions.https.HttpsError(
        'permission-denied',
        'Must be an administrative user to initiate delete.'
      );
    }
    // &#128070;

    const path = data.path;
    console.log(
      `User ${context.auth.uid} has requested to delete path ${path}`
    );

    // 在给定的文档或集合路径上运行递归删除。
    // 'token' 必须在函数配置中设置,并且可以通过在命令行上运行 'firebase login:ci' 来生成。
    await firebase_tools.firestore
      ...
英文:

If you're worried about who can call this Cloud Function to delete data, consider implementing the security measure that is present in the code sample in the documentation on deleting collections on which the code you use is based:

exports.recursiveDelete = functions
  .runWith({
    timeoutSeconds: 540,
    memory: &#39;2GB&#39;
  })
  .https.onCall(async (data, context) =&gt; {
    // &#128071; This part is missing in your code
    // Only allow admin users to execute this function.
    if (!(context.auth &amp;&amp; context.auth.token &amp;&amp; context.auth.token.admin)) {
      throw new functions.https.HttpsError(
        &#39;permission-denied&#39;,
        &#39;Must be an administrative user to initiate delete.&#39;
      );
    }
    // &#128070;

    const path = data.path;
    console.log(
      `User ${context.auth.uid} has requested to delete path ${path}`
    );

    // Run a recursive delete on the given document or collection path.
    // The &#39;token&#39; must be set in the functions config, and can be generated
    // at the command line by running &#39;firebase login:ci&#39;.
    await firebase_tools.firestore
      ...

huangapple
  • 本文由 发表于 2023年7月13日 10:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定