shopify_app 12.0.0 → 17.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +1 -0
  3. data/.github/ISSUE_TEMPLATE/bug-report.md +63 -0
  4. data/.github/ISSUE_TEMPLATE/config.yml +1 -0
  5. data/.github/ISSUE_TEMPLATE/feature-request.md +33 -0
  6. data/.github/PULL_REQUEST_TEMPLATE.md +22 -0
  7. data/.github/workflows/build.yml +41 -0
  8. data/.github/workflows/release.yml +24 -0
  9. data/.github/workflows/rubocop.yml +22 -0
  10. data/.gitignore +0 -2
  11. data/.rubocop.yml +14 -6
  12. data/CHANGELOG.md +180 -0
  13. data/CONTRIBUTING.md +76 -0
  14. data/Gemfile +5 -0
  15. data/Gemfile.lock +257 -0
  16. data/README.md +72 -487
  17. data/Rakefile +1 -0
  18. data/SECURITY.md +59 -0
  19. data/app/assets/images/storage_access.svg +1 -2
  20. data/app/assets/javascripts/shopify_app/itp_helper.js +6 -6
  21. data/app/assets/javascripts/shopify_app/storage_access.js +37 -7
  22. data/app/assets/javascripts/shopify_app/top_level_interaction.js +1 -1
  23. data/app/controllers/concerns/shopify_app/authenticated.rb +2 -1
  24. data/app/controllers/concerns/shopify_app/ensure_authenticated_links.rb +26 -0
  25. data/app/controllers/concerns/shopify_app/require_known_shop.rb +39 -0
  26. data/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb +32 -0
  27. data/app/controllers/shopify_app/authenticated_controller.rb +1 -0
  28. data/app/controllers/shopify_app/callback_controller.rb +113 -23
  29. data/app/controllers/shopify_app/extension_verification_controller.rb +2 -7
  30. data/app/controllers/shopify_app/sessions_controller.rb +62 -14
  31. data/app/controllers/shopify_app/webhooks_controller.rb +6 -5
  32. data/app/views/shopify_app/partials/_button_styles.html.erb +41 -36
  33. data/app/views/shopify_app/partials/_card_styles.html.erb +3 -3
  34. data/app/views/shopify_app/partials/_empty_state_styles.html.erb +28 -59
  35. data/app/views/shopify_app/partials/_form_styles.html.erb +56 -0
  36. data/app/views/shopify_app/partials/_layout_styles.html.erb +16 -1
  37. data/app/views/shopify_app/partials/_typography_styles.html.erb +6 -6
  38. data/app/views/shopify_app/sessions/enable_cookies.html.erb +3 -8
  39. data/app/views/shopify_app/sessions/new.html.erb +38 -110
  40. data/app/views/shopify_app/sessions/request_storage_access.html.erb +2 -2
  41. data/app/views/shopify_app/sessions/top_level_interaction.html.erb +21 -22
  42. data/config/locales/de.yml +11 -11
  43. data/config/locales/fi.yml +1 -1
  44. data/config/locales/nl.yml +8 -8
  45. data/config/locales/pt-BR.yml +1 -1
  46. data/config/locales/th.yml +4 -4
  47. data/config/locales/vi.yml +22 -0
  48. data/config/locales/zh-CN.yml +1 -1
  49. data/config/routes.rb +1 -0
  50. data/docs/Quickstart.md +15 -87
  51. data/docs/Releasing.md +18 -14
  52. data/docs/Troubleshooting.md +129 -4
  53. data/docs/Upgrading.md +126 -0
  54. data/docs/shopify_app/authentication.md +124 -0
  55. data/docs/shopify_app/engine.md +82 -0
  56. data/docs/shopify_app/generators.md +127 -0
  57. data/docs/shopify_app/handling-access-scopes-changes.md +14 -0
  58. data/docs/shopify_app/script-tags.md +28 -0
  59. data/docs/shopify_app/session-repository.md +88 -0
  60. data/docs/shopify_app/testing.md +38 -0
  61. data/docs/shopify_app/webhooks.md +72 -0
  62. data/karma.conf.js +1 -1
  63. data/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb +5 -3
  64. data/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb +1 -0
  65. data/lib/generators/shopify_app/add_marketing_activity_extension/add_marketing_activity_extension_generator.rb +2 -1
  66. data/lib/generators/shopify_app/add_marketing_activity_extension/templates/marketing_activities_controller.rb +4 -8
  67. data/lib/generators/shopify_app/add_webhook/add_webhook_generator.rb +5 -4
  68. data/lib/generators/shopify_app/add_webhook/templates/{webhook_job.rb → webhook_job.rb.tt} +5 -0
  69. data/lib/generators/shopify_app/app_proxy_controller/app_proxy_controller_generator.rb +4 -3
  70. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_controller.rb +3 -3
  71. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_route.rb +10 -9
  72. data/lib/generators/shopify_app/authenticated_controller/authenticated_controller_generator.rb +1 -1
  73. data/lib/generators/shopify_app/controllers/controllers_generator.rb +2 -1
  74. data/lib/generators/shopify_app/home_controller/home_controller_generator.rb +31 -3
  75. data/lib/generators/shopify_app/home_controller/templates/home_controller.rb +2 -0
  76. data/lib/generators/shopify_app/home_controller/templates/index.html.erb +66 -16
  77. data/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +11 -0
  78. data/lib/generators/shopify_app/install/install_generator.rb +46 -11
  79. data/lib/generators/shopify_app/install/templates/embedded_app.html.erb +1 -1
  80. data/lib/generators/shopify_app/install/templates/flash_messages.js +0 -2
  81. data/lib/generators/shopify_app/install/templates/omniauth.rb +3 -1
  82. data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +25 -0
  83. data/lib/generators/shopify_app/install/templates/shopify_provider.rb.tt +8 -0
  84. data/lib/generators/shopify_app/install/templates/user_agent.rb +2 -1
  85. data/lib/generators/shopify_app/products_controller/products_controller_generator.rb +19 -0
  86. data/lib/generators/shopify_app/products_controller/templates/products_controller.rb +8 -0
  87. data/lib/generators/shopify_app/routes/routes_generator.rb +1 -0
  88. data/lib/generators/shopify_app/routes/templates/routes.rb +10 -9
  89. data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +39 -7
  90. data/lib/generators/shopify_app/shop_model/templates/db/migrate/add_shop_access_scopes_column.erb +5 -0
  91. data/lib/generators/shopify_app/shop_model/templates/shop.rb +2 -1
  92. data/lib/generators/shopify_app/shopify_app_generator.rb +4 -3
  93. data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_access_scopes_column.erb +5 -0
  94. data/lib/generators/shopify_app/user_model/templates/user.rb +2 -1
  95. data/lib/generators/shopify_app/user_model/user_model_generator.rb +39 -7
  96. data/lib/generators/shopify_app/views/views_generator.rb +2 -1
  97. data/lib/shopify_app/access_scopes/noop_strategy.rb +13 -0
  98. data/lib/shopify_app/access_scopes/shop_strategy.rb +24 -0
  99. data/lib/shopify_app/access_scopes/user_strategy.rb +41 -0
  100. data/lib/shopify_app/configuration.rb +46 -11
  101. data/lib/shopify_app/controller_concerns/app_proxy_verification.rb +3 -3
  102. data/lib/shopify_app/controller_concerns/csrf_protection.rb +15 -0
  103. data/lib/shopify_app/controller_concerns/embedded_app.rb +3 -2
  104. data/lib/shopify_app/controller_concerns/localization.rb +1 -0
  105. data/lib/shopify_app/controller_concerns/login_protection.rb +105 -30
  106. data/lib/shopify_app/controller_concerns/payload_verification.rb +24 -0
  107. data/lib/shopify_app/controller_concerns/webhook_verification.rb +3 -18
  108. data/lib/shopify_app/engine.rb +27 -1
  109. data/lib/shopify_app/jobs/scripttags_manager_job.rb +1 -1
  110. data/lib/shopify_app/jobs/webhooks_manager_job.rb +1 -1
  111. data/lib/shopify_app/managers/scripttags_manager.rb +4 -3
  112. data/lib/shopify_app/managers/webhooks_manager.rb +4 -3
  113. data/lib/shopify_app/middleware/jwt_middleware.rb +42 -0
  114. data/lib/shopify_app/middleware/same_site_cookie_middleware.rb +19 -45
  115. data/lib/shopify_app/omniauth/omniauth_configuration.rb +64 -0
  116. data/lib/shopify_app/session/in_memory_session_store.rb +7 -3
  117. data/lib/shopify_app/session/in_memory_shop_session_store.rb +16 -0
  118. data/lib/shopify_app/session/in_memory_user_session_store.rb +16 -0
  119. data/lib/shopify_app/session/jwt.rb +63 -0
  120. data/lib/shopify_app/session/null_user_session_store.rb +22 -0
  121. data/lib/shopify_app/session/session_repository.rb +36 -14
  122. data/lib/shopify_app/session/session_storage.rb +1 -10
  123. data/lib/shopify_app/session/shop_session_storage.rb +42 -0
  124. data/lib/shopify_app/session/shop_session_storage_with_scopes.rb +58 -0
  125. data/lib/shopify_app/session/user_session_storage.rb +42 -0
  126. data/lib/shopify_app/session/user_session_storage_with_scopes.rb +58 -0
  127. data/lib/shopify_app/test_helpers/all.rb +2 -0
  128. data/lib/shopify_app/test_helpers/webhook_verification_helper.rb +17 -0
  129. data/lib/shopify_app/utils.rb +18 -5
  130. data/lib/shopify_app/version.rb +2 -1
  131. data/lib/shopify_app.rb +24 -5
  132. data/package.json +8 -9
  133. data/shopify_app.gemspec +15 -10
  134. data/translation.yml +1 -1
  135. data/yarn.lock +2120 -2168
  136. metadata +94 -20
  137. data/.github/ISSUE_TEMPLATE.md +0 -14
  138. data/.travis.yml +0 -27
  139. data/docs/install-on-dev-shop.png +0 -0
  140. data/docs/test-your-app.png +0 -0
  141. data/lib/generators/shopify_app/install/templates/shopify_app.rb +0 -15
  142. data/lib/generators/shopify_app/install/templates/shopify_provider.rb +0 -20
  143. data/lib/shopify_app/session/storage_strategies/shop_storage_strategy.rb +0 -23
  144. data/lib/shopify_app/session/storage_strategies/user_storage_strategy.rb +0 -24
  145. data/package-lock.json +0 -7224
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class HomeController < ApplicationController
4
+ include ShopifyApp::EmbeddedApp
5
+ include ShopifyApp::RequireKnownShop
6
+ include ShopifyApp::ShopAccessScopesVerification
7
+
8
+ def index
9
+ @shop_origin = current_shopify_domain
10
+ end
11
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'rails/generators/base'
2
3
 
3
4
  module ShopifyApp
@@ -10,13 +11,14 @@ module ShopifyApp
10
11
  class_option :scope, type: :array, default: ['read_products']
11
12
  class_option :embedded, type: :string, default: 'true'
12
13
  class_option :api_version, type: :string, default: nil
14
+ class_option :with_cookie_authentication, type: :boolean, default: false
13
15
 
14
16
  def create_shopify_app_initializer
15
17
  @application_name = format_array_argument(options['application_name'])
16
18
  @scope = format_array_argument(options['scope'])
17
19
  @api_version = options['api_version'] || ShopifyAPI::Meta.admin_versions.find(&:latest_supported).handle
18
20
 
19
- template 'shopify_app.rb', 'config/initializers/shopify_app.rb'
21
+ template('shopify_app.rb', 'config/initializers/shopify_app.rb')
20
22
  end
21
23
 
22
24
  def create_session_store_initializer
@@ -24,28 +26,30 @@ module ShopifyApp
24
26
  end
25
27
 
26
28
  def create_and_inject_into_omniauth_initializer
27
- unless File.exist? "config/initializers/omniauth.rb"
28
- copy_file 'omniauth.rb', 'config/initializers/omniauth.rb'
29
+ unless File.exist?("config/initializers/omniauth.rb")
30
+ copy_file('omniauth.rb', 'config/initializers/omniauth.rb')
29
31
  end
30
32
 
33
+ return if !Rails.env.test? && shopify_provider_exists?
34
+
31
35
  inject_into_file(
32
36
  'config/initializers/omniauth.rb',
33
- File.read(File.expand_path(find_in_source_paths('shopify_provider.rb'))),
34
- after: "Rails.application.config.middleware.use OmniAuth::Builder do\n"
37
+ shopify_provider_template,
38
+ after: "Rails.application.config.middleware.use(OmniAuth::Builder) do\n"
35
39
  )
36
40
  end
37
41
 
38
42
  def create_embedded_app_layout
39
43
  return unless embedded_app?
40
44
 
41
- copy_file 'embedded_app.html.erb', 'app/views/layouts/embedded_app.html.erb'
42
- copy_file '_flash_messages.html.erb', 'app/views/layouts/_flash_messages.html.erb'
45
+ copy_file('embedded_app.html.erb', 'app/views/layouts/embedded_app.html.erb')
46
+ copy_file('_flash_messages.html.erb', 'app/views/layouts/_flash_messages.html.erb')
43
47
 
44
48
  if ShopifyApp.use_webpacker?
45
49
  copy_file('shopify_app.js', 'app/javascript/shopify_app/shopify_app.js')
46
50
  copy_file('flash_messages.js', 'app/javascript/shopify_app/flash_messages.js')
47
51
  copy_file('shopify_app_index.js', 'app/javascript/shopify_app/index.js')
48
- append_to_file('app/javascript/packs/application.js', 'require("shopify_app")')
52
+ append_to_file('app/javascript/packs/application.js', "require(\"shopify_app\")\n")
49
53
  else
50
54
  copy_file('shopify_app.js', 'app/assets/javascripts/shopify_app.js')
51
55
  copy_file('flash_messages.js', 'app/assets/javascripts/flash_messages.js')
@@ -53,23 +57,50 @@ module ShopifyApp
53
57
  end
54
58
 
55
59
  def create_user_agent_initializer
56
- template 'user_agent.rb', 'config/initializers/user_agent.rb'
60
+ template('user_agent.rb', 'config/initializers/user_agent.rb')
57
61
  end
58
62
 
59
63
  def mount_engine
60
- route "mount ShopifyApp::Engine, at: '/'"
64
+ route("mount ShopifyApp::Engine, at: '/'")
61
65
  end
62
66
 
63
67
  def insert_hosts_into_development_config
64
68
  inject_into_file(
65
69
  'config/environments/development.rb',
66
- " config.hosts = (config.hosts rescue []) << /\\h+.ngrok.io/\n",
70
+ " config.hosts = (config.hosts rescue []) << /\\w+\\.ngrok\\.io/\n",
67
71
  after: "Rails.application.configure do\n"
68
72
  )
69
73
  end
70
74
 
71
75
  private
72
76
 
77
+ def shopify_provider_exists?
78
+ File.open("config/initializers/omniauth.rb") do |file|
79
+ file.each_line do |line|
80
+ if line =~ /provider :shopify/
81
+ puts "\e[33m#{omniauth_warning}\e[0m"
82
+ return true
83
+ end
84
+ end
85
+ end
86
+ false
87
+ end
88
+
89
+ def omniauth_warning
90
+ <<~OMNIAUTH
91
+ \n[WARNING] The Shopify App generator attempted to add the following Shopify Omniauth \
92
+ provider 'config/initializers/omniauth.rb':
93
+
94
+ \e[0m#{shopify_provider_template}\e[33m
95
+
96
+ Consider updating 'config/initializers/omniauth.rb' to match the configuration above.
97
+ OMNIAUTH
98
+ end
99
+
100
+ def shopify_provider_template
101
+ File.read(File.expand_path(find_in_source_paths('shopify_provider.rb.tt')))
102
+ end
103
+
73
104
  def embedded_app?
74
105
  options['embedded'] == 'true'
75
106
  end
@@ -77,6 +108,10 @@ module ShopifyApp
77
108
  def format_array_argument(array)
78
109
  array.join(' ').tr('"', '')
79
110
  end
111
+
112
+ def with_cookie_authentication?
113
+ options['with_cookie_authentication'] || !embedded_app?
114
+ end
80
115
  end
81
116
  end
82
117
  end
@@ -28,7 +28,7 @@
28
28
 
29
29
  <%= content_tag(:div, nil, id: 'shopify-app-init', data: {
30
30
  api_key: ShopifyApp.configuration.api_key,
31
- shop_origin: (@shop_session.domain if @shop_session),
31
+ shop_origin: @shop_origin || (@current_shopify_session.domain if @current_shopify_session),
32
32
  debug: Rails.env.development?
33
33
  } ) %>
34
34
 
@@ -20,7 +20,5 @@ if (!document.documentElement.hasAttribute("data-turbolinks-preview")) {
20
20
  isError: true,
21
21
  }).dispatch(Toast.Action.SHOW);
22
22
  }
23
-
24
- document.removeEventListener(eventName, flash)
25
23
  });
26
24
  }
@@ -1,2 +1,4 @@
1
- Rails.application.config.middleware.use OmniAuth::Builder do
1
+ # frozen_string_literal: true
2
+
3
+ Rails.application.config.middleware.use(OmniAuth::Builder) do
2
4
  end
@@ -0,0 +1,25 @@
1
+ ShopifyApp.configure do |config|
2
+ config.application_name = "<%= @application_name %>"
3
+ config.old_secret = "<%= @old_secret %>"
4
+ config.scope = "<%= @scope %>" # Consult this page for more scope options:
5
+ # https://help.shopify.com/en/api/getting-started/authentication/oauth/scopes
6
+ config.embedded_app = <%= embedded_app? %>
7
+ config.after_authenticate_job = false
8
+ config.api_version = "<%= @api_version %>"
9
+ config.shop_session_repository = 'Shop'
10
+
11
+ config.reauth_on_access_scope_changes = true
12
+
13
+ config.allow_jwt_authentication = <%= !with_cookie_authentication? %>
14
+ config.allow_cookie_authentication = <%= with_cookie_authentication? %>
15
+
16
+ config.api_key = ENV.fetch('SHOPIFY_API_KEY', '').presence
17
+ config.secret = ENV.fetch('SHOPIFY_API_SECRET', '').presence
18
+ if defined? Rails::Server
19
+ raise('Missing SHOPIFY_API_KEY. See https://github.com/Shopify/shopify_app#requirements') unless config.api_key
20
+ raise('Missing SHOPIFY_API_SECRET. See https://github.com/Shopify/shopify_app#requirements') unless config.secret
21
+ end
22
+ end
23
+
24
+ # ShopifyApp::Utils.fetch_known_api_versions # Uncomment to fetch known api versions from shopify servers on boot
25
+ # ShopifyAPI::ApiVersion.version_lookup_mode = :raise_on_unknown # Uncomment to raise an error if attempting to use an api version that was not previously known
@@ -0,0 +1,8 @@
1
+ provider :shopify,
2
+ ShopifyApp.configuration.api_key,
3
+ ShopifyApp.configuration.secret,
4
+ scope: ShopifyApp.configuration.scope,
5
+ setup: lambda { |env|
6
+ configuration = ShopifyApp::OmniAuthConfiguration.new(env['omniauth.strategy'], Rack::Request.new(env))
7
+ configuration.build_options
8
+ }
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyAPI
2
3
  class Base < ActiveResource::Base
3
- self.headers['User-Agent'] << " | ShopifyApp/#{ShopifyApp::VERSION}"
4
+ headers['User-Agent'] << " | ShopifyApp/#{ShopifyApp::VERSION}"
4
5
  end
5
6
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/base'
4
+
5
+ module ShopifyApp
6
+ module Generators
7
+ class ProductsControllerGenerator < Rails::Generators::Base
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ def create_products_controller
11
+ template('products_controller.rb', 'app/controllers/products_controller.rb')
12
+ end
13
+
14
+ def add_products_route
15
+ route("get '/products', :to => 'products#index'")
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ProductsController < AuthenticatedController
4
+ def index
5
+ @products = ShopifyAPI::Product.find(:all, params: { limit: 10 })
6
+ render(json: { products: @products })
7
+ end
8
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'rails/generators/base'
2
3
 
3
4
  module ShopifyApp
@@ -1,11 +1,12 @@
1
+ # frozen_string_literal: true
1
2
 
2
- controller :sessions do
3
- get 'login' => :new, :as => :login
4
- post 'login' => :create, :as => :authenticate
5
- get 'auth/shopify/callback' => :callback
6
- get 'logout' => :destroy, :as => :logout
7
- end
3
+ controller :sessions do
4
+ get 'login' => :new, :as => :login
5
+ post 'login' => :create, :as => :authenticate
6
+ get 'auth/shopify/callback' => :callback
7
+ get 'logout' => :destroy, :as => :logout
8
+ end
8
9
 
9
- namespace :webhooks do
10
- post ':type' => :receive
11
- end
10
+ namespace :webhooks do
11
+ post ':type' => :receive
12
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'rails/generators/base'
2
3
  require 'rails/generators/active_record'
3
4
 
@@ -7,31 +8,62 @@ module ShopifyApp
7
8
  include Rails::Generators::Migration
8
9
  source_root File.expand_path('../templates', __FILE__)
9
10
 
11
+ class_option :new_shopify_cli_app, type: :boolean, default: false
12
+
10
13
  def create_shop_model
11
- copy_file 'shop.rb', 'app/models/shop.rb'
14
+ copy_file('shop.rb', 'app/models/shop.rb')
12
15
  end
13
16
 
14
17
  def create_shop_migration
15
- migration_template 'db/migrate/create_shops.erb', 'db/migrate/create_shops.rb'
18
+ migration_template('db/migrate/create_shops.erb', 'db/migrate/create_shops.rb')
19
+ end
20
+
21
+ def create_shop_with_access_scopes_migration
22
+ scopes_column_prompt = <<~PROMPT
23
+ It is highly recommended that apps record the access scopes granted by \
24
+ merchants during app installation. See app/models/shop.rb to modify how \
25
+ access scopes are stored and retrieved.
26
+
27
+ [WARNING] You will need to update the access_scopes accessors in the Shop model \
28
+ to allow shopify_app to store and retrieve scopes when going through OAuth.
29
+
30
+ The following migration will add an `access_scopes` column to the Shop model. \
31
+ Do you want to include this migration? [y/n]
32
+ PROMPT
33
+
34
+ if new_shopify_cli_app? || Rails.env.test? || yes?(scopes_column_prompt)
35
+ migration_template(
36
+ 'db/migrate/add_shop_access_scopes_column.erb',
37
+ 'db/migrate/add_shop_access_scopes_column.rb'
38
+ )
39
+ end
16
40
  end
17
41
 
18
42
  def update_shopify_app_initializer
19
- gsub_file 'config/initializers/shopify_app.rb', 'ShopifyApp::InMemorySessionStore', 'Shop'
43
+ gsub_file('config/initializers/shopify_app.rb', 'ShopifyApp::InMemoryShopSessionStore', 'Shop')
20
44
  end
21
45
 
22
46
  def create_shop_fixtures
23
- copy_file 'shops.yml', 'test/fixtures/shops.yml'
47
+ copy_file('shops.yml', 'test/fixtures/shops.yml')
24
48
  end
25
49
 
26
50
  private
27
51
 
52
+ def new_shopify_cli_app?
53
+ options['new_shopify_cli_app']
54
+ end
55
+
28
56
  def rails_migration_version
29
57
  Rails.version.match(/\d\.\d/)[0]
30
58
  end
31
59
 
32
- # for generating a timestamp when using `create_migration`
33
- def self.next_migration_number(dir)
34
- ActiveRecord::Generators::Base.next_migration_number(dir)
60
+ class << self
61
+ private :next_migration_number
62
+
63
+ # for generating a timestamp when using `create_migration`
64
+ def next_migration_number(dir)
65
+ ActiveRecord::Generators::Base.next_migration_number(dir)
66
+ end
35
67
  end
36
68
  end
37
69
  end
@@ -0,0 +1,5 @@
1
+ class AddShopAccessScopesColumn < ActiveRecord::Migration[<%= rails_migration_version %>]
2
+ def change
3
+ add_column :shops, :access_scopes, :string
4
+ end
5
+ end
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
1
2
  class Shop < ActiveRecord::Base
2
- include ShopifyApp::SessionStorage
3
+ include ShopifyApp::ShopSessionStorageWithScopes
3
4
 
4
5
  def api_version
5
6
  ShopifyApp.configuration.api_version
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyApp
2
3
  module Generators
3
4
  class ShopifyAppGenerator < Rails::Generators::Base
@@ -7,10 +8,10 @@ module ShopifyApp
7
8
  end
8
9
 
9
10
  def run_all_generators
10
- generate "shopify_app:install #{@opts.join(' ')}"
11
- generate "shopify_app:shop_model"
11
+ generate("shopify_app:install #{@opts.join(' ')}")
12
+ generate("shopify_app:shop_model #{@opts.join(' ')}")
12
13
  generate("shopify_app:authenticated_controller")
13
- generate "shopify_app:home_controller"
14
+ generate("shopify_app:home_controller #{@opts.join(' ')}")
14
15
  end
15
16
  end
16
17
  end
@@ -0,0 +1,5 @@
1
+ class AddUserAccessScopesColumn < ActiveRecord::Migration[<%= rails_migration_version %>]
2
+ def change
3
+ add_column :users, :access_scopes, :string
4
+ end
5
+ end
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
1
2
  class User < ActiveRecord::Base
2
- include ShopifyApp::SessionStorage
3
+ include ShopifyApp::UserSessionStorageWithScopes
3
4
 
4
5
  def api_version
5
6
  ShopifyApp.configuration.api_version
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'rails/generators/base'
2
3
  require 'rails/generators/active_record'
3
4
 
@@ -7,31 +8,62 @@ module ShopifyApp
7
8
  include Rails::Generators::Migration
8
9
  source_root File.expand_path('../templates', __FILE__)
9
10
 
11
+ class_option :new_shopify_cli_app, type: :boolean, default: false
12
+
10
13
  def create_user_model
11
- copy_file 'user.rb', 'app/models/user.rb'
14
+ copy_file('user.rb', 'app/models/user.rb')
12
15
  end
13
16
 
14
17
  def create_user_migration
15
- migration_template 'db/migrate/create_users.erb', 'db/migrate/create_users.rb'
18
+ migration_template('db/migrate/create_users.erb', 'db/migrate/create_users.rb')
19
+ end
20
+
21
+ def create_scopes_storage_in_user_model
22
+ scopes_column_prompt = <<~PROMPT
23
+ It is highly recommended that apps record the access scopes granted by \
24
+ merchants during app installation. See app/models/user.rb to modify how \
25
+ access scopes are stored and retrieved.
26
+
27
+ [WARNING] You will need to update the access_scopes accessors in the User model \
28
+ to allow shopify_app to store and retrieve scopes when going through OAuth.
29
+
30
+ The following migration will add an `access_scopes` column to the User model. \
31
+ Do you want to include this migration? [y/n]
32
+ PROMPT
33
+
34
+ if new_shopify_cli_app? || Rails.env.test? || yes?(scopes_column_prompt)
35
+ migration_template(
36
+ 'db/migrate/add_user_access_scopes_column.erb',
37
+ 'db/migrate/add_user_access_scopes_column.rb'
38
+ )
39
+ end
16
40
  end
17
41
 
18
42
  def update_shopify_app_initializer
19
- gsub_file 'config/initializers/shopify_app.rb', 'ShopifyApp::InMemorySessionStore', 'User'
43
+ gsub_file('config/initializers/shopify_app.rb', 'ShopifyApp::InMemoryUserSessionStore', 'User')
20
44
  end
21
45
 
22
46
  def create_user_fixtures
23
- copy_file 'users.yml', 'test/fixtures/users.yml'
47
+ copy_file('users.yml', 'test/fixtures/users.yml')
24
48
  end
25
49
 
26
50
  private
27
51
 
52
+ def new_shopify_cli_app?
53
+ options['new_shopify_cli_app']
54
+ end
55
+
28
56
  def rails_migration_version
29
57
  Rails.version.match(/\d\.\d/)[0]
30
58
  end
31
59
 
32
- # for generating a timestamp when using `create_migration`
33
- def self.next_migration_number(dir)
34
- ActiveRecord::Generators::Base.next_migration_number(dir)
60
+ class << self
61
+ private :next_migration_number
62
+
63
+ # for generating a timestamp when using `create_migration`
64
+ def next_migration_number(dir)
65
+ ActiveRecord::Generators::Base.next_migration_number(dir)
66
+ end
35
67
  end
36
68
  end
37
69
  end