2026-01-02 13:41:09 +00:00
|
|
|
# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2023-03-10 14:49:19 +00:00
|
|
|
class Validations::DataPrivacyTaskValidator < ActiveModel::Validator
|
2020-09-08 15:06:23 +00:00
|
|
|
|
|
|
|
|
attr_reader :record
|
|
|
|
|
|
2023-11-03 12:47:39 +00:00
|
|
|
delegate :deletable, to: :record
|
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
def validate(record)
|
|
|
|
|
@record = record
|
|
|
|
|
|
2023-11-03 12:47:39 +00:00
|
|
|
check_for_deletable_type
|
2020-09-08 15:06:23 +00:00
|
|
|
check_for_existing_task
|
2023-11-03 12:47:39 +00:00
|
|
|
check_for_user
|
2020-09-08 15:06:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2023-11-03 12:47:39 +00:00
|
|
|
def check_for_deletable_type
|
2021-06-02 11:37:57 +00:00
|
|
|
return if !record.deletable_type_changed?
|
2025-08-06 06:17:01 +00:00
|
|
|
return if [User, Ticket].any? { deletable.is_a?(it) }
|
2020-09-08 15:06:23 +00:00
|
|
|
|
2023-11-22 15:41:08 +00:00
|
|
|
record.errors.add(:base, __('Data privacy task allows to delete a user or a ticket only.'))
|
2020-09-08 15:06:23 +00:00
|
|
|
end
|
|
|
|
|
|
2023-11-03 12:47:39 +00:00
|
|
|
def check_for_user
|
2021-06-02 11:37:57 +00:00
|
|
|
return if !record.deletable_id_changed?
|
2023-11-03 12:47:39 +00:00
|
|
|
return if !deletable.is_a?(User)
|
|
|
|
|
|
|
|
|
|
check_for_system_user
|
|
|
|
|
check_for_current_user
|
|
|
|
|
check_for_last_admin
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check_for_system_user
|
2020-09-08 15:06:23 +00:00
|
|
|
return if deletable.id != 1
|
|
|
|
|
|
2023-11-22 15:41:08 +00:00
|
|
|
record.errors.add(:base, __('It is not possible to delete the system user.'))
|
2020-09-08 15:06:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check_for_current_user
|
|
|
|
|
return if deletable.id != UserInfo.current_user_id
|
|
|
|
|
|
2023-11-22 15:41:08 +00:00
|
|
|
record.errors.add(:base, __('It is not possible to delete your current account.'))
|
2020-09-08 15:06:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check_for_last_admin
|
|
|
|
|
return if !last_admin?
|
|
|
|
|
|
2023-11-22 15:41:08 +00:00
|
|
|
record.errors.add(:base, __('It is not possible to delete the last account with admin permissions.'))
|
2020-09-08 15:06:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check_for_existing_task
|
2021-06-02 11:37:57 +00:00
|
|
|
return if !record.deletable_id_changed?
|
2020-09-08 15:06:23 +00:00
|
|
|
return if !tasks_exists?
|
|
|
|
|
|
2023-11-22 15:41:08 +00:00
|
|
|
record.errors.add(:base, __('Selected object is already queued for deletion.'))
|
2020-09-08 15:06:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def tasks_exists?
|
2023-11-03 12:47:39 +00:00
|
|
|
DataPrivacyTask
|
|
|
|
|
.where.not(id: record.id)
|
|
|
|
|
.where.not(state: 'failed')
|
|
|
|
|
.exists? deletable: deletable
|
2020-09-08 15:06:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def last_admin?
|
|
|
|
|
return false if !deletable_is_admin?
|
|
|
|
|
|
|
|
|
|
future_admin_ids.blank?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def future_admin_ids
|
|
|
|
|
other_admin_ids - existing_jobs_admin_ids
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def other_admin_ids
|
|
|
|
|
admin_users.where.not(id: deletable.id).pluck(:id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def deletable_is_admin?
|
|
|
|
|
admin_users.exists?(id: deletable.id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def existing_jobs_admin_ids
|
|
|
|
|
DataPrivacyTask.where(
|
|
|
|
|
deletable_id: other_admin_ids,
|
|
|
|
|
deletable_type: 'User'
|
|
|
|
|
).pluck(:deletable_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def admin_users
|
|
|
|
|
User.with_permissions('admin')
|
|
|
|
|
end
|
|
|
|
|
end
|