organization.rb 3.2 KB

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