【问题标题】:How do I control user assets, but let admins see everything?如何控制用户资产,但让管理员查看所有内容?
【发布时间】:2009-03-04 07:52:11
【问题描述】:

我有一个 Rails 应用程序,其中用户拥有项目的成员资格(以及其他事物,多态)。用户也有角色。我希望 User#projects 像普通的 ActiveRecord 查找一样工作,但我也希望管理员能够访问每个项目。

我一直在这样做:

class User < ActiveRecord::Base
   has_many :memberships, :dependent => :destroy

   def projects
     if has_role?(:admin)
       Project.find(:all)
     else
       Project.find(:all, :include => :memberships, :conditions => ["memberships.user_id = ?", id])
     end
   end
end

class Project < ActiveRecord::Base
  has_many :memberships, :as => :member, :dependent => :destroy
end

class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :member, :polymorphic => :true
end

但我真的更愿意做这样的事情:

class User < ActiveRecord::Base
  has_many :memberships, :dependent => :destroy
  has_many :projects, :through => :memberships, :source => :member, :source_type => "Project"
end

这样我就可以更频繁地使用 named_scope(例如“alfred.projects.recent.active”)。

如果您自动为管理员添加新的会员资格,这很有效,但很快就会失控。

我想保留 User#projects 界面。这里的正确路线是什么?

非常感谢。

【问题讨论】:

  • 感谢所有伟大的建议!可能有另一种方法来解决这个问题 - 是否有一种直接的方法可以使 ActiveRecord 对象数组的行为类似于 AR 关联,以便 named_scope 并找到工作?即项目 = Project.find(:all); projects.active.recent - 再次感谢大家!

标签: ruby-on-rails activerecord


【解决方案1】:

Take a look at activefx's restfull authenticaion tutorial:(restful认证教程)是一个rails应用,有以下特点:

当前功能

- Login / Logout
- Restful, with the exception of the "activate" action
- Namespaced admin and user sections
- OpenID Authentication with support for incomplete OpenID    profiles
- Roles and permissions
- Administrative user controller
- Set roles, activate, enable / disable users  
- Login, permission, and access denied redirection system
- Member list and public profiles for logged in users
- Activation, with option to resend activation code
- Beta invitation system 
- easy on/off functionality, add/remove invites, send emails to   
- pending users
- Forgot Password / Reset Password
- Change Password 
- Failed login attempts database logging
- Recaptcha displayed for more than 5 failed logins
- Helper methods (link_to_user, if_admin?, etc.)

This thread will explain how you give owner and administrator access. #28.

祝你好运。

【讨论】:

  • 您的 github 链接返回失败章鱼:该页面不存在! github.com/activefx/restful_authentication_tutorial/tree/master 下划线不知怎的搞砸了
  • 嗯,你是对的:/我已经更新了链接。为什么我不能使用下划线?
  • 感谢 atmorell。 "has_" 是为单个资源抽象它的好方法,并且将有助于重构我正在使用的其他一些代码。我唯一担心的是它与我当前的 User#projects 方法在 current_user.projects.find(:all) 等方面相同。感谢您的回答!
【解决方案2】:

我个人不喜欢像您尝试那样在模型之间传播授权问题。

这有几个原因(主要原因是最终它通常会导致非 RESTful 设计)。另一个是矛盾方法的问题。

考虑一下。

user.projects 在您的系统中是什么意思?在你的模型中它有两个含义:它意味着

  1. 用户参与的所有项目,
  2. 所有存在的项目。

如果您以这种方式建模您的系统,您最终会得到一堆具有许多不同含义的方法,即。目的(想想你有三个、四个或更多角色的情况),这不是一个好的做法。不过,还有更多的原因。例如。如果某个用户拥有多个角色怎么办?管理员通常也是普通用户。

那么“正确”的解决方案是什么? 好吧,它们可能还有更多 pf,但我现在喜欢和使用的是 rails-authorization-plugin 使用的那个。在模型中定义角色并在控制器和视图中实现授权逻辑。查看plugin 了解更多信息。

希望对您有所帮助!如果没有,请发表评论。

【讨论】:

  • 感谢您的回复。我试图将所有权和授权分开,这是它们交叉的唯一实例。我同意它可能会扩展到其他方法,但我不确定它是否值得将它们完全混合。不过,我会尝试一下 rails-authorization-plugin 。谢谢!
【解决方案3】:

我会为RESTful_ACL插入一个不要脸的插件:http://github.com/mdarby/restful_acl/tree/master

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多