permissions_controller.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase::PermissionsController < ApplicationController
  3. prepend_before_action :authentication_check
  4. before_action :fetch_object
  5. def show
  6. render json: response_hash
  7. end
  8. def update
  9. permissions_params = params.require(:permissions_dialog).permit(permissions: {})
  10. KnowledgeBase::PermissionsUpdate.new(@object, current_user).update_using_params!(permissions_params)
  11. render json: response_hash
  12. end
  13. private
  14. def fetch_object
  15. if params[:knowledge_base_id]
  16. @object = KnowledgeBase::Category.includes(:permissions).find params[:id]
  17. authorize @object, :permissions?
  18. else
  19. @object = KnowledgeBase.includes(:permissions).find params[:id]
  20. authorize @object, :update?
  21. end
  22. end
  23. def parent_object
  24. return if !@object.is_a? KnowledgeBase::Category
  25. @object.parent || @object.knowledge_base
  26. end
  27. def response_hash
  28. roles_editor = Role.with_permissions('knowledge_base.editor')
  29. roles_reader = Role.with_permissions('knowledge_base.reader') - roles_editor
  30. {
  31. roles_reader: roles_reader.pluck_as_hash(:id, :name),
  32. roles_editor: roles_editor.pluck_as_hash(:id, :name),
  33. permissions: @object.permissions_effective.pluck_as_hash(:id, :access, :role_id),
  34. inherited: parent_object&.permissions_effective&.pluck_as_hash(:id, :access, :role_id) || []
  35. }
  36. end
  37. end