2012-03-05 41 views
0

我已經添加到我的用戶控制器的一個字段叫isAdmin,我希望它,所以選擇一個友好的消息,如果用戶不是管理員有before_filter :authenticate_user!註冊設計使用作爲初始未批准的狀態

如何覆蓋之前的過濾器,以便它也檢查用戶是否是管理員?

我見過使用康康的例子,但我只想讓管理員用戶在我的應用程序中做任何事情,但我希望新的管理員用戶不會被設爲管理員,但會被另一個管理員確認。

回答

0

我想也許你要覆蓋在你的Rails應用程序文件夾中創建一個Ruby文件的方法(以便在寶石文件夾中的文件將被覆蓋):

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    

未經測試。 :-)