Firebase Firestore安全访问角色系统
我知道有很多关于使用安全规则在firestore上创建角色系统的线程可用,但我仍然无法做到这一点。我希望能够有三个不同的角色,三个不同的访问级别Firebase Firestore安全访问角色系统,firebase,google-cloud-firestore,firebase-authentication,firebase-security,Firebase,Google Cloud Firestore,Firebase Authentication,Firebase Security,我知道有很多关于使用安全规则在firestore上创建角色系统的线程可用,但我仍然无法做到这一点。我希望能够有三个不同的角色,三个不同的访问级别 管理员:可以读取和写入组织内的任何内容 经理:可以阅读组织内的任何内容,但除非是他们自己的文件,否则不能写作 员工:可以读取和更新自己的数据 我已经找到了google关于这个主题()的文档,但我不希望每个用户和estabishment上的每个经理或管理员,因为这是太多的重复数据。此外,角色应适用于组织内的任何地方,每个子集合的角色不应不同 我的数据
- 管理员:可以读取和写入组织内的任何内容
- 经理:可以阅读组织内的任何内容,但除非是他们自己的文件,否则不能写作
- 员工:可以读取和更新自己的数据
organisations{
organisation1{
<data about organisation>
establishments{
establishment1{
<data about establishment>
}
establishment2{
<data about establishment>
}
}
people{
user1{
<data about user>
userId: <UID from authentication>
accountType: <Administrator, Manager or Employee>
}
user2{
<data about user>
userId: <UID from authentication>
accountType: <Administrator, Manager or Employee>
}
}
}
organisation2{
<data about organisation>
establishments{
establishment1{
<data about establishment>
}
establishment2{
<data about establishment>
}
}
people{
user1{
<data about user>
userId: <UID from authentication>
accountType: <Administrator, Manager or Employee>
documents{
some documents
}
}
user2{
<data about user>
userId: <UID from authentication>
accountType: <Administrator, Manager or Employee>
}
}
}
}
我现在的代码没有给出错误,但它的行为与我预期的不同。当我使用管理员帐户登录时,我首先只看到自己的用户记录。如果我使用Beyerder或Medewerker帐户登录,我根本没有任何记录,甚至连我自己的记录也没有。此外,如果我将我的帐户更改回管理员,我将无法恢复我自己的记录。有没有办法解决这个问题/我的代码出了什么问题?此外,我是否需要在每个文档中都包含一个组织ID,或者是否有解决方法?我现在的代码确实有这个功能。谢谢你的帮助 由于这个问题相当大,这里有两件事: 1.具有3级访问角色的示例项目(针对此问题制作) 完整的开放源代码示例是,如果需要,可以随意克隆/分叉和贡献 Firestore型号
- 组织(收集)
- 组织1(文档)
- 组织名称(字段)
- 创建数据(字段)
- 机构单位(收集)
- 建立1(文档)
- 建立2(文档)
- 人民(收藏)
- 用户ID(文档)
- uid:用户id(字段)
- 帐户类型:“管理员”| |“版主”| |“牡丹”(字段)
- 文件(收集)
- docId(doc)
- 用户ID(文档)
- 组织1(文档)
function getAccountType(orgId, userId) {
return get(/databases/$(database)/documents/organizations/$(orgId)/peoples/$(userId)).data.accountType;
}
- 阅读:他所在的整个组织(包括机构、人民文件、组织)
- 写:他所在的整个组织(包括机构、人民文件、组织)
- 阅读:他所在的整个组织(包括机构、人民文件、组织)
- 写:只写他自己的文档(
)组织/orgId/peoples/userId/documents/*
function getAccountType(orgId, userId) {
return get(/databases/$(database)/documents/organizations/$(orgId)/peoples/$(userId)).data.accountType;
}
- 阅读:只有他自己的文件(
),而不是组织或机构组织/orgId/peoples/userId/documents/*
- 写:只写他自己的文档(
)组织/orgId/peoples/userId/documents/*
请求.data
或匹配规则中的id
function isAdmin(orgID){
return getAccountType(orgID, request.auth.uid) == "admin";
}
检查当前登录的用户request.auth.uid是否在参数中具有给定组织的“admin”角色
match /organizations/{orgID} {
allow read: if authenticated() && (isAdmin(orgID) || isModerator(orgID));
仅当用户已登录并且是管理员或版主时才允许读取
完整的规则可用
2.一般指导: 遵守干净的规则:
isAdmin()
isManager()
isPeon()
您可以在以后轻松使用和阅读:
match /reminders/{reminderId} {
allow read: if authenticated() && (isAdmin(resource.data.organizationId) || isManager(...) || isPeon(...));
allow write: if authenticated() && (isAdmin(request.resource.data.organizationId) || isManager(...));
allow delete: if authenticated() && isAdmin(resource.data.organizationId);
}
一些firestore.rules
示例:和
设置单元测试(⚠️ 重要信息)用于您的规则,因此更简单的方法是对其进行迭代,并检查所有内容是否按预期工作。
测试示例,请使用
jest
运行它,因为这个问题相当大,这里有两件事:
1.具有3级访问角色的示例项目(针对此问题制作)
完整的开放源代码示例是,如果需要,可以随意克隆/分叉和贡献
Firestore型号
- 组织(收集)
- 组织1(文档)
- 组织名称(字段)
- 创建数据(字段)
- 机构单位(收集)
- 建立1(文档)
- 建立2(文档)
- 人民(收藏)
- 用户ID(文档)
- uid:用户id(字段)
- 帐户类型:“管理员”| |“版主”| |“牡丹”(字段)
- 文件(收集)
- docId(doc)
- 用户ID(文档)
- 组织1(文档)
function getAccountType(orgId, userId) {
return get(/databases/$(database)/documents/organizations/$(orgId)/peoples/$(userId)).data.accountType;
}
- 阅读:他所在的整个组织(包括机构、人民文件、组织)
- 写:他所在的整个组织(包括机构、人民文件、组织)
- 阅读:他所在的整个组织(包括机构、人民文件、组织)
- 写:只写他自己的文档(
)组织/orgId/peoples/userId/documents/*
function getAccountType(orgId, userId) {
return get(/databases/$(database)/documents/organizations/$(orgId)/peoples/$(userId)).data.accountType;
}
- 阅读:只有他自己的文件(
),而不是组织或机构组织/orgId/peoples/userId/documents/*
- 写:只写他自己的文档(
)组织/orgId/peoples/userId/documents/*
请求.data
或匹配规则中的id
function isAdmin(orgID){
return getAccountType(orgID, request.auth.uid) == "admin";
}
检查当前登录的用户<