2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
class Organization < ApplicationModel
|
2017-05-02 15:21:13 +00:00
|
|
|
include HasActivityStreamLog
|
|
|
|
include ChecksClientNotification
|
|
|
|
include ChecksLatestChangeObserved
|
|
|
|
include HasHistory
|
|
|
|
include HasSearchIndexBackend
|
2018-02-20 04:29:30 +00:00
|
|
|
include CanCsvImport
|
2019-02-12 07:38:59 +00:00
|
|
|
include ChecksHtmlSanitized
|
2019-03-13 23:51:22 +00:00
|
|
|
include HasObjectManagerAttributesValidation
|
2020-09-08 15:06:23 +00:00
|
|
|
include HasTaskbars
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2013-08-19 06:29:49 +00:00
|
|
|
include Organization::Assets
|
2018-04-26 08:55:53 +00:00
|
|
|
include Organization::Search
|
2014-01-29 23:54:34 +00:00
|
|
|
include Organization::SearchIndex
|
2018-05-05 17:03:14 +00:00
|
|
|
|
2021-05-20 06:59:02 +00:00
|
|
|
include HasTransactionDispatcher
|
|
|
|
|
2020-11-11 10:07:41 +00:00
|
|
|
has_many :members, class_name: 'User'
|
|
|
|
has_many :tickets, class_name: 'Ticket'
|
2021-01-27 09:58:35 +00:00
|
|
|
belongs_to :created_by, class_name: 'User'
|
|
|
|
belongs_to :updated_by, class_name: 'User'
|
2014-08-12 19:40:56 +00:00
|
|
|
|
2016-11-13 22:59:39 +00:00
|
|
|
before_create :domain_cleanup
|
|
|
|
before_update :domain_cleanup
|
|
|
|
|
2020-06-27 12:02:20 +00:00
|
|
|
validates :name, presence: true
|
|
|
|
validates :domain, presence: { message: 'required when Domain Based Assignment is enabled' }, if: :domain_assignment
|
2018-04-12 14:57:37 +00:00
|
|
|
|
2021-01-27 09:58:35 +00:00
|
|
|
association_attributes_ignored :tickets, :created_by, :updated_by
|
2020-09-14 12:34:36 +00:00
|
|
|
|
2017-01-31 17:13:45 +00:00
|
|
|
activity_stream_permission 'admin.role'
|
2015-06-18 22:39:34 +00:00
|
|
|
|
2019-02-12 07:38:59 +00:00
|
|
|
sanitized_html :note
|
|
|
|
|
2021-08-09 08:47:42 +00:00
|
|
|
def destroy(associations: false)
|
|
|
|
if associations
|
|
|
|
delete_associations
|
|
|
|
else
|
|
|
|
unset_associations
|
|
|
|
end
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
2015-06-18 22:39:34 +00:00
|
|
|
private
|
|
|
|
|
2016-11-13 22:59:39 +00:00
|
|
|
def domain_cleanup
|
2017-06-16 22:53:20 +00:00
|
|
|
return true if domain.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-05-12 11:37:44 +00:00
|
|
|
domain.gsub!(%r{@}, '')
|
|
|
|
domain.gsub!(%r{\s*}, '')
|
2016-11-13 22:59:39 +00:00
|
|
|
domain.strip!
|
|
|
|
domain.downcase!
|
2017-06-16 22:53:20 +00:00
|
|
|
true
|
2016-11-13 22:59:39 +00:00
|
|
|
end
|
|
|
|
|
2020-11-11 10:07:41 +00:00
|
|
|
def delete_associations
|
|
|
|
User.where(organization_id: id).find_each(&:destroy)
|
|
|
|
Ticket.where(organization_id: id).find_each(&:destroy)
|
|
|
|
end
|
2021-08-09 08:47:42 +00:00
|
|
|
|
|
|
|
def unset_associations
|
|
|
|
User.where(organization_id: id).find_each do |user|
|
|
|
|
user.update(organization_id: nil)
|
|
|
|
end
|
|
|
|
Ticket.where(organization_id: id) do |ticket|
|
|
|
|
ticket.update(organization_id: nil)
|
|
|
|
end
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|