0

我使用的间隙进行身份验证和acts_as_tenant设置租户用户无法添加acts_as_tenant

User.rb

Clearance::User::Validations.module_eval do 

    included do 

    email_regex = /\A[\w+\-.][email protected][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后sign_in

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 

日志

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"=>"[email protected]", "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 ='[email protected]') LIMIT 1 
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`company_id` = 1 AND `users`.`email` = '[email protected]' 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,认证失败。我尝试了一切,但是当我在用户模型中注释出acts_as_tenant(:company)时,它工作正常,请帮助!

回答

2

从您的日志中我看到,set_current_tenant_by_subdomain正在触发查询以查找值为NULL的子域。

它看起来像你没有正确使用子域。调试页面视图时request.subdomain返回什么?