首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firebase数据结构和安全规则

Firebase数据结构和安全规则
EN

Stack Overflow用户
提问于 2015-12-07 22:45:41
回答 2查看 405关注 0票数 0

我在firebase的安全规则方面遇到了问题,我不是100%出错的地方。我在想,也许我的数据结构是错误的:

代码语言:javascript
复制
{
"users": {
    "uid": {
        "displayName": "Name";
    }

},
"modules": {
    "id": {
        "title": "buttons",
        "uid": "(user id string)"
    },
    "id": {
        "title": "navbars",
        "uid": "(user id string)"
    }
},
"snippets": {
    "id = moduleID": {
        "id (of snippet)": "(id string)" {
            "uid (user ID)": "(string)",
            "body": {
                "css": "(some code)",
                "html": "(Some code)",
                "name": "(string)",
                "description": "(string)"
            }
        }
    }
}

应用程序中的一切工作正常,但当我开始添加安全规则时,我得到了访问被拒绝的错误。我只是想知道我的数据结构是正确的还是安全规则是完全错误的?

安全规则:

代码语言:javascript
复制
{
"rules": {
    "users": {
        "$uid": {
            // grants write and read access to the owner of this user account whose uid must exactly match the key ($uid)
            ".write": "auth != null && auth.uid == $uid",
            ".read": "auth != null && auth.uid == $uid"

        }
    },
    "snippets": {
        "$uid": {
            // grants write and read access to the owner of this user account whose uid must exactly match the key ($uid)
            ".write": "auth != null && auth.uid == $uid",
            ".read": "auth != null && auth.uid == $uid"
        }
    },
    "modules": {
        "$uid": {
            // grants write and read access to the owner of this user account whose uid must exactly match the key ($uid)
            ".write": "auth != null && auth.uid == $uid",
            ".read": "auth != null && auth.uid == $uid"
        }
    }
}

任何建议都将不胜感激。

EN

回答 2

Stack Overflow用户

发布于 2015-12-08 02:21:48

根据数据结构,这些规则似乎是错误的。

规则在每个节点中都有$uid,但您的数据与此不匹配。用户有uid,但模块有id,snippet有id = moduleID。

$uid是一个保存节点名的变量,因此可以在{}中引用它,因此您应该(为了可读性)将其他两个节点中的变量重命名为在每个{}中更有意义的名称。就像在模块中一样,让它成为$module_id。

然而。我认为其中的笑点是,您希望将读取代码片段和模块的权限限制为经过身份验证的用户。为此,您可以引用users节点。

.read规则应该是这样的

代码语言:javascript
复制
"modules": {
    "$module_id": {
        ".read": "auth != null && root.child('users/' + auth.id).exists()
    }

因此,您的模块节点可以由经过身份验证的用户读取,并且他们的uid也会出现在用户/节点中

票数 1
EN

Stack Overflow用户

发布于 2015-12-14 06:31:56

您是否正在使用Firebase Bolt编译器来执行规则?我不得不写一些复杂的规则,而且手工操作很快就会让人感到困惑。

下面是它的样子。很容易进行修改、编译和试用。

代码语言:javascript
复制
//current logged in user
isUser(uid) = auth != null && auth.uid == uid;
//does this module id exist
hasValidModule(module_id) = root['modules'][module_id] != null;

//dont let anyone read or write to top node
path / {
    read() = false;
    write() = false;
}
path /users/$user_id 
{   
    write() = isUser($user_id);
    read() = isUser($user_id);
}
path /snippets/$module_id/$snipit_id/$user_id 
{   
    write() = isUser($user_id) && hasValidModule($module_id);
    read() = isUser($user_id);
}
path /modules/$user_id 
{   
    write() = isUser($user_id);
    read() = isUser($user_id);
}

下面是它输出的json:

代码语言:javascript
复制
{
  "rules": {
    "users": {
      "$user_id": {
        ".read": "auth != null && auth.uid == $user_id",
        ".write": "auth != null && auth.uid == $user_id"
      }
    },
    "snippets": {
      "$module_id": {
        "$snipit_id": {
          "$user_id": {
            ".read": "auth != null && auth.uid == $user_id",
            ".write": "auth != null && auth.uid == $user_id && newData.parent().parent().parent().parent().child('modules').child($module_id).val() != null"
          }
        }
      }
    },
    "modules": {
      "$user_id": {
        ".read": "auth != null && auth.uid == $user_id",
        ".write": "auth != null && auth.uid == $user_id"
      }
    }
  }
}

Firebase博客上有一些信息,但真正对我有帮助的文档是

https://github.com/firebase/bolt/blob/master/docs/language.md

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34136289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档