2012-03-05 12 views
0

isAdminというフィールドをユーザコントローラに追加しました。ユーザがadminでない場合はbefore_filter :authenticate_user!のページでフレンドリーなメッセージを選択します。ユーザーが管理者であれば、それはまた、チェックするよう最初の承認されていない状態としてのDeviseの使用の登録

どのように私は、フィルタの前に上書きしますか?

私はカンカンを使って上の例を見てきましたが、私は唯一の管理者ユーザーは私のアプリで何かをしたいが、私は新しい管理者ユーザーが管理者として敷石されていないことではなく、その後、別の管理者によって確認されたいです。

答えて

0

私は多分あなたは(宝石フォルダ内のファイルが上書きされるように)、あなたのRailsアプリのフォルダにルビーのファイルを作成する方法を上書きすると思う:

1.オリジナルauthenticate_user!私たちは私たち自身を定義することができ

# GEM_HOME/gems/cancan-2.0.x/lib/devise/controllers/helpers.rb 

1 module Devise 
2 module Controllers 
3  # Those helpers are convenience methods added to ApplicationController. 
4  module Helpers 
... #.... some code and comments 
42  def self.define_helpers(mapping) #:nodoc: 
43   mapping = mapping.name 
44 
45   class_eval <<-METHODS, __FILE__, __LINE__ + 1 
46   def authenticate_#{mapping}!(opts={}) 
47    opts[:scope] = :#{mapping} 
48    warden.authenticate!(opts) if !devise_controller? || opts.delete(:force) 
49   end 

2.so:

# RAILS_APP/lib/devise/controllers/helpers.rb 
1 module Devise 
2 module Controllers 
3  # Those helpers are convenience methods added to ApplicationController. 
4  module Helpers 
5  def authenticate_user!(opts={}) 
6   authenticate_if_the_user_is_admin # PUT your hook here. 
7   opts[:scope] = :#{mapping} 
8   warden.authenticate!(opts) if !devise_controller? || opts.delete(:force) 
9   # other code... 
10  end 
11 end 
12 end 
13end    

テストされていないメソッドが定義されています。 :-)

関連する問題