2016-07-08 16 views
1

私のアプリケーションにはいくつかの異なるユーザータイプがあります。私はコントローラにアクセスするときにこのようなことをしようとしていました。"or"ステートメントを使用したbefore_action

class Admins::ProgrammesController < ApplicationController 
    # only allow entry if the user is one of these types. 
    before_action :authenticate_admin! || :authenticate_super_admin! || :authenticate_programme_leader! || :authenticate_caseworker! 

    def index 
    end 

ユーザーの種類を確認するには、このコントローラにアクセスできます。

ああ、それは正しいとは思われません。 Railsは最初のbefore_action引数に注意を払うだけです - ユーザがadminかどうかを確認してください。したがって、ユーザーがケースワーカーである場合、私の仕様は合格しません。

これをどのようにして同様の方法で達成できますか?要求され、より多くのコードとして

UPDATE

..

これは私の失敗の仕様です。

RSpec.describe Admins::ProgrammesController, :type => :controller do 
    context 'authenticated user' do 
    before :each do 
     @user = FactoryGirl.create :caseworker 
     sign_in @user 
    end 

    describe 'GET index' do 
     it 'responds with a 200 OK status code' do 
     get :index 
     expect(response).to have_http_status(:success) 
     end 
    end 
    end 
end 

私は、Caseworkタイプのユーザーが作成されていることを100%確信しています。ここに工場があります。

FactoryGirl.define do 
    factory :caseworker, class: 'Users::Caseworker' do 
    email { "[email protected]" } 
    password { "password" } 
    end 
end 

ここにルートファイルの関連する行があります。

Rails.application.routes.draw do 
    devise_for :users, controllers: {sessions: 'users/sessions'}, skip: :registrations 
    devise_for :admins, class_name: 'Users::Admin' 
    devise_for :caseworker, class_name: 'Users::Caseworker' 
    devise_for :cardholder, class_name: 'Users::Cardholder' 
    devise_for :programme_leader, class_name: 'Users::ProgrammeLeader' 
    devise_for :super_admin, class_name: 'Users::SuperAdmin' 

    scope module: 'admins' do 
    # this is the route in question... 
    resources :programmes, only: [:index, :show, :new] 
+0

admin、super_adminなどの別々のスコープがありますか(つまり、authenticate_admin!メソッドdeviseはあなたに無料で与えるか、自分で書いたものですか?後者の場合、それは何ですか?) –

+0

それは、私は自分自身を書いた。基本的にSTIです。作成できるユーザーにはいくつかの種類があります。ユーザーはそれぞれの親クラスです。今は何も特別なことをしていない、彼らはちょうど異なるタイプのユーザーです。 – mikelovelyuk

+0

認証方法を表示できますか? –

答えて

0

## application_controller.rb 

def authenticate! 
    if [ current_admin, current_super_admin, current_programme_leader,current_caseworker ].all? {|e| e.nil?} 
    :authenticate_admin! ### We can only redirect to one sign in page if there is no user logged in 
    end 
end 

でカスタムメソッドを定義し、(のための場合

authenticate_admin! || authenticate_super_admin! || ... 

よう

before_action :authenticate! 
+0

したがって、 'super_admin'と' admin'だけがアクセスを許可されたとき、次のコントローラではどうなりますか? - 私はこれを使用することができません – mikelovelyuk

+0

'定義?その名前のメソッドがあるので(たとえそのメソッドがnilを返しても) –

+0

それを微調整してください。引数として許可されているユーザーモデルをauthenticateメソッドに渡すことができます。 –

0

何かが原因で動作しませんあなたのコントローラでは、このメソッドを使用します例)管理者が署名されていないi n authenticate_admin!はリダイレクトをトリガーします。次に何が起こるかは関係ありませんが、リダイレクトはまだ発生します。

あなたは一例

def check_authenticated? 
    if current_admin || current_super_admin ... 
    #let them in 
    else 
    #redirect them 
    end 
end 

のために、簡単に十分なフィルタを書くことができます。しかし、あなたが得られます。このコントローラ内で

class Admins::ProgrammesController < ApplicationController 
    devise_group :programme_editor, contains: %i(admin super_admin case_worker) 
    before_action :authenticate_programme_editor! 
end 

ような何かを行うことができますdevise groupsの概念も存在します通常のヘルパー:current_programme_editorprogramme_editor_signed_in?指定された順序で、指定されたものの存在を確認/確認します。

current_programme_editorsも、最初のものではなくすべてのユーザーなどです。

+0

もっとコードを提供できますか?あなたはRailsの初心者を扱っています。私は文法をよく守っていない – mikelovelyuk

+0

二番目のケースでbefore_actionを忘れている以外は何を理解していないか –

+0

これはきれいです –

関連する問題