applications_controller.rb 1.7 KB

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