繁体   English   中英

不能阻止访问

[英]CanCan not preventing access when it should

当我以其他用户身份登录时访问users/1/edit不会引发AccessDenied错误,我也不知道为什么:

  authorize_resource only: [:edit, :update]
  def edit
    @user = User.find(params[:id])
  end
  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      redirect_to @user
    else
      render 'edit'
    end
  end

能力等级:

class Ability
  include CanCan::Ability
  def initialize(user)
    user ||= User.new

    can :read, :all
    can :create, User
    can :create, Group

    can :update, User, id: user.id
  end
end

如果我将authorize_resource更改为load_and_authorize_resource则它将按预期工作。 但这肯定不相关吗?

您的代码仅授权用户访问编辑和更新操作,而不授权@user对象

您必须像这样手动授权对象

尝试这个,

def edit
  @user = User.find(params[:id])
  authorize! :update, @user
end

def update
  @user = User.find(params[:id])
  authorize! :update, @user
  if @user.update_attributes(params[:user])
   redirect_to @user
  else
   render 'edit'
  end
end

我面临着和您一样的问题,但对我来说,我正在使用康康设计。 因此,在我的控制器中,我将

 before_filter :authenticate_user!, :except=>[:create]

它将验证除创建以外的用户。

def index
    @user = User.all
    authorize! :index, @user
    respond_to do |format|
       format.html # index.html.erb
       format.xml  { render :xml => @user }
    end
end

您想要授权用户访问权限的每个控制器功能,您都可以这样做,看来您需要做很多工作,只需将每一个控制器放入需要授权的功能中,而不是仅使用load_and_authorize_resource,但是希望可以从我已经完成的工作中得到一点帮助。 这是资源:https://github.com/ryanb/cancan/wiki/authorizing-controller-actions。 如果您得到答案以及为什么load_and_authorize_resource无法正常工作,也可以在此处发布:)

我还没有答案(到目前为止)为什么会发生这种情况,但是我遇到了基本上相同的问题。 我的情况与众不同之处仅在于手动授权每个操作(而不是依赖“授权资源”或“ load_and_authorize”是关键)。

我也遇到了这个问题,这就是我所发现的。

如果我:update动作中正确读取源代码 ,则load_and_authorize执行find_by加载资源,然后调用authorize! 在上面。 但是,在应用了传入参数之后,我看不到它在哪里对其进行授权。 (如果我读错了,请纠正我。)

我看到的用例是某人编辑资源,然后在编辑中更新资源中的值,使其不再有资格在保存时通过授权。 (当然,我正在设置UI来帮助避免这种情况,但是显然我仍然想保护资源。)运行功能测试,我能够设置我希望不通过控制器授权的属性:update操作,大概是因为检查是在解析属性之前进行的。

到目前为止,我解决该问题的方法是调用authorize! 设置属性后再次出现,这意味着我不能使用update_attributes因为我要在保存之前进行授权:

class FooController < ApplicationControlller

  load_and_authorize_resource

  def update

    # slurp the mass assignable params
    @foo.attributes = params[:foo]

    # set some other params
    @foo.some_other_attr = 'bar'

    # authorize again, now that we have updated the params
    authorize! :update, @foo

    if @foo.save!
      flash[:notice] = I18n.t(...)   
      respond_with(@foo)
    # ...
    end 
  end 
end

一种替代方法是创建一个before_filter,自己加载@foo实例,然后按上述方式调用authorize,但这实际上并不能使事情变得更加整洁,恕我直言。 这将节省一个授权! 呼叫。

我对其他人如何处理此事感到好奇。 我是CanCan的新手,所以我想我做错了什么。 :)

暂无
暂无

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

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