繁体   English   中英

允许cancan Ability.rb仅管理模型的特定字段的访问

[英]Allowing cancan Ability.rb manage access only specific fields of a model

我正在尝试添加一个按钮以将回复标记为在Rails中已读。 我目前有这样的事情。

# /app/models/ability.rb
...
can :manage, Reply, :user_id => user.id
...

我的load_and_authorize_resource中也有load_and_authorize_resource

# /app/controllers/replies_controller.rb
class RepliesController < ApplicationController
  load_and_authorize_resource

  def update 
    @reply = Reply.find(params[:id])
    @reply.isRead = true
    if @reply.save
      flash[:notice] = "Marked as ready."
      flash[:alert] = params[:id]
      redirect_to root_path
    else
      render :action => 'new'
    end
  end

我有一个按钮,用户可以将回复标记为已读。

  = button_to "Mark as read", idea_reply_path(reply.idea,reply), :method => "put"

问题是,由于我要尝试根据能力.rb(上)中定义的其他user.id所有者更新对象,因此我没有权限对其进行编辑。

如果我添加这样的内容,它将起作用,但是我也将管理整个答复对象的权限授予其他人。

can :manage, Reply, :to_user_id => user.id

我需要一种只允许用户管理isRead?属性的isRead? 他的user.id与to_user_id相匹配的to_user_id

您可以在控制器中为mark_as_read定义一个新动作

 def mark_as_read
  #action to mark as read  
 end

并在能力中定义

can :manage, :Reply, :user_id => user.id
can :mark_as_read, :to_user_id => user.id

订购非常重要。 现在,已登录的用户可以管理答复,并且作为该用户的用户将仅具有mark_as_read的能力。

我想你们可以两者兼得

can :manage, Reply, :user_id => user.id
can :update, Reply, :to_user_id => user.id

如果更新操作仅用于将标记回复标记为已读,那么这就是您想要的

暂无
暂无

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

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