2011-03-02 64 views
7

我使用設計來管理我的Rails應用程序中的用戶身份驗證。設計對此非常好。設計白名單

但是,我對我的應用程序有一個特殊的要求:用戶必須被列入白名單才能註冊爲用戶。

所以有一個管理員創建一個允許的電子郵件列表。用戶註冊一封電子郵件,如果電子郵件在白名單表中,他將被註冊。但是,如果郵件不在白名單中,則應通過諸如「您尚未被邀請」之類的消息中止註冊。

你有什麼想法可以用設計來解決嗎?

在此先感謝。

回答

15

我只是使用模型驗證。我假設你的用戶類具有色器件方法

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

我沒有創建自己的控制器的建議:

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 

我把它放在app/users/registrations_controller.rb。然後,我必須將設計註冊視圖複製到app/views/users/registrations中,因爲未使用默認視圖。

它現在的工作,感謝您的幫助