Question

I'm using Clearance for authentication and acts_as_tenant to set tenant

User.rb

Clearance::User::Validations.module_eval do

  included do

    email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

    validates :email, uniqueness: { scope: :company_id, case_sensitive: false }, :format => {:with => email_regex}
    validates_presence_of :password, :unless => :password_optional?

  end

end

class User < ActiveRecord::Base

  acts_as_tenant(:company)

  include Clearance::User

  attr_accessible :email, :fname, :lname, :password, :password_confirmation, :user_type_id, :company_id
  attr_accessor :password_confirmation

   #defining the association
  belongs_to :user_type
  belongs_to :company

  VALID_CHAR_REGEX =  /^[a-zA-Z][\sa-zA-Z]*$/
  VALID_PASSWORD_REGEX =/^(?=.*[a-zA-Z])(?=.*[0-9]).{7,}$/

  validates :password, :presence => true, :on => :update
  validates :password, format: { with: VALID_PASSWORD_REGEX, :message => "must include one number, one letter and more than 6 characters" }, :allow_blank => true
  validates_confirmation_of :password
  validates :password_confirmation, :presence => true
  validates :fname, :presence => true
  validates :fname, format: { with: VALID_CHAR_REGEX }, :allow_blank => true
  validates :lname, :presence => true
  validates :lname, format: { with: VALID_CHAR_REGEX }, :allow_blank => true
end

application_controller.rb

class ApplicationController < ActionController::Base

  include Clearance::Authentication

  #calling acts_as_tenant method to set current tenant
  set_current_tenant_by_subdomain(:company, :subdomain)

  protect_from_forgery
end

company.rb

class Company < ActiveRecord::Base
  attr_accessible :company_description, :company_name, :is_deleted, :subdomain, :logo, :users_attributes

  has_many :investors, :dependent => :nullify
  has_many :users, :dependent => :nullify
  has_many :series, :dependent => :delete_all
  has_many :dividends, :dependent => :delete_all

  has_attached_file :logo,
                :styles => { :thumb => "150x>" }

  has_many :series, :dependent => :delete_all
  has_many :transactions


  validates :company_name, :presence => true
  validates :company_name, :uniqueness => true 
  validates :company_description, :presence => true
  validates_attachment :logo, :presence => true, :content_type => { :content_type => ["image/jpg","image/jpeg","image/png"] },
                   :size => { :in => 0..5.megabytes }


  accepts_nested_attributes_for :users, :allow_destroy => true
end

log

Started POST "/sessions" for 127.0.0.1 at 2013-10-10 11:22:28 +0530
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "session"=>{"email"=>"test@test.test", "password"=>"[FILTERED]"}, "commit"=>"Sign in", "method"=>"post"}
Company Load (0.1ms)  SELECT `companies`.* FROM `companies` WHERE `companies`.`subdomain` IS NULL LIMIT 1
User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`company_id` = 1 AND (email ='test@test.test') LIMIT 1
User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`company_id` = 1 AND `users`.`email` = 'test@test.test' LIMIT 1
Rendered sessions/_form.html.erb (2.9ms)
Rendered sessions/new.html.erb within layouts/application (57.7ms)
User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`company_id` = 1 AND `users`.`remember_token` = '7424474653d9bcdf853fdca0493314a283f3ccd6' LIMIT 1
Rendered layouts/_navigation.html.erb (2.4ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 401 Unauthorized in 388ms (Views: 347.4ms | ActiveRecord: 3.3ms)

Whenever I login it by default takes company_id=1 and authentication fails. I've tried everything but when I comment out acts_as_tenant(:company) in User model it works fine, Please help!

Was it helpful?

Solution

From your log I see that set_current_tenant_by_subdomain is firing a query to look for a subdomain with value NULL.

It looks like you are not using the subdomain correctly. What does request.subdomain return when you debug a page view?

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