ruby-on-rails - 使用 CanCan 在 Active Admin 中不会拒绝用户访问

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

我正在使用 Active Admin 的 CanCan 授权适配器和 Rolify 来管理管理站点上的授权。我有一个模型,company ,那 has_many :manuals ,和另一个模型,manuals ,那 has_many :parts .

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

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

config/routes.rb

ActiveAdmin.routes(self)

模型/能力.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

我还覆盖了 中的默认授权方法 Controller /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

最佳答案

您是否添加了方法load_and_authorize_resource到您的 Controller ?

像这样:

class SomeController < ApplicationController
  load_and_authorize_resource
  ...
end

Check Abilities & Authorization

关于ruby-on-rails - 使用 CanCan 在 Active Admin 中不会拒绝用户访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18963036/

相关文章:

ruby-on-rails - 如何在 rails 中禁用 select_tag 提示?

ruby - 当销毁失败时,我可以在事件管理员中有一个闪现消息吗?

ruby - Heroku FreeTDS,如何将 Rasil SQL Server 与带有 Multi Buildpacks 的 TinyTDS 一起使用

ruby-on-rails - Goji,RoRails或Grails for “Social Network”

ruby-on-rails - ImageMagick - "CORE_RL_magick_.dll not found"或如何使用 ruby​​ 1.9.2 在 Windows 上安装 RMagick

ruby-on-rails - 如何在 Ubuntu 中连接 PostgreSQL

css - Rails 4 x Bootstrap 3 : how to apply custom style to select field

mysql - 具有连接表关系的事件管理

ruby-on-rails - 活跃的管理员过滤器

jquery - 类似于 Facebook 的推送式通知,带有 Rails 和 jQuery