【问题标题】:Using CanCan to limit view on index page to user_id使用 CanCan 将索引页面上的视图限制为 user_id
【发布时间】:2012-09-17 16:39:08
【问题描述】:

我正在将 Devise 和 Cancan 用于 rails 3.2.6 应用程序。在应用程序中,我允许用户创建一个文档,其中包含一些收集在表单中的信息。然后我想允许用户在 localhost:3000/users/1/documents 的文档索引页面上仅列出他们的文档,这是有效的。什么不起作用,是我试图通过将 /users/:id/documents 替换为另一个数字来限制用户查看其他人的文档。

我正在使用cancan,并且都尝试过

可以 :index, Document, :user_id => user.id 可以 :read, Document, :user_id => user.id

然后在 Document 控制器索引方法上

if can? :read, Document
 @documents = @user.documents
else
 redirect_to root_path
end

也尝试使用:index...但这不起作用。我也在使用load_and_authorize_resource..

对我缺少什么有什么想法吗?

我会说,cancan 正在为我的用户管理和用户控制器工作,以便管理员创建、列出和编辑用户,所以我知道 cancan 正常工作。它还用于更新和删除用户文档。只是索引功能不起作用。

class Ability 

include CanCan::Ability 

  def initialize(user) 

    user ||= User.new # guest user (not logged in) 
    if user.id 
      if user.has_role? :user 
        can :create, Document 
        can :read, Document, :user_id => user.id 
        can :update, Document, :user_id => user.id 
      end 
    end 
  end 
end

【问题讨论】:

    标签: ruby-on-rails list devise indexing cancan


    【解决方案1】:

    您必须确保未登录的用户以及user.id 与文档的user_id 不同的用户(文档所有者)没有读取所有文档的权限。

    class Ability
      include CanCan::Ability
    
      def initialize(account)
    
        user ||= User.new  #non-logged-in user
    
        # logged in users
        if user.id and user.has_role?(:user)
    
          #content owners
          can :manage, Document, :user_id => user.id
    
          #other logged-in users
          can [:show, :create], Document
    
        end
    
      end
    end
    

    小心你没有像can :read, :allcan :read, Document 这样的行,如果你说cancan 已经在工作,你很可能是在某处给予许可。

    【讨论】:

    • 我将if can? :read, Documents @documents = @user.documents else redirect_to root_url end 添加到文档控制器,而我的ability.rb 有class Ability include CanCan::Ability def initialize(user) user ||= User.new # guest user (not logged in) if user.id if user.has_role? :user can :create, Document can :read, Document, :user_id => user.id can :update, Document, :user_id => user.id end end end end ,但它仍然无法正常工作......我错过了什么?
    • sorry.new to post here...我的格式需要工作 :) 无论如何,CanCan 正在工作,我无法编辑或更新我不拥有的文档...只是不工作,我不希望用户能够列出任何不属于他们的文档。
    • 使用您的代码编辑您的原始评论,以便我们更好地了解问题所在。或者,您实际上可以只控制视图中的逻辑,但我认为这不是您想要的。
    • 不..我希望在视图之外处理
    【解决方案2】:

    在你的情况下,你应该写在你的能力课上

    def initialize(user)
      can :manage, Document do |document|
        document.user == user
      end  
    end
    

    这将检查文档是否属于登录用户。如果是 can 将返回 true,否则返回 false。

    有关如何使用块处理复杂授权的更多详细信息,

    https://github.com/ryanb/cancan/wiki/Defining-Abilities-with-Blocks

    【讨论】:

    • 进行了更改,然后if can? :manage, Document @documents = @user.documents else redirect_to root_url end 同样的问题..我仍然可以以用户 ID 1 登录并列出用户 ID 2 的文档
    • 你更新了技能类的代码了吗?如果没有,请先更新那个,并将 load_and_authorize_resource 添加到您的控制器。访问任何不属于用户的文档应该通过异常
    • 在您的控制器代码中,而不是检查文档模型上的用户凭据 {if can? :manage, Document},总是错误的,因为你没有在能力中定义它;你应该直接写@documents = @user.documents。试试 @documents = Document.all;应该通过异常
    猜你喜欢
    • 2013-12-27
    • 1970-01-01
    • 2017-01-19
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多