firebase规则:如何根据用户角色限制访问

2023-12-26

我是 Firebase 新手,正在尝试了解安全规则。为此,我正在实现项目 - 团队成员 - 任务的典型功能。 每个项目都会有一个团队负责人、多个成员和多个任务。

这是我试图实现的结构和规则(也称为要求):

/Members - each member has { displayName, email, emailVerified }
    any logged in user should be able to read data from Members (to get the 
        display names of all users)
    any logged in user should be able to update his/her record

/Projects - each project has { Lead, Members{}, Name, Tasks{} }
    any logged in user should be able to read the list of projects
    any logged in user should be able to read the list of members (if possible
        only for the projects where they are part of)
    any logged in user should be able to read the list of tasks (if possible only 
        for the projects where they are part of)
    only the team leader should be able to update project details i.e.
        - add / remove members
        - add / remove tasks
        - change project title

/Tasks - { project, status, title }
    team leader / team members should be able to read the tasks
    team leader can add/edit/delete tasks
    team members can update only status (of a task that is associated with their project)
    team leader / team members should be able to filter project tasks based on 
    task status (completed / not completed)

我设置了以下 Firebase 规则:

{
 "rules": {
    "Members": {
        ".read": "auth != null",
        "$mid" : {
            ".write": "auth != null && auth.uid == $mid"
        }
    }, // Members
    "Projects": {
        ".read": "auth != null",
        // only team lead can edit project details
        ".write": "auth != null && auth.uid == data.child('Lead').val()",
        // Lead and Name are mandatory fields
        ".validate": "newData.hasChildren(['Lead', 'Name'])",
        "Name": {
            ".validate": "newData.isString() && newData.val().length > 0"
        },
        "Lead": {
            ".validate": "root.child('Members/' + newData.val()).exists()"
        },
        "Members": {
            "$mid": {
                ".validate": "root.child('Members/' + $mid).exists()"
            }
        },
        "Tasks": {
            "$tid": {
                ".validate": "root.child('Tasks/' + $tid).exists()"
            }
        }
    }, // Projects
    "Tasks": {
        // allow read / write only if current user is team lead or a member of the project
        ".read": "(auth != null) && (data.child('project').val() == 'Project1')",
        ".write": "auth != null && ( root.child('Projects/' + newData.child('project').val() + '/Lead').val() == auth.uid || root.child('Projects/' + newData.child('project').val() + '/Members/' + auth.uid).exists() )",
        ".validate": "newData.hasChildren(['project', 'status', 'title'])",
        "status": {
            ".validate": "newData.isString() && newData.val().length > 0"
        },
        // if team member is saving the item, allow changes only to status
        "title": {
            ".validate": "(root.child('Projects/' + newData.parent().child('project').val() + '/Lead').val() == auth.uid) ? newData.isString() && newData.val().length > 0 : data.exists() && data.val() == newData.val()"
        }
    } // Tasks
 } // rules
}

目前我正在评估.read功能。我没有测试过.write功能还没有。

我能够得到Members名单(成员的displayName)对于给定的项目。 但是在获取项目的任务详细信息时(来自/Tasks)我收到权限被拒绝的错误。

请注意,我想使用.read规则与.write规则为Tasks。但当我收到错误时,我将其更改为当前规则(这样,任何经过身份验证的用户都可以读取任务Project1 - Project1是一个项目的关键)。即使那样我的许可也被拒绝。如果我只保留"auth != null"然后我就可以阅读任务,但这不是我想要的。

有人可以帮助我了解我应该对 Firebase 规则进行哪些更改才能实现上述要求吗?


在尝试了不同的组合之后,我发现了这一点。

我试图访问/Tasks with orderByChild('project').equalTo(projectKey)以便获取与项目相关的任务的详细信息。但是当我这样做时,.read规则执行于/Tasks级别并且没有命名的孩子'project'在那个级别。'project'可以在/Tasks/<taskId>/project。所以我需要改变Task规则为:

"Tasks": {
    "$tid": {
        // allow read / write only if current user is team lead or a member of the project
        ".read": "auth != null && ( root.child('Projects/' + data.child('project').val() + '/Lead').val() == auth.uid || root.child('Projects/' + data.child('project').val() + '/Members/' + auth.uid).exists() )",
        ".write": "auth != null && ( root.child('Projects/' + newData.child('project').val() + '/Lead').val() == auth.uid || root.child('Projects/' + newData.child('project').val() + '/Members/' + auth.uid).exists() )",
        ".validate": "newData.hasChildren(['project', 'status', 'title'])",
        "status": {
            ".validate": "newData.isString() && newData.val().length > 0"
        },
        // if team member is saving the item, allow changes only to status
        "title": {
            ".validate": "(root.child('Projects/' + newData.parent().child('project').val() + '/Lead').val() == auth.uid) ? newData.isString() && newData.val().length > 0 : data.exists() && data.val() == newData.val()"
        }
    }
} // Tasks

即使使用此规则访问/Tasks with orderByChild('project').equalTo(projectKey)给予许可被拒绝。这是因为现在没有.read规则定义于/Tasks等级。所以我需要改变程序逻辑来迭代/Projects/<projectId>/Tasks并且对于每个taskId access /Tasks/<taskId>。当我这样做时,.read规则得到正确评估,用户只能访问其所属项目的任务详细信息。然后我需要在客户端处理这些任务详细信息,以区分已完成的任务和未完成的任务。

我还没有核实.write and .validate规则。但与此同时,我会等待有人确认我的理解或纠正它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

firebase规则:如何根据用户角色限制访问 的相关文章

随机推荐

  • 如何在运行时从 NUnit 测试运行中获取单元测试方法属性?

    我将有关给定测试的各种信息 多个错误跟踪系统的 ID 存储在属性中 如下所示 TestCaseVersion 001 B 8345 X543 public void TestSomethingOrOther 为了在测试过程中获取这些信息 我
  • 如何以编程方式为视图分配 ID?

    在 XML 文件中 我们可以为视图分配一个 ID 例如android id id something 然后打电话findViewById 但是当以编程方式创建视图时 如何分配 ID I think setId 与默认分配不同 setId 是
  • 使用动态而不是反射来按名称调用方法

    使用 NET 4 0 我将如何使用 Dynamic 来完成以下任务而不使用反射 public void InvokeMethod string methodName Type t typeof GCS WebService GCS WebS
  • 如何从为结果启动的活动返回字符串

    我已经启动了结果活动 但如何从该活动返回类似参数的字符串 只需使用以下代码块 Intent intent new Intent intent putExtra RESULT STRING string setResult RESULT OK
  • 通过点击图像(jquery 或 javascript)动态生成地图区域坐标

    我想知道是否有一种方法可以通过单击图像的某些部分来动态生成地图区域坐标 这是为了生成完整的图像图 举个例子 我有这张图片 单击此处查看图像 http mauricio lairet es otr plano jpg 通过单击它 我想为每个定
  • 切片类型的切片

    我目前正在努力通过优秀的围棋之旅 https tour golang org 我使用以下解决方案完成了其中一项练习 45 func Pic dx dy int uint8 pic make uint8 dy type declaration
  • 使用带有大 int 的 cython 时出现 OverflowError

    python 3 4 Windows 10 cython 0 21 1 我正在用 cython 将此函数编译为 c def weakchecksum data Generates a weak checksum from an iterab
  • 将字体大小转换为英寸

    我需要在之间进行转换Drawing Font Size 浮动 和 WPFFontSize 双精度 WPF 像素 最后 我决定将字体大小 以英寸为单位 存储在数据库中 如何将 GDI FontSize 转换为英寸 将 WPF FontSize
  • 展平相同类型的嵌套列表

    假设我想展平相同类型的嵌套列表 例如 ListA Element A Element B ListA Element C Element D ListB Element E Element F ListA包含相同类型的嵌套列表 ListA
  • 在 YAML 中构建字典项数组?

    基本上尝试在 yaml 中执行一些可以使用此 json 完成的操作 models model a type x bunch of properties model b type y bunch of properties 到目前为止 这就是
  • XD 代理 Facebook

    我正在使用 facebook connect 登录我的网站 在我的 html 页面中我编写代码 div div
  • Jenkins 和 Artifactory 的 Nuget 登录错误

    遇到问题Nuget Jenkins and 人工工厂 似乎无法获取Jenkins管道来识别Nuget配置 什么在起作用 使用我正在尝试阅读的帐户登录神器 查看我尝试访问的存储库和工件 使用 nuget 命令行访问存储库并在出现提示时输入用户
  • 返回新的 LINQ 对象

    我想编写 LINQ 它返回新对象 string int 包含 字符串 位置名称 int 位置计数 Output PositionA 8 PostionB 12 PostionC 13 这是我到目前为止所拥有的 public List
  • Android Studio 2022.2.1:网络检查器数据不可用

    我正在使用最新最好的 Android Studio 版本 Android Studio Flamingo 2022 2 1 Build AI 222 4459 24 2221 9862592 built on March 31 2023 R
  • 按索引合并两个数据帧

    我有以下数据框 gt df1 id begin conditional confidence discoveryTechnique 0 278 56 false 0 0 1 1 421 18 false 0 0 1 gt df2 conce
  • 如何在 webView 中启用 javascript

    在android中 如果我在webView中使用javascript 它会强制关闭 是否有可能在 webView 中使用 java 脚本 请帮忙 01 10 10 08 51 513 W dalvikvm 5994 JNI WARNING
  • 连续 WebJobs 和 CancellationToken

    我不明白取消令牌和网络作业背后的机制 我知道我可以使用Microsoft Azure WebJobs WebJobsShutdownWatcher Token获取令牌并做出反应token IsCancellationRequested例如
  • 获取C# WinForms App的应用程序图标

    我已使用 项目属性 选项卡为 C WinForms 应用程序分配了一个图标 该图标在构建时与程序清单一起提供 有没有办法获得System Drawing Icon在运行时获取此图标的对象 而无需再次将其嵌入资源中 我已经做了我的研究 有一种
  • Laravel Echo Server、Redis、Socket.IO:似乎无法使它们工作

    我正在使用 Laravel 开发实时应用程序 由于我不想使用 Pusher 所以我尝试使用 websockets 来工作Laravel 回声服务器 https github com tlaverdure laravel echo serve
  • firebase规则:如何根据用户角色限制访问

    我是 Firebase 新手 正在尝试了解安全规则 为此 我正在实现项目 团队成员 任务的典型功能 每个项目都会有一个团队负责人 多个成员和多个任务 这是我试图实现的结构和规则 也称为要求 Members each member has d