organization.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. validates :name, presence: true, uniqueness: { case_sensitive: false }
  33. validates :domain, presence: { message: 'required when Domain Based Assignment is enabled' }, if: :domain_assignment
  34. # secondary_members will break eager_load of attributes_with_association_ids because it mixes up with the members relation.
  35. # so it will get added afterwards
  36. association_attributes_ignored :secondary_members, :tickets, :created_by, :updated_by
  37. activity_stream_permission 'admin.role'
  38. validates :note, length: { maximum: 5000 }
  39. sanitized_html :note, no_images: true
  40. def all_members
  41. User
  42. .left_outer_joins(:organization, :organizations_users)
  43. .distinct
  44. .where('organizations.id = :id OR organizations_users.organization_id = :id', id:)
  45. end
  46. def destroy(associations: false)
  47. if associations
  48. delete_associations
  49. else
  50. unset_associations
  51. end
  52. secondary_members_to_touch = secondary_members.records
  53. super()
  54. secondary_members_to_touch.each(&:touch)
  55. end
  56. def attributes_with_association_ids
  57. attributes = super
  58. attributes['secondary_member_ids'] = secondary_member_ids
  59. attributes
  60. end
  61. private
  62. def domain_cleanup
  63. return true if domain.blank?
  64. domain.gsub!(%r{@}, '')
  65. domain.gsub!(%r{\s*}, '')
  66. domain.strip!
  67. domain.downcase!
  68. true
  69. end
  70. def delete_associations
  71. User.where(organization_id: id).find_each(&:destroy)
  72. Ticket.where(organization_id: id).find_each(&:destroy)
  73. end
  74. def member_update(user)
  75. if persisted?
  76. touch # rubocop:disable Rails/SkipsModelValidations
  77. end
  78. user&.touch # rubocop:disable Rails/SkipsModelValidations
  79. end
  80. def unset_associations
  81. User.where(organization_id: id).find_each do |user|
  82. user.update(organization_id: nil)
  83. end
  84. Ticket.where(organization_id: id).find_each do |ticket|
  85. ticket.update(organization_id: nil)
  86. end
  87. end
  88. end