基于用户<>项目的角色

发布于 2024-09-19 04:38:39 字数 446 浏览 6 评论 0 原文

目前我正在使用 Devise & CanCan 允许我创建 具有使用 (Roles_Users) 表的角色的用户。

这很好,但我想要的是在我的应用程序中拥有项目 用户的每个项目可能具有类似的角色(管理员、查看者、 等)IE,角色不是分配给用户,而是根据用户所属的项目分配给用户。

示例:

  • 用户 X 属于项目 A,具有管理员角色
  • 用户 X 属于项目 B,具有访客角色
  • 用户 Y 属于项目 B,具有观察员角色

什么样的模型适用于此?

型号

用户 has_many:项目 项目 ? 角色 ?

Users_Roles_Projects(user_id、project_id、role_id)

你觉得怎么样?我是新手,可以利用理解和 你们这些经验丰富的人的想法。谢谢!

Currently I'm using Devise & CanCan which allows me to create
Users with Roles using a (Roles_Users) table.

That's nice, but what I want is to Have Projects in my app and for
each project for a user to possibly have a role like (Admin, Viewer,
etc) IE, roles are not assigned to users but to users based on what projects they are a member of.

Examples:

  • User X belong to Project A with an Admin Role
  • User X belong to Project B with an Guest Role
  • User Y belong to Project B with an Observer Role

What kind of Model would work for this?

Models

Users
has_many: projects
Projects
?
Roles
?

Users_Roles_Projects (user_id, project_id, role_id)

What do you think? I'm a newbie and could use the understanding and
thinking from you fine experienced folks. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就是爱搞怪 2024-09-26 04:38:39

您应该查看 has_many :through。此 Railscast 应该可以帮助您启动并运行:http://railscasts.com/episodes /47-two-many-to-many

例如,您可以通过会员资格拥有 User has_many 项目(我相信您可以想出一个更好的名称!)

您的 Users 模型将包含标准用户详细信息,项目模型将包含项目详细信息,并且可能您在某处有一些角色模型(我没有使用您提到的任何一个库,因此我无法评论它们的工作方式)。关键是会员模式。

成员资格模型将包含用户 ID、项目 ID 和角色 ID。在数据库中,任何给定的用户 ID 和项目 ID 配对都应该只有一个实例,因此通过将角色 ID 与该配对一起存储,您可以将角色分配给指定项目上的该用户。

You should have a look in to has_many :through. This Railscast should get you up and running: http://railscasts.com/episodes/47-two-many-to-many

For example, you could have User has_many Projects through Memberships (I'm sure you can come up with a better name!)

Your Users model would contain the standard user details, the Projects model would contain the project details and presumably you have some Roles model somewhere (I've not used either of the libraries you mentioned so I can't comment in terms of how they work). The key is the Memberships model.

The membership model would contain the userID, projectID and a roleID. In the database there should only be one instance of any given userID and projectID pairing so by storing the roleID along side this pairing you can assign the role to that user on the specified project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文