2013-09-23 2 views
4

저는 관리자 계정에서 권한을 관리하기 위해 Active Admin의 CanCan 인증 어댑터와 Rolify를 사용하고 있습니다. 모델이 company이고, 그 모델이 has_many :manuals이고, 다른 모델이 manuals인데, 그 모델은 has_many :parts입니다.CanCan을 사용하는 Active Admin에서 사용자의 액세스가 거부되지 않습니다.

사용자가 admin/manuals/1을 읽을 수있는 권한이없고 주소 표시 줄에 입력하면 제대로 리디렉션되고 인증되지 않은 메시지가 표시됩니다. 그러나 사용자가 admin/manuals/1/parts을 입력하면 액세스가 거부됩니다. 모든 부분이 숨겨진 부분을 제외하고는 해당 페이지로 이동합니다. 승인되지 않은 메시지로 대시 보드로 리디렉션되어야합니다.

다음은 나의 구성입니다. 제공 할 수있는 조언에 대해 미리 감사드립니다.

설정/routes.rb

ActiveAdmin.routes(self) 

모델/

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 

ability.rb 또한 컨트롤러/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 
+0

이봐 당신이 이제까지이 알아낼 않았다? 나는'protected method authorize! '에러를 얻고있다. –

+0

나는 그렇지 않았다. 나는 너무 많은 중첩 된 루트로 끝내었고, 단지 내 자신의 관리자를 만드는 것이 더 쉬워졌습니다. – seancdavis

답변

1

load_and_authorize_resource 메서드를 컨트롤러에 추가 했습니까? 이처럼

:

class SomeController < ApplicationController 
    load_and_authorize_resource 
    ... 
end 

Check Abilities & Authorization

+2

'controller.load_and_authorize_resource' 또는'controller.authorize_resource'를'app/admin/parts.rb'에 추가하면'protected method authorize! '오류가 발생합니다. – seancdavis