简体   繁体   English

如何在mongodb / nosql中创建用户角色和权限

[英]How to create user roles and permission in mongodb / nosql

Ive been searching for a while trying to find tutorials on how to make a typical website MVC permission system using user roles. 我一直在寻找一些关于如何使用用户角色制作典型网站MVC权限系统的教程。

Ive done such things using SQL and relations, but in nosql databases, its done differently ( i assume). 我已经使用SQL和关系完成了这些事情,但在nosql数据库中,它的完成方式不同(我假设)。

what i have in mind is something like this 我的想法是这样的

//Role objects
{Roles : [
{
    '_id' : 'uniqueId',
    'role_name' : 'admin',
    'permissions_granted' : [array of permission strings]
},
{
    '_id' : 'uniqueId',
    'role_name' : 'user',
    'permissions_granted' : [array of permission strings]
},
{
    '_id' : 'uniqueId',
    'role_name' : 'guest',
    'permissions_granted' : [array of permission strings]
}
]}

//User objects
{Users: [
{
    '_id' : 'uniqueId',
    'username' : 'mike',
    'password' : 'mypass',
    'permissions' : [an instance of role group, or its id ? ]
},
{
    '_id' : 'uniqueId',
    'username' : 'jonny',
    'password' : '123',
    'permissions' : [???]
}
]}

how could i make a single query which fetches user data + its permissions ? 我如何制作一个获取用户数据及其权限的查询? is there a downside of using 2 querys, one to get user's role id, and then get permissions from roles document. 是否有使用2个查询的缺点,一个用于获取用户的角色ID,然后从角色文档获取权限。 ? how are user permissions handled in nosql databases like mongodb 如何在mongodb等nosql数据库中处理用户权限

You should use the IDs and not the objects as such. 您应该使用ID而不是对象。 This way you can update everything easily. 这样您就可以轻松更新所有内容。 If you have a scenario, when you read permissions often and write very seldom (which I assume) you can store additionally to the role ID's also the actual permissions of the user (based on its roles) 如果您有一个场景,当您经常读取权限并且很少写(我假设)时,您可以另外存储角色ID以及用户的实际权限(基于其角色)

However you need to update those permissions, every time a role is updated. 但是,每次更新角色时都需要更新这些权限。 But when querying a user you get the roles immediately with it, which is much faster than with 2 separate queries. 但是在查询用户时,您可以立即使用它获得角色,这比使用2个单独的查询要快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM