我正在使用 Clearance 进行身份验证,并使用acts_as_tenant 来设置租户
用户.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
公司.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
日志
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)
每当我默认登录时,它都会使用 company_id=1 并且身份验证失败。我已经尝试了所有方法,但是当我在用户模型中注释掉 act_as_tenant(:company) 时,它工作正常,请帮助!