2012-03-05 2 views
0

isAdmin 필드를 내 사용자 컨트롤러에 추가 했으므로 사용자가 admin이 아닌 경우 before_filter :authenticate_user! 인 모든 페이지에서 친숙한 메시지를 선택해야합니다.초기 승인되지 않은 상태로 Devise를 등록하는 중

사용자가 관리자인지 확인하기 위해 이전 필터를 어떻게 대체합니까?

저는 cancan 사용에 대한 예를 보았습니다.하지만 관리자 만이 내 앱에서 어떤 작업을 수행하기를 원합니다.하지만 새 관리자를 admin으로 설정하지 않고 다른 관리자가 확인하도록하고 싶습니다.

답변

0

나는 어쩌면 당신이 (보석 폴더에있는 파일을 덮어 쓸 수 있도록) 레일즈 응용 프로그램 폴더에 루비 파일을 생성하여 방법을 덮어 줄 생각

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    

테스트하지. :-)

관련 문제