You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9 KiB

  1. # frozen_string_literal: true
  2. class Settings::ApplicationsController < ApplicationController
  3. layout 'admin'
  4. before_action :authenticate_user!
  5. before_action :set_application, only: [:show, :update, :destroy, :regenerate]
  6. before_action :prepare_scopes, only: [:create, :update]
  7. before_action :set_body_classes
  8. def index
  9. @applications = current_user.applications.order(id: :desc).page(params[:page])
  10. end
  11. def new
  12. @application = Doorkeeper::Application.new(
  13. redirect_uri: Doorkeeper.configuration.native_redirect_uri,
  14. scopes: 'read write follow'
  15. )
  16. end
  17. def show; end
  18. def create
  19. @application = current_user.applications.build(application_params)
  20. if @application.save
  21. redirect_to settings_applications_path, notice: I18n.t('applications.created')
  22. else
  23. render :new
  24. end
  25. end
  26. def update
  27. if @application.update(application_params)
  28. redirect_to settings_applications_path, notice: I18n.t('generic.changes_saved_msg')
  29. else
  30. render :show
  31. end
  32. end
  33. def destroy
  34. @application.destroy
  35. redirect_to settings_applications_path, notice: I18n.t('applications.destroyed')
  36. end
  37. def regenerate
  38. @access_token = current_user.token_for_app(@application)
  39. @access_token.destroy
  40. redirect_to settings_application_path(@application), notice: I18n.t('applications.token_regenerated')
  41. end
  42. private
  43. def set_application
  44. @application = current_user.applications.find(params[:id])
  45. end
  46. def application_params
  47. params.require(:doorkeeper_application).permit(
  48. :name,
  49. :redirect_uri,
  50. :scopes,
  51. :website
  52. )
  53. end
  54. def prepare_scopes
  55. scopes = params.fetch(:doorkeeper_application, {}).fetch(:scopes, nil)
  56. params[:doorkeeper_application][:scopes] = scopes.join(' ') if scopes.is_a? Array
  57. end
  58. def set_body_classes
  59. @body_classes = 'admin'
  60. end
  61. end