2010-11-26 8 views
0

레일 보안 주제에 관심이 있으며 Security on Rails을 사용하고 있습니다. RBAC 구현/142 페이지에 있습니다. 그리고 주제의 오류를 지나칠 수 없습니다. 다음 예외기대하지 않았을 때 객체가 없습니다.

module RoleBasedControllerAuthorization 

    def self.included(base) 
    base.extend(AuthorizationClassMethods) 
    end 

    def authorization_filter 
    user = User.find(:first, 
     :conditions => ["id = ?", session[:user_id]]) 

    action_name = request.parameters[:action].to_sym 
    action_roles = self.class.access_list[action_name] 

    if action_roles.nil? 
     logger.error "You must provide a roles declaration\ 
     or add skip_before_filter :authorization_filter to\ 
     the beginning of #{self}." 
     redirect_to :controller => 'root', :action => 'index' 
     return false 
    elsif action_roles.include? user.role.name.to_sym 
     return true 
    else 
     logger.info "#{user.user_name} (role: #{user.role.name}) attempted to access\ 
     #{self.class}##{action_name} without the proper permissions." 
     flash[:notice] = "Not authorized!" 
     redirect_to :controller => 'root', :action => 'index' 
     return false 
    end 
    end  
end  

module AuthorizationClassMethods 
    def self.extended(base) 
    class << base 
     @access_list = {} 
     attr_reader :access_list 
    end 
    end 

    def roles(*roles) 
    @roles = roles 
    end 

    def method_added(method) 
    logger.debug "#{caller[0].inspect}" 
    logger.debug "#{method.inspect}" 
    @access_list[method] = @roles 
    end 
end 

그리고 @access_list [방법] = @roles 광고 송구 : I가 레일 3.0.3 및 1.9.2 루비 사용하고

ActionController::RoutingError (You have a nil object when you didn't expect it! 
You might have expected an instance of ActiveRecord::Base. 
The error occurred while evaluating nil.[]=): 
    app/security/role_based_controller_authorization.rb:66:in `method_added' 
    app/controllers/application_controller.rb:5:in `<class:ApplicationController>' 
    app/controllers/application_controller.rb:1:in `<top (required)>' 
    app/controllers/home_controller.rb:1:in `<top (required)>' 

여기서 코드이다. 데이터베이스에 세션을 저장하고 있습니다. 마침내 모든 조언에 감사드립니다.

+0

로 캉캉을 확인 할 수 있습니다. 최신 버전에서는 두 가지 모두에서 매우 중요한 변화가있었습니다. 그 콤보를 실행하고 여전히 문제가 있는지 확인하려고합니다. RVM을 사용한다면 레일과 루비 버전을 정말 빨리 전환 할 수 있습니다. – rwilliams

+0

확인. 나는 RVM을 사용하고있다. 하지만 Rails 3과 Ruby 1.9.2에서 필요합니다. 도와주세요? – Zeck

+1

예외 백 트레이스를 추가 할 때 알 수 없기 때문에 예외 행이 무엇인지에 대해 적어도 일부 행 번호를 표시해보십시오. – Nakilon

답변

0

@access_list에 액세스 할 수없는 것 같습니다. method_added입니다. 나는

class << base 
    attr_accessor :access_list 
    @access_list = {} 
end 

이 특정 문제를 해결하지 않을 수 있습니다 시도 할 것이지만, 그렇지 않으면 당신은 당신의 ACCESS_LIST 속성이 읽기 전용 인 경우 @access_list[method] = @roles를 호출 할 수 없습니다.

+0

'@access_list'는 접근자를 필요로하지 않으므로 일반적으로 인스턴스 내부에 있습니다. 'attr_accessor'는 인스턴스 변수 밖의 코드가 개발자가 명시 적으로 쓸 필요없이 인스턴스 변수의 값으로 얻을 수있는 getter/setter 메소드를 생성합니다. –

0
나는이 문제가 있다면 모르겠지만,이 의심스러운

:

class << base 
    @access_list = {} 
    attr_reader :access_list 
end 

@access_list@@access_list를 클래스 변수가 있어야하지 않나요?

0

@access_list을 클래스의 인스턴스 변수로 정의하지만 instance_variable이 이고 인스턴스가 인 클래스에 액세스 할 수 있습니다. 다음은 아마 작동합니다 : 당신이 Auhorization 필요한 경우

module AuthorizationClassMethods 
    def access_list 
    @access_list ||={} 
    end 

    def method_added(method) 
    logger.debug "#{caller[0].inspect}" 
    logger.debug "#{method.inspect}" 
    access_list[method] = @roles 
    end 
end 

당신이 그 책은 아마도 레일 2.3과 루비 1.8를 위해 작성되었습니다 라이언 베이츠

https://github.com/ryanb/cancan

관련 문제