organization.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 all_members
  40. User
  41. .left_outer_joins(:organization, :organizations_users)
  42. .distinct
  43. .where('organizations.id = :id OR organizations_users.organization_id = :id', id:)
  44. end
  45. def destroy(associations: false)
  46. if associations
  47. delete_associations
  48. else
  49. unset_associations
  50. end
  51. secondary_members_to_touch = secondary_members.records
  52. super()
  53. secondary_members_to_touch.each(&:touch)
  54. end
  55. def attributes_with_association_ids
  56. attributes = super
  57. attributes['secondary_member_ids'] = secondary_member_ids
  58. attributes
  59. end
  60. private
  61. def domain_cleanup
  62. return true if domain.blank?
  63. domain.gsub!(%r{@}, '')
  64. domain.gsub!(%r{\s*}, '')
  65. domain.strip!
  66. domain.downcase!
  67. true
  68. end
  69. def delete_associations
  70. User.where(organization_id: id).find_each(&:destroy)
  71. Ticket.where(organization_id: id).find_each(&:destroy)
  72. end
  73. def member_update(user)
  74. if persisted?
  75. touch # rubocop:disable Rails/SkipsModelValidations
  76. end
  77. user&.touch # rubocop:disable Rails/SkipsModelValidations
  78. end
  79. def unset_associations
  80. User.where(organization_id: id).find_each do |user|
  81. user.update(organization_id: nil)
  82. end
  83. Ticket.where(organization_id: id).find_each do |ticket|
  84. ticket.update(organization_id: nil)
  85. end
  86. end
  87. end