1 # frozen_string_literal: true
 
   4   desc "Register the built-in apps with specified user as owner; append necessary changes to settings file"
 
   5   task :register_apps, [:display_name] => :environment do |task, args|
 
   6     already_defined_keys = [:id_application, :oauth_application, :oauth_key] & Settings.keys
 
   7     settings_filename = Rails.root.join("config/settings.local.yml")
 
   8     settings_file_exists = File.file?(settings_filename)
 
  10     unless already_defined_keys.empty?
 
  11       if settings_file_exists
 
  12         puts "The following settings are already defined (likely in #{settings_filename}):"
 
  14         puts "The following settings are already defined:"
 
  16       already_defined_keys.each do |key|
 
  20       puts "Delete or comment them out to proceed."
 
  22       raise "App settings already defined"
 
  25     user = User.find_by! :display_name => args.display_name
 
  27     model = Doorkeeper.config.application_model
 
  29     id_application = model.create :name => "Local iD",
 
  30                                   :redirect_uri => "http://localhost:3000",
 
  31                                   :scopes => %w[read_prefs write_prefs write_api read_gpx write_gpx write_notes],
 
  32                                   :confidential => false,
 
  35     web_application = model.create :name => "OpenStreetMap Web Site",
 
  36                                    :redirect_uri => "http://localhost:3000",
 
  37                                    :scopes => %w[write_api write_notes],
 
  40     File.open settings_filename, "a" do |file|
 
  41       file << "\n" if settings_file_exists
 
  43         # generated by #{task} rake task
 
  44         # OAuth 2 Client ID for iD
 
  45         id_application: "#{id_application.uid}"
 
  46         # OAuth 2 Client ID for the web site
 
  47         oauth_application: "#{web_application.uid}"
 
  48         # OAuth 2 Client Secret for the web site
 
  49         oauth_key: "#{web_application.plaintext_secret}"
 
  53     puts "Updated settings in #{settings_filename}"