organization.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Organization < ApplicationModel
  3. include HasDefaultModelUserRelations
  4. include HasActivityStreamLog
  5. include ChecksClientNotification
  6. include HasHistory
  7. include HasSearchIndexBackend
  8. include CanCsvImport
  9. include ChecksHtmlSanitized
  10. include HasObjectManagerAttributes
  11. include HasTaskbars
  12. include Organization::Assets
  13. include Organization::Search
  14. include Organization::SearchIndex
  15. include Organization::TriggersSubscriptions
  16. include HasTransactionDispatcher
  17. default_scope { order(:id) }
  18. has_many :members, class_name: 'User', after_add: :member_update, after_remove: :member_update
  19. has_and_belongs_to_many :secondary_members, class_name: 'User', after_add: :member_update, after_remove: :member_update
  20. has_many :tickets, class_name: 'Ticket'
  21. before_create :domain_cleanup
  22. before_update :domain_cleanup
  23. # workflow checks should run after before_create and before_update callbacks
  24. include ChecksCoreWorkflow
  25. core_workflow_screens 'create', 'edit'
  26. validates :name, presence: true
  27. validates :domain, presence: { message: 'required when Domain Based Assignment is enabled' }, if: :domain_assignment
  28. # secondary_members will break eager_load of attributes_with_association_ids because it mixes up with the members relation.
  29. # so it will get added afterwards
  30. association_attributes_ignored :secondary_members, :tickets, :created_by, :updated_by
  31. activity_stream_permission 'admin.role'
  32. validates :note, length: { maximum: 5000 }
  33. sanitized_html :note, no_images: true
  34. def destroy(associations: false)
  35. if associations
  36. delete_associations
  37. else
  38. unset_associations
  39. end
  40. secondary_members_to_touch = secondary_members.records
  41. super()
  42. secondary_members_to_touch.each(&:touch)
  43. end
  44. def attributes_with_association_ids
  45. attributes = super
  46. attributes['secondary_member_ids'] = secondary_member_ids
  47. attributes
  48. end
  49. private
  50. def domain_cleanup
  51. return true if domain.blank?
  52. domain.gsub!(%r{@}, '')
  53. domain.gsub!(%r{\s*}, '')
  54. domain.strip!
  55. domain.downcase!
  56. true
  57. end
  58. def delete_associations
  59. User.where(organization_id: id).find_each(&:destroy)
  60. Ticket.where(organization_id: id).find_each(&:destroy)
  61. end
  62. def member_update(user)
  63. if persisted?
  64. touch # rubocop:disable Rails/SkipsModelValidations
  65. end
  66. user&.touch # rubocop:disable Rails/SkipsModelValidations
  67. end
  68. def unset_associations
  69. User.where(organization_id: id).find_each do |user|
  70. user.update(organization_id: nil)
  71. end
  72. Ticket.where(organization_id: id).find_each do |ticket|
  73. ticket.update(organization_id: nil)
  74. end
  75. end
  76. end