Question

I want to restrict user to a particular subdomain, for different subdomain he needs to signup again. so every user has an email and subdomain. I put the unique index on [:email, :subdomain]

User Model

class User < ActiveRecord::Base
  # TODO: :confirmable has been disabled for now.
  devise :database_authenticatable, :validatable, :omniauthable, :registerable, :recoverable, request_keys: [ :subdomain ]

end

For Sign in, I override "find_for_database_authetication" method by this in my user model.

def self.find_for_database_authentication(warden_conditions)
    where(:email => warden_conditions[:email], :subdomain => warden_conditions[:subdomain]).first
end

It works fine.

But for sign up, to check if user already exists in database with same subdomain, I am not able to find which method should be overrided .

Is there any way to do this?

Was it helpful?

Solution

When you add the validatable module of devise in your model, automatically devise adds the follow validations:

 devise/lib/devise/models/validatable.rb

# All validations used by this module.
  VALIDATIONS = [ :validates_presence_of, :validates_uniqueness_of, :validates_format_of,
                  :validates_confirmation_of, :validates_length_of ].freeze


  def self.included(base)
    base.extend ClassMethods
    assert_validations_api!(base)

    base.class_eval do
      validates_presence_of   :email, if: :email_required?
      validates_uniqueness_of :email, allow_blank: true, if: :email_changed?
      validates_format_of     :email, with: email_regexp, allow_blank: true, if: :email_changed?

      validates_presence_of     :password, if: :password_required?
      validates_confirmation_of :password, if: :password_required?
      validates_length_of       :password, within: password_length, allow_blank: true
    end
  end

So, you could remove the validatable module and make your owns validations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top