Copy email to subdomain in rail development

I use an authentication program in a rails application, and my application can have many subdomains. It currently uses email as authentication, and email must be unique to the entire application.

Now can I indicate the uniqueness of the email address on the subdomain, and not on the entire application? I tried:

validates_uniqueness_of :email, :scope => :account_id 

But it didn’t work out. He is still looking for email uniqueness for the entire application, rather than a specific subdomain when registering a new user.

Any help would be greatly appreciated.

+6
source share
2 answers

Ok, I did it.
Edited config.authentication_keys in devise.rb as

 config.authentication_keys = [ :login, :account_id ] 

I also created a hidden field to include account_id in the login form

 <%= f.hidden_field :account_id, :value => @account.id %> 

Here, @account holds the account associated with the current subdomain.

And added the following protected method to user.rb to override the find_for_database_authentication class method

 protected def self.find_for_database_authentication(warden_conditions) conditions = warden_conditions.dup login = conditions.delete(:login) account_id = conditions.delete(:account_id) where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).where("account_id = ?", account_id).first end 

If there is a better solution, then feel free to comment on the guys ..
Hurrah!

+7
source

How do you save your users, do you set up an account on the user model before trying to save it? If not, then you will see the identifier_space nil - therefore, it will look like a scope.

0
source

Source: https://habr.com/ru/post/890334/


All Articles