permission.rb 940 B

123456789101112131415161718192021222324252627282930313233
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase
  3. class Category
  4. class Permission
  5. def initialize(category)
  6. @category = category
  7. end
  8. def permissions_effective
  9. parents_for_category
  10. .map(&:permissions)
  11. .flatten
  12. .each_with_object([]) do |elem, memo|
  13. memo << elem if !memo.find { |added| added.role == elem.role }
  14. end
  15. end
  16. private
  17. def parents_for_category
  18. categories_tree = @category.self_with_parents
  19. categories_with_permissions = KnowledgeBase::Category.where(id: categories_tree).includes(:permissions).to_a
  20. sorted_with_permissions = categories_tree
  21. .map { |elem| categories_with_permissions.find { |elem_with_permissions| elem_with_permissions == elem } }
  22. sorted_with_permissions + [@category.knowledge_base]
  23. end
  24. end
  25. end
  26. end