organization.rb 1.8 KB

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