required.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # This validator is used in special situations only. In most cases CoreWorkflow is used instead.
  3. # It does not run for any object which has ChecksCoreWorkflow concern.
  4. # Please note that all objects that may have custom attributes created via UI use CoreWorkflow.
  5. # This may run in Ticket::Article if custom attribute were created manually.
  6. # Or if there are custom modifications using this functionality
  7. class Validations::ObjectManager::AttributeValidator::Required < Validations::ObjectManager::AttributeValidator::Backend
  8. def validate
  9. return if record.class.include?(ChecksCoreWorkflow)
  10. return if !value.nil?
  11. return if optional_for_user?
  12. invalid_because_attribute(__('is required but missing'))
  13. end
  14. private
  15. def optional_for_user?
  16. return true if system_user?
  17. return true if required_for_permissions.blank?
  18. return false if required_for_permissions.include?('-all-')
  19. !user.permissions?(required_for_permissions)
  20. end
  21. def system_user?
  22. user_id.blank? || user_id == 1
  23. end
  24. def user_id
  25. @user_id ||= UserInfo.current_user_id
  26. end
  27. def user
  28. @user ||= User.lookup(id: user_id)
  29. end
  30. def required_for_permissions
  31. @required_for_permissions ||= begin
  32. attribute.screens[action]&.each_with_object([]) do |(permission, config), result|
  33. result.push(permission) if config[:required].present?
  34. end
  35. end
  36. end
  37. def action
  38. return :edit if record.persisted?
  39. attribute.screens.keys.find { |e| e.start_with?('create') }
  40. end
  41. end