【问题标题】:User not being denied access in Active Admin using CanCan使用 CanCan 在 Active Admin 中未拒绝用户访问
【发布时间】:2013-09-23 15:22:56
【问题描述】:

我正在使用 Active Admin 的 CanCan 授权适配器以及 Rolify 来管理管理站点上的授权。我有一个模型,companyhas_many :manuals,还有另一个模型,manualshas_many :parts

如果用户无权阅读admin/manuals/1 并将其键入地址栏中,他们将被正确重定向并显示未经授权的消息。但是,如果用户输入admin/manuals/1/parts,他们不会被拒绝访问。他们被带到那个页面,除了所有部分都对他们隐藏。他们应该被重定向到带有未经授权的消息的仪表板。

这是我的配置。提前感谢您提供的任何建议。

config/routes.rb

ActiveAdmin.routes(self)

models/ability.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new

    can :read, ActiveAdmin::Page, :name => "Dashboard"

    if user.has_role? :admin
      can :manage, :all
    elsif user.has_role? :moderator
      can :manage, Part, :manual => { :company_id => user.company_id }
    else
      can :read, Part, :manual => { :company_id => user.company_id }
    end
  end
end

我还覆盖了 controllers/application_controller.rb

中的默认授权方法
rescue_from CanCan::AccessDenied do |exception|
  redirect_to root_url, :alert => exception.message
end

def authenticate_admin_user!
  authenticate_user!
  unless user_signed_in?
    flash[:alert] = "You are not authorized to view this page"
    redirect_to root_path
  end
end

def current_admin_user #use predefined method name
  return nil unless user_signed_in?
  current_user
end

def after_sign_in_path_for(user)
  if current_user.has_role? :admin
    admin_dashboard_path
  elsif current_user.has_role? :moderator
    admin_manuals_path
  else
    company_path(user.company)
  end
end

【问题讨论】:

  • 嘿,你有没有想过这个问题?我正处于收到protected method authorize! 错误的地步。
  • 我没有。我最终得到了如此多的深度嵌套路由,以至于构建自己的管理员变得更加容易。

标签: ruby-on-rails ruby-on-rails-4 activeadmin cancan rolify


【解决方案1】:

您是否将方法 load_and_authorize_resource 添加到您的控制器中?

像这样:

class SomeController < ApplicationController
  load_and_authorize_resource
  ...
end

Check Abilities & Authorization

【讨论】:

  • app/admin/parts.rb 中添加controller.load_and_authorize_resourcecontroller.authorize_resource 会给我一个protected method authorize! 错误。
猜你喜欢
  • 2011-10-09
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
  • 2012-05-19
  • 1970-01-01
  • 2020-03-02
相关资源
最近更新 更多