applications_controller.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 show; end
  9. def new
  10. @application = Doorkeeper::Application.new(
  11. redirect_uri: Doorkeeper.configuration.native_redirect_uri,
  12. scopes: 'read write follow'
  13. )
  14. 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. if @application.scopes_previously_changed?
  26. @access_token = current_user.token_for_app(@application)
  27. @access_token.destroy
  28. redirect_to settings_application_path(@application), notice: I18n.t('applications.token_regenerated')
  29. else
  30. redirect_to settings_application_path(@application), notice: I18n.t('generic.changes_saved_msg')
  31. end
  32. else
  33. render :show
  34. end
  35. end
  36. def destroy
  37. @application.destroy
  38. redirect_to settings_applications_path, notice: I18n.t('applications.destroyed')
  39. end
  40. def regenerate
  41. @access_token = current_user.token_for_app(@application)
  42. @access_token.destroy
  43. redirect_to settings_application_path(@application), notice: I18n.t('applications.token_regenerated')
  44. end
  45. private
  46. def set_application
  47. @application = current_user.applications.find(params[:id])
  48. end
  49. def application_params
  50. params.require(:doorkeeper_application).permit(
  51. :name,
  52. :redirect_uri,
  53. :scopes,
  54. :website
  55. )
  56. end
  57. def prepare_scopes
  58. scopes = params.fetch(:doorkeeper_application, {}).fetch(:scopes, nil)
  59. params[:doorkeeper_application][:scopes] = scopes.join(' ') if scopes.is_a? Array
  60. end
  61. end