| 
									
										
										
										
											2017-08-22 12:33:57 -04:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 06:32:13 +09:00
										 |  |  | class Settings::ApplicationsController < Settings::BaseController | 
					
						
							| 
									
										
										
										
											2017-08-22 12:33:57 -04:00
										 |  |  |   layout 'admin' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   before_action :authenticate_user! | 
					
						
							| 
									
										
										
										
											2017-08-23 00:59:35 +02:00
										 |  |  |   before_action :set_application, only: [:show, :update, :destroy, :regenerate] | 
					
						
							| 
									
										
										
										
											2017-08-23 22:16:20 +09:00
										 |  |  |   before_action :prepare_scopes, only: [:create, :update] | 
					
						
							| 
									
										
										
										
											2017-08-22 12:33:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def index | 
					
						
							| 
									
										
										
										
											2018-05-22 14:44:53 +02:00
										 |  |  |     @applications = current_user.applications.order(id: :desc).page(params[:page]) | 
					
						
							| 
									
										
										
										
											2017-08-22 12:33:57 -04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def new | 
					
						
							|  |  |  |     @application = Doorkeeper::Application.new( | 
					
						
							|  |  |  |       redirect_uri: Doorkeeper.configuration.native_redirect_uri, | 
					
						
							|  |  |  |       scopes: 'read write follow' | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 00:59:35 +02:00
										 |  |  |   def show; end | 
					
						
							| 
									
										
										
										
											2017-08-22 12:33:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def create | 
					
						
							|  |  |  |     @application = current_user.applications.build(application_params) | 
					
						
							| 
									
										
										
										
											2017-08-23 00:59:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 12:33:57 -04:00
										 |  |  |     if @application.save | 
					
						
							| 
									
										
										
										
											2017-08-23 00:59:35 +02:00
										 |  |  |       redirect_to settings_applications_path, notice: I18n.t('applications.created') | 
					
						
							| 
									
										
										
										
											2017-08-22 12:33:57 -04:00
										 |  |  |     else | 
					
						
							|  |  |  |       render :new | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def update | 
					
						
							| 
									
										
										
										
											2017-08-23 00:59:35 +02:00
										 |  |  |     if @application.update(application_params) | 
					
						
							| 
									
										
										
										
											2017-08-22 12:33:57 -04:00
										 |  |  |       redirect_to settings_applications_path, notice: I18n.t('generic.changes_saved_msg') | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       render :show | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def destroy | 
					
						
							|  |  |  |     @application.destroy | 
					
						
							| 
									
										
										
										
											2017-08-23 00:59:35 +02:00
										 |  |  |     redirect_to settings_applications_path, notice: I18n.t('applications.destroyed') | 
					
						
							| 
									
										
										
										
											2017-08-22 12:33:57 -04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def regenerate | 
					
						
							|  |  |  |     @access_token = current_user.token_for_app(@application) | 
					
						
							|  |  |  |     @access_token.destroy | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 00:59:35 +02:00
										 |  |  |     redirect_to settings_application_path(@application), notice: I18n.t('applications.token_regenerated') | 
					
						
							| 
									
										
										
										
											2017-08-22 12:33:57 -04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 00:59:35 +02:00
										 |  |  |   def set_application | 
					
						
							|  |  |  |     @application = current_user.applications.find(params[:id]) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 12:33:57 -04:00
										 |  |  |   def application_params | 
					
						
							|  |  |  |     params.require(:doorkeeper_application).permit( | 
					
						
							|  |  |  |       :name, | 
					
						
							|  |  |  |       :redirect_uri, | 
					
						
							|  |  |  |       :scopes, | 
					
						
							|  |  |  |       :website | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2017-08-23 22:16:20 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def prepare_scopes | 
					
						
							|  |  |  |     scopes = params.fetch(:doorkeeper_application, {}).fetch(:scopes, nil) | 
					
						
							|  |  |  |     params[:doorkeeper_application][:scopes] = scopes.join(' ') if scopes.is_a? Array | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2017-08-22 12:33:57 -04:00
										 |  |  | end |