2012-03-05 44 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    

未经测试。 :-)