organization.rb 3.0 KB

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