applications_controller.rb 1.8 KB

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