2011-03-02 9 views
7

私は私のレールアプリでユーザー認証を管理するためにdeviseを使用しています。 Deviseは本当に素晴らしいです。devitのホワイトリスト

ただし、私は自分のアプリケーションに特別な要件があります。ユーザーは、ユーザーとして登録する前にホワイトリストに登録する必要があります。

したがって、許可された電子メールのリストを作成する管理者がいます。ユーザーは電子メールで登録し、電子メールがホワイトリストテーブルに登録されている場合、登録されます。ただし、メールがホワイトリストにない場合は、「あなたはまだ招待されていません」のようなメッセージで登録を中止する必要があります。

あなたはそれがどのように考案で解決できるか考えていますか?

ありがとうございます。

class MyRegistrationController < Devise::RegistrationsController 
    def create 
    # do your checks 
    super 
    end 
end 

項目:https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb そして:https://github.com/plataformatec/devise/wiki/How-to:-Customize-routes-to-user-registration-pages

幸運あなたは何ができるか

答えて

6

は、独自の登録コントローラを作成し、同様にデバイス1を延長です!

15

私はモデル検証を使用します。私はapp/users/registrations_controller.rbでそれを置い

class Users::RegistrationsController < Devise::RegistrationsController 
    def create 
     email = params[:user][:email] 
     if Admin::Whitelist.find_by_email(email) != nil 
      super 
     else 
      build_resource 

      set_flash_message :error, "You are not permitted to sign up yet. If you have already payed your registration fee, try again later." 
      render_with_scope :new 
     end 
    end 
end 

:私は私が示唆したように、私自身のコントローラを作成していた自分のUserクラスが工夫メソッドを持ってい

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable #etc 

    before_validation :whitelisted 

    def whitelisted 
    unless celebrityemail.include? email 
     errors.add :email, "#{email} is not on our invitation list" 
    end 
    end 

end 
+0

ためのおかげで作業していますか? – Magne

+0

@Magne 'errors.add:email"は私たちの招待状リストに載っていません:#{email} "' –

2

を想定しています。デフォルトビューが使用されなかったので、私はapp/views/users/registrationsにdevise登録ビューをコピーしなければなりませんでした。

それはあなたがエラーMSGでの実際の電子メールを表示したい場合は、どのようにこのコードを変更するであろう、今あなたの助け

関連する問題