client_management/app/models/site_server.rb

61 lines
2.5 KiB
Ruby
Raw Normal View History

class SiteServer
include Mongoid::Document
include Mongoid::Timestamps
include OrbitCategory::Categorizable
include OrbitTag::Taggable
include OrbitModel::Status
field :server_name , type: String ,default: ''
field :enable_redirect_default_domain, type: Boolean, default: false
field :default_domain_names, type: Array, default: []
field :domain_name , type: String ,default: ''
field :domain_names, type: Array, default: []
field :ip , type: String ,default: ''
field :account , type: String ,default: ''
field :password , type: String ,default: ''
field :active , type: Boolean ,default: true
2021-08-11 08:16:14 +00:00
field :has_certbot, type: Boolean , default: false
field :need_update_site_ids, type: Array, default: []
2022-09-08 06:48:32 +00:00
has_many :site_server_file_backups, :autosave => true, :dependent => :destroy
has_many :site_server_db_backups, :autosave => true, :dependent => :destroy
def domain_names
if self.domain_name != ''
[self.domain_name]
else
super
end
end
def site_constructs
SiteConstruct.where(server_type: self.server_name)
end
before_save do
if self.domain_name != '' && self.domain_names.length !=0
self.domain_name = ''
end
tmp_site_constructs = site_constructs
need_change = false
if self.enable_redirect_default_domain_changed?
tmp_site_constructs = tmp_site_constructs.where(:enable_redirect_default_domain.in=>[nil,0])
need_change = true
end
if (need_change || (!need_change && self.enable_redirect_default_domain))
if self.default_domain_names_changed?
check_domains = []
2021-11-18 01:39:01 +00:00
removed_default_domains = self.default_domain_names_was.to_a - self.default_domain_names
add_default_domains = self.default_domain_names - self.default_domain_names_was.to_a
check_domains += removed_default_domains
check_domains += add_default_domains
check_domains = check_domains.map{|domain| ::Regexp.new("(\\A|\\s)"+domain.gsub(".","\\.").gsub("*","[^.]*") + "($|\\s)")}
tmp_site_constructs = tmp_site_constructs.where(:domain_name.in=>check_domains)
need_change = true
elsif need_change
check_domains = self.default_domain_names
check_domains = check_domains.map{|domain| ::Regexp.new("(\\A|\\s)"+domain.gsub(".","\\.").gsub("*","[^.]*") + "($|\\s)")}
tmp_site_constructs = tmp_site_constructs.where(:domain_name.in=>check_domains)
end
end
if need_change
self.need_update_site_ids += tmp_site_constructs.pluck(:id)
self.need_update_site_ids = self.need_update_site_ids.uniq
end
end
end