data_privacy_task.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class DataPrivacyTask < ApplicationModel
  3. include DataPrivacyTask::HasActivityStreamLog
  4. include ChecksClientNotification
  5. store :preferences
  6. belongs_to :created_by, class_name: 'User'
  7. belongs_to :updated_by, class_name: 'User'
  8. # optional because related data will get deleted and it would
  9. # cause validation errors if e.g. the created_by_id of the task
  10. # would need to get mapped by a deletion
  11. belongs_to :deletable, polymorphic: true, optional: true
  12. before_create :prepare_deletion_preview
  13. validates_with DataPrivacyTask::Validation
  14. def perform
  15. perform_deletable
  16. update!(state: 'completed')
  17. rescue => e
  18. handle_exception(e)
  19. end
  20. # set user inactive before destroy to prevent
  21. # new online notifications or other events while
  22. # the deletion process is running
  23. # https://github.com/zammad/zammad/issues/3942
  24. def update_inactive(object)
  25. object.update(active: false)
  26. end
  27. def perform_deletable
  28. return if !deletable_type.constantize.exists?(id: deletable_id)
  29. prepare_deletion_preview
  30. save!
  31. if delete_organization?
  32. perform_organization
  33. else
  34. perform_user
  35. end
  36. end
  37. def perform_organization
  38. update_inactive(deletable.organization)
  39. deletable.organization.members.find_each { |user| update_inactive(user) }
  40. deletable.organization.destroy(associations: true)
  41. end
  42. def perform_user
  43. update_inactive(deletable)
  44. deletable.destroy
  45. end
  46. def handle_exception(e)
  47. Rails.logger.error e
  48. preferences[:error] = "ERROR: #{e.inspect}"
  49. self.state = 'failed'
  50. save!
  51. end
  52. def delete_organization?
  53. return false if preferences[:delete_organization].blank?
  54. return false if preferences[:delete_organization] != 'true'
  55. return false if !deletable.organization
  56. return false if deletable.organization.members.count != 1
  57. true
  58. end
  59. def prepare_deletion_preview
  60. prepare_deletion_preview_tickets
  61. prepare_deletion_preview_user
  62. prepare_deletion_preview_organization
  63. prepare_deletion_preview_anonymize
  64. end
  65. def prepare_deletion_preview_tickets
  66. preferences[:owner_tickets] = deletable.owner_tickets.order(id: 'DESC').map(&:number)
  67. preferences[:customer_tickets] = deletable.customer_tickets.order(id: 'DESC').map(&:number)
  68. end
  69. def prepare_deletion_preview_user
  70. preferences[:user] = {
  71. firstname: deletable.firstname,
  72. lastname: deletable.lastname,
  73. email: deletable.email,
  74. }
  75. end
  76. def prepare_deletion_preview_organization
  77. return if !deletable.organization
  78. preferences[:user][:organization] = deletable.organization.name
  79. end
  80. def prepare_deletion_preview_anonymize
  81. preferences[:user] = Pseudonymisation.of_hash(preferences[:user])
  82. end
  83. def self.cleanup(diff = 12.months)
  84. where('created_at < ?', diff.ago).destroy_all
  85. true
  86. end
  87. end