2016-08-12 59 views
5
{ 
    "rules": { 
     "users": { 
      "$uid":{ 
       //Private whatever under "uid" but Public is exposed 
       ".read": "auth != null && auth.uid == $uid", 
       ".write": "auth != null && auth.uid == $uid", 

       "public": { ".read": "auth != null" } 
       } 
       } 
      } 
} 
  • 我创建这些规则,让用户公/私轮廓
  • 用户/{} UID/公共”轮廓应该是那些被认证的任何用户访问,但在“用户/ UID

这里不能访问的数据存储在我的火力点数据库中的一些假数据。如何使用Firebase安全规则创建公/私用户配置文件?

{ 
    "users" : { 
    "YFIIAgwa2kaannrXjwvSZmoywma2" : { 
     "Name:" : "Example 1", 
     //This public child should be accessible by 
     //"Example 2" but cannot know the name of 
     // this user 
     "public" : { 
     "email" : "[email protected]" 
     } 
    }, 
    "YgSfSzPzxLbyDL17r6P9id2cdvH2" : { 
     "Name:" : "Example 2", 
     //This public child should be accessible by 
     //"Example 1" but cannot know the name of 
     // this user 
     "public" : { 
     "email" : "[email protected]" 
     } 
    } 
    } 
} 

我想知道这是否是防止任何用户访问用户重要信息的可靠方法!无论如何,我可以通过验证来改善这一点吗?我很乐意接受你们的建议。我想为我的应用程序创建最佳和简单的安全规则。

+0

请拼写您的标题。 – 2016-08-12 17:04:56

回答

7

您可以使用当前的数据结构来确保访问私人和公共数据。

但是您可能需要的一种用例是显示所有用户的公共信息列表。用你目前的数据结构是不可能的,因为Firebase's security model cannot be used to filter data。有关这方面的很好的答案,请参阅Restricting child/field access with security rules

大多数开发商在完全独立的子树分割的公共和私有数据:

{ 
    "users" : { 
    "YFIIAgwa2kaannrXjwvSZmoywma2" : { 
     "Name:" : "Example 1", 
    }, 
    "YgSfSzPzxLbyDL17r6P9id2cdvH2" : { 
     "Name:" : "Example 2", 
    } 
    }, 
    "public_profiles": { 
    "YFIIAgwa2kaannrXjwvSZmoywma2" : { 
     "email" : "[email protected]" 
    }, 
    "YgSfSzPzxLbyDL17r6P9id2cdvH2" : { 
     "email" : "[email protected]" 
    } 
    } 
} 

可以与当时的安全访问:

{ 
    "rules": { 
    "users": { 
     "$uid":{ 
      ".read": "auth != null && auth.uid == $uid", 
      ".write": "auth != null && auth.uid == $uid", 
     } 
    }, 
    "public_profiles": { 
     ".read": "auth != null", 
     "$uid":{ 
      ".write": "auth != null && auth.uid == $uid", 
     } 
    } 
    } 
} 

现在,任何身份验证的用户可以收听/public_profiles,这意味着你可以轻松显示这些配置文件的列表。

0

嗯不会更容易(重新)结构分贝,让你有一个公共和私人领域每个用户?喜欢的东西:

{ 
    "users" : { 
    "YFIIAgwa2kaannrXjwvSZmoywma2" : { 
     "private": { 
     "Name:" : "Example 1" 
     }, 
     "public" : { 
     "email" : "[email protected]" 
     } 
    }, 
    "YgSfSzPzxLbyDL17r6P9id2cdvH2" : { 
     "private": { 
     "Name:" : "Example 2" 
     }, 
     "public" : { 
     "email" : "[email protected]" 
     } 
    } 
    } 
} 

/UPD:这样,它应该是很容易(ER)有不同的权限,因为他们不会从父继承呢?

+0

我喜欢你如何分开。我想我可以做到这一点,但是当我想向它添加更多数据时,它不会在将来产生嵌套问题!有没有办法为公共数据创建一个单独的树? – user2884707bond

+0

您可以使用公共配置文件作为“默认”数据并使用它在私有字段中查找其他内容。这样你就不会有私人和公共场所有相同数据的重复字段。您请求公开数据,然后私人并将它们合并到客户端 – REJH

+0

感谢您的帮助:D – user2884707bond