2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
class DataPrivacyTask::Validation < ActiveModel::Validator
|
|
|
|
|
|
|
|
attr_reader :record
|
|
|
|
|
|
|
|
def validate(record)
|
|
|
|
@record = record
|
|
|
|
|
|
|
|
check_for_user
|
|
|
|
check_for_system_user
|
|
|
|
check_for_current_user
|
|
|
|
check_for_last_admin
|
|
|
|
check_for_existing_task
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_for_user
|
2021-06-02 11:37:57 +00:00
|
|
|
return if !record.deletable_type_changed?
|
2020-09-08 15:06:23 +00:00
|
|
|
return if deletable_is_user?
|
|
|
|
|
|
|
|
invalid_because(:deletable, 'is not a User')
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_for_system_user
|
2021-06-02 11:37:57 +00:00
|
|
|
return if !record.deletable_id_changed?
|
2020-09-08 15:06:23 +00:00
|
|
|
return if !deletable_is_user?
|
|
|
|
return if deletable.id != 1
|
|
|
|
|
|
|
|
invalid_because(:deletable, 'is undeletable system User with ID 1')
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_for_current_user
|
2021-06-02 11:37:57 +00:00
|
|
|
return if !record.deletable_id_changed?
|
2020-09-08 15:06:23 +00:00
|
|
|
return if !deletable_is_user?
|
|
|
|
return if deletable.id != UserInfo.current_user_id
|
|
|
|
|
|
|
|
invalid_because(:deletable, 'is your current account')
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_for_last_admin
|
2021-06-02 11:37:57 +00:00
|
|
|
return if !record.deletable_id_changed?
|
2020-09-08 15:06:23 +00:00
|
|
|
return if !deletable_is_user?
|
|
|
|
return if !last_admin?
|
|
|
|
|
|
|
|
invalid_because(:deletable, 'is last account with admin permissions')
|
|
|
|
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 !deletable_is_user?
|
|
|
|
return if !tasks_exists?
|
|
|
|
|
|
|
|
invalid_because(:deletable, 'has an existing DataPrivacyTask queued')
|
|
|
|
end
|
|
|
|
|
|
|
|
def deletable_is_user?
|
|
|
|
deletable.is_a?(User)
|
|
|
|
end
|
|
|
|
|
|
|
|
def deletable
|
|
|
|
record.deletable
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_because(attribute, message)
|
|
|
|
record.errors.add attribute, message
|
|
|
|
end
|
|
|
|
|
|
|
|
def tasks_exists?
|
|
|
|
DataPrivacyTask.where(
|
|
|
|
deletable: deletable
|
|
|
|
).where.not(
|
|
|
|
id: record.id,
|
2021-06-23 11:35:27 +00:00
|
|
|
).where.not(
|
2020-09-08 15:06:23 +00:00
|
|
|
state: 'failed'
|
|
|
|
).exists?
|
|
|
|
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
|