organization.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
  2. class Organization < ApplicationModel
  3. include HasActivityStreamLog
  4. include ChecksClientNotification
  5. include ChecksLatestChangeObserved
  6. include HasHistory
  7. include HasSearchIndexBackend
  8. include CanCsvImport
  9. include ChecksHtmlSanitized
  10. include HasObjectManagerAttributesValidation
  11. include HasTaskbars
  12. include Organization::Assets
  13. include Organization::Search
  14. include Organization::SearchIndex
  15. include HasTransactionDispatcher
  16. has_many :members, class_name: 'User'
  17. has_many :tickets, class_name: 'Ticket'
  18. belongs_to :created_by, class_name: 'User'
  19. belongs_to :updated_by, class_name: 'User'
  20. before_create :domain_cleanup
  21. before_update :domain_cleanup
  22. validates :name, presence: true
  23. validates :domain, presence: { message: 'required when Domain Based Assignment is enabled' }, if: :domain_assignment
  24. association_attributes_ignored :tickets, :created_by, :updated_by
  25. activity_stream_permission 'admin.role'
  26. sanitized_html :note
  27. def destroy(associations: false)
  28. if associations
  29. delete_associations
  30. else
  31. unset_associations
  32. end
  33. super()
  34. end
  35. private
  36. def domain_cleanup
  37. return true if domain.blank?
  38. domain.gsub!(%r{@}, '')
  39. domain.gsub!(%r{\s*}, '')
  40. domain.strip!
  41. domain.downcase!
  42. true
  43. end
  44. def delete_associations
  45. User.where(organization_id: id).find_each(&:destroy)
  46. Ticket.where(organization_id: id).find_each(&:destroy)
  47. end
  48. def unset_associations
  49. User.where(organization_id: id).find_each do |user|
  50. user.update(organization_id: nil)
  51. end
  52. Ticket.where(organization_id: id) do |ticket|
  53. ticket.update(organization_id: nil)
  54. end
  55. end
  56. end