【问题标题】:How can I deny the admin from calling Firebase functions?如何拒绝管理员调用 Firebase 函数?
【发布时间】:2019-10-28 22:16:13
【问题描述】:

我有两个 firebase 函数触发器:

  1. 用户更新
  2. 更新所有者

当所有者 onUpdate 被触发时,它会进行操作并更新用户文档。

用户 onUpdate 由最后一个操作调用,而不是来自客户端。

有没有办法拒绝管理员或服务器调用函数?

我搜索Firebase documentation,发现如果我使用:

console.log(context.auth.uid);

它将显示进行更改的用户 ID,但我收到此错误:

Cannot read property 'uid' of undefined

所以我尝试打印上下文对象,结果如下:

context : { eventId: '7eac4df1-85bd-47dd-b83a-827b63597e10-0',
 eventType: 'google.firestore.document.update',
 notSupported: {},
 params: 
  { serviceId: 'bY0RjOSgygnXuojmDyng',
    userId: 'Poz0UKIShnSUJNPCrug3zvfiyKn1' },
  resource: 
   { service: 'firestore.googleapis.com',
    name: 'projects/*Project name*/databases/(default)/documents/users/Poz0UKIShnSUJNPCrug3zvfiyKn1/services/bY0RjOSgygnXuojmDyng' },
 timestamp: '2019-06-14T06:33:48.525983Z' }

所以,我的答案是:如果从服务器或客户端应用程序进行调用,是否有返回真/假的函数?

我使用谷歌身份验证,当我调用该函数时,我已通过身份验证

编辑

Android 用户代码

用户删除订单。 (对于 GDPR 政策,我无法删除它,但我需要在取消属性后使其不可见)

FirebaseFirestore db = FirebaseFirestore.getInstance();
    db.collection("users").document(User.getUserId())
            .collection("orders").document(orderId)
                .update("canceled", true);

UserCode Firebase 函数

exports.updateLocalOrder = functions.firestore
    .document('users/{userId}/orders/{orderId}')
    .onUpdate((change, context) => {

      const order = change.after.data();
      const orderId = ontext.params.orderId;
      const ownerId = order.ownerId;

      if (order.canceled){

          admin.firestore().collection("owners")
              .doc(ownerId).collection("orders")
                  .doc(orderId).update("canceled", true);

      }

      ...
});

Android 所有者代码

店主删除订单。 (对于 GDPR 政策,我无法删除它,但我需要在取消属性后使其不可见)

FirebaseFirestore db = FirebaseFirestore.getInstance();
    db.collection("owners").document(Owner.getOwnerId())
            .collection("orders").document(orderId)
                .update("canceled", true);

OwnerCode Firebase 函数

exports.updateUserOrder = functions.firestore
    .document('owners/{ownerId}/orders/{orderId}')
    .onUpdate((change, context) => {

      const order = change.after.data();
      const orderId = ontext.params.orderId;
      const userId = order.userId;

      if (order.canceled){

          admin.firestore().collection("users")
              .doc(userId).collection("orders")
                  .doc(orderId).update("canceled", true);

      }

      ...
}); 

因此,每次我删除订单时,都会从客户端(Android 应用)调用 Functions,然后从服务器进行无限调用。

重要!!!

我不想制作另一个名为“订单”的集合。所以问题不在于我如何管理我的应用程序或如何管理用户和所有者之间的订单,因为我只需要知道如何区分调用。

【问题讨论】:

  • uid 代表 userId 吗?
  • 请使用您的函数的代码编辑问题。很难说你想做什么。
  • @DougStevenson 已编辑

标签: android node.js firebase google-cloud-functions


【解决方案1】:

这个问题没有真正的答案,但我已经解决了。

当我更新文档时,如果任务成功,我会对 Firebase 函数进行 HTTP 调用并更新所有者文档。我将所有 Firestore 触发器都转换为 OnCall 函数,因此没有递归。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-09
    • 2023-03-22
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 2016-07-16
    • 2013-04-29
    • 2011-11-05
    相关资源
    最近更新 更多