2010-11-26 10 views
0

私はRailsセキュリティのトピックに興味があり、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ライン投げ:私はRailsの3.0.3とRuby 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としてアクセスすると、クラスのインスタンスになります。次は、おそらく動作するはずです:

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 

あなたがAuhorizationが必要な場合は、カンカンをチェックアウトする場合がありますライアンベイツによって

本はおそらく、Railsの2.3とRuby 1.8のために書かれ

https://github.com/ryanb/cancan

関連する問題