applications_controller.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class ApplicationsController < ApplicationController
  3. prepend_before_action { authentication_check && authorize! }
  4. def index
  5. all = Doorkeeper::Application.all
  6. if response_full?
  7. assets = {}
  8. item_ids = []
  9. all.each do |item|
  10. item_ids.push item.id
  11. if !assets[:Application]
  12. assets[:Application] = {}
  13. end
  14. application = item.attributes
  15. application[:clients] = Doorkeeper::AccessToken.where(application_id: item.id).count
  16. assets[:Application][item.id] = application
  17. end
  18. render json: {
  19. record_ids: item_ids,
  20. assets: assets,
  21. }, status: :ok
  22. return
  23. end
  24. render json: all, status: :ok
  25. end
  26. def token
  27. access_token = Doorkeeper::AccessToken.create!(application_id: params[:id], resource_owner_id: current_user.id)
  28. render json: { token: access_token.token }, status: :ok
  29. end
  30. def show
  31. application = Doorkeeper::Application.find(params[:id])
  32. render json: application, status: :ok
  33. end
  34. def create
  35. application = Doorkeeper::Application.new(clean_params)
  36. application.save!
  37. render json: application, status: :ok
  38. end
  39. def update
  40. application = Doorkeeper::Application.find(params[:id])
  41. application.update!(clean_params)
  42. render json: application, status: :ok
  43. end
  44. def destroy
  45. application = Doorkeeper::Application.find(params[:id])
  46. application.destroy!
  47. render json: {}, status: :ok
  48. end
  49. private
  50. def clean_params
  51. params_data = params.permit!.to_h
  52. params_data.delete('application')
  53. params_data.delete('action')
  54. params_data.delete('controller')
  55. params_data.delete('id')
  56. params_data.delete('uid')
  57. params_data.delete('secret')
  58. params_data.delete('created_at')
  59. params_data.delete('updated_at')
  60. params_data
  61. end
  62. end