beyond_canvas 0.15.1.pre → 0.16.2.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +0 -6
  3. data/Rakefile +3 -3
  4. data/app/assets/javascripts/beyond_canvas/base.js +248 -99
  5. data/app/assets/stylesheets/beyond_canvas/settings/_breakpoints.scss +6 -6
  6. data/app/assets/stylesheets/beyond_canvas/settings/_variables.scss +13 -13
  7. data/app/controllers/beyond_canvas/authentications_controller.rb +62 -0
  8. data/app/controllers/concerns/beyond_canvas/authentication.rb +24 -0
  9. data/app/controllers/concerns/beyond_canvas/request_validation.rb +1 -1
  10. data/app/controllers/concerns/beyond_canvas/resource_management.rb +33 -0
  11. data/app/javascript/beyond_canvas/base.js +0 -2
  12. data/app/javascript/beyond_canvas/initializers/buttons.js +7 -40
  13. data/app/javascript/beyond_canvas/initializers/flash.js +5 -13
  14. data/app/javascript/beyond_canvas/initializers/functions.js +41 -0
  15. data/app/javascript/beyond_canvas/initializers/inputs.js +3 -7
  16. data/app/views/beyond_canvas/authentications/new.html.erb +18 -0
  17. data/app/views/layouts/beyond_canvas/public.html.erb +3 -1
  18. data/config/locales/en.yml +4 -0
  19. data/config/routes.rb +6 -0
  20. data/lib/beyond_canvas.rb +18 -2
  21. data/lib/beyond_canvas/configuration.rb +4 -1
  22. data/lib/beyond_canvas/engine.rb +4 -0
  23. data/lib/beyond_canvas/models/authentication.rb +66 -0
  24. data/lib/beyond_canvas/models/shop.rb +28 -0
  25. data/lib/beyond_canvas/models/utils.rb +55 -0
  26. data/lib/beyond_canvas/parameter_sanitizer.rb +43 -0
  27. data/lib/beyond_canvas/rails/routes.rb +21 -0
  28. data/lib/beyond_canvas/version.rb +1 -1
  29. data/lib/generators/beyond_canvas/auth_model/auth_model_generator.rb +50 -0
  30. data/lib/generators/beyond_canvas/auth_model/templates/migration.erb +20 -0
  31. data/lib/generators/beyond_canvas/auth_model/templates/model.erb +5 -0
  32. data/lib/generators/beyond_canvas/controller/controller_generator.rb +20 -0
  33. data/lib/generators/beyond_canvas/controller/templates/controller.erb +37 -0
  34. data/lib/generators/beyond_canvas/custom_styles/templates/beyond_canvas_custom_styles.scss +153 -0
  35. data/lib/generators/beyond_canvas/install/install_generator.rb +15 -5
  36. data/lib/generators/beyond_canvas/install/templates/beyond_canvas.rb.erb +11 -0
  37. data/lib/generators/beyond_canvas/views/views_generator.rb +19 -0
  38. metadata +50 -5
  39. data/lib/generators/beyond_canvas/custom_styles/templates/beyond_canvas_custom_styles.sass +0 -123
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BeyondCanvas
4
+ module Models
5
+ module Shop # :nodoc:
6
+ extend ActiveSupport::Concern
7
+ include BeyondCanvas::Models::Authentication
8
+ include BeyondCanvas::Models::Utils
9
+
10
+ included do
11
+ attr_accessor :api_url, :return_url
12
+
13
+ ##############################################################################
14
+ # Validations
15
+ ##############################################################################
16
+
17
+ # Callback url params
18
+
19
+ validates :api_url,
20
+ presence: true,
21
+ on: :create
22
+ validates :return_url,
23
+ presence: true,
24
+ on: :create
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BeyondCanvas
4
+ module Models
5
+ module Utils # :nodoc:
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ ##############################################################################
10
+ # Instance methods
11
+ ##############################################################################
12
+
13
+ #
14
+ # Generates a new access_token and refresh_token
15
+ #
16
+ def refresh_token
17
+ beyond_session = BeyondApi::Session.new(api_url: beyond_api_url, refresh_token: beyond_refresh_token)
18
+ beyond_session.token.refresh
19
+
20
+ update(beyond_access_token: beyond_session.access_token,
21
+ beyond_refresh_token: beyond_session.refresh_token)
22
+ end
23
+
24
+ #
25
+ # Generates a new access_token and refresh_token if they have expired
26
+ #
27
+ def refresh_token_if_needed
28
+ token_timestamp = JWT.decode(beyond_access_token, nil, false).first['exp']
29
+ current_timestamp = DateTime.now.to_i
30
+ return unless token_timestamp - current_timestamp <= 3600
31
+
32
+ refresh_token
33
+ end
34
+
35
+ #
36
+ # Returns a BeyondApi::Session object with api_url, access_token and refresh_token attributes
37
+ #
38
+ def to_session
39
+ BeyondApi::Session.new(api_url: beyond_api_url,
40
+ access_token: beyond_access_token,
41
+ refresh_token: beyond_refresh_token)
42
+ end
43
+
44
+ ##############################################################################
45
+ # Class methods
46
+ ##############################################################################
47
+
48
+ def self.find_session(id)
49
+ shop = find(id)
50
+ shop.to_session
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BeyondCanvas
4
+ class ParameterSanitizer # :nodoc:
5
+ DEFAULT_PERMITTED_ATTRIBUTES = %i[code signature return_url api_url access_token_url].freeze
6
+
7
+ def initialize(resource_name, params)
8
+ @params = params
9
+ @resource_name = resource_name
10
+ @permitted = DEFAULT_PERMITTED_ATTRIBUTES
11
+ end
12
+
13
+ def sanitize
14
+ permit_keys(default_params)
15
+ end
16
+
17
+ def permit(*keys)
18
+ @permitted.concat(keys)
19
+ end
20
+
21
+ private
22
+
23
+ def default_params
24
+ if hashable_resource_params?
25
+ @params.fetch(@resource_name)
26
+ else
27
+ empty_params
28
+ end
29
+ end
30
+
31
+ def hashable_resource_params?
32
+ @params[@resource_name].respond_to?(:permit)
33
+ end
34
+
35
+ def empty_params
36
+ ActionController::Parameters.new({})
37
+ end
38
+
39
+ def permit_keys(parameters)
40
+ parameters.permit(*@permitted)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionDispatch
4
+ module Routing
5
+ class Mapper # :nodoc:
6
+ def beyond_canvas_for(*resources)
7
+ mount BeyondCanvas::Engine => BeyondCanvas.configuration.namespace
8
+
9
+ resource_name, options = resources
10
+ BeyondCanvas.auth_model = resource_name.to_s.singularize
11
+ BeyondCanvas.use_rails_app_controller = options.present? && options[:controller].present?
12
+
13
+ set_routes(resource_name, options[:controller]) if BeyondCanvas.use_rails_app_controller
14
+ end
15
+
16
+ def set_routes(resource_name, controller)
17
+ resources resource_name, controller: controller
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BeyondCanvas
4
- VERSION = '0.15.1.pre'
4
+ VERSION = '0.16.2.pre'
5
5
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/active_record'
4
+
5
+ module BeyondCanvas
6
+ module Generators
7
+ class AuthModelGenerator < ActiveRecord::Generators::Base # :nodoc:
8
+ desc 'Generates a model with the given name and provides a method to authenticate in Beyond Backend'
9
+
10
+ argument :attributes, type: :array, default: [], banner: 'field:type field:type'
11
+
12
+ source_root File.expand_path('templates', __dir__)
13
+
14
+ def copy_beyond_canvas_migration
15
+ migration_path = File.join('db', 'migrate')
16
+ migration_template 'migration.erb',
17
+ "#{migration_path}/beyond_canvas_create_#{table_name}.rb",
18
+ migration_version: migration_version
19
+ end
20
+
21
+ def generate_model
22
+ template 'model.erb', File.join('app', 'models', "#{file_path}.rb")
23
+ end
24
+
25
+ private
26
+
27
+ def rails5_and_up?
28
+ Rails::VERSION::MAJOR >= 5
29
+ end
30
+
31
+ def migration_version
32
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if rails5_and_up?
33
+ end
34
+
35
+ def migration_data
36
+ <<RUBY
37
+ t.string :encrypted_beyond_api_url, null: false
38
+ t.string :encrypted_beyond_api_url_iv, null: false
39
+ t.string :beyond_api_url_bidx, null: false
40
+
41
+ t.text :encrypted_beyond_access_token, null: true
42
+ t.text :encrypted_beyond_access_token_iv, null: true
43
+
44
+ t.text :encrypted_beyond_refresh_token, null: true
45
+ t.text :encrypted_beyond_refresh_token_iv, null: true
46
+ RUBY
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class BeyondCanvasCreate<%= table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
4
+ def change
5
+ create_table :<%= table_name %><%= primary_key_type %> do |t|
6
+ <%= migration_data -%>
7
+
8
+ <% attributes.each do |attribute| -%>
9
+ t.<%= attribute.type %> :<%= attribute.name %>
10
+ <% end -%>
11
+
12
+ t.timestamps null: false
13
+ end
14
+
15
+ add_index :<%= table_name %>, :encrypted_beyond_api_url_iv, unique: true
16
+ add_index :<%= table_name %>, :beyond_api_url_bidx, unique: true
17
+ add_index :<%= table_name %>, :encrypted_beyond_access_token_iv, unique: true
18
+ add_index :<%= table_name %>, :encrypted_beyond_refresh_token_iv, unique: true
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= file_path.classify %> < ApplicationRecord
4
+ include BeyondCanvas::Models::Shop
5
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/active_record'
4
+
5
+ module BeyondCanvas
6
+ module Generators
7
+ class ControllerGenerator < Rails::Generators::Base # :nodoc:
8
+ desc 'Creates an inherited Beyond Canvas controller in the app/controllers folder'
9
+
10
+ argument :scope, required: true, desc: 'The scope to create the controller, e.g. shops, users'
11
+
12
+ source_root File.expand_path('templates', __dir__)
13
+
14
+ def create_controller
15
+ template 'controller.erb',
16
+ "app/controllers/#{scope}_controller.rb"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= scope.camelize %>Controller < BeyondCanvas::AuthenticationsController
4
+ # before_action :configure_params, only: [:create]
5
+
6
+ # def new
7
+ # super
8
+ # end
9
+
10
+ # def create
11
+ # super
12
+ # end
13
+
14
+ # private
15
+
16
+ # If you have extra params to permit, append them to the sanitizer.
17
+ # def configure_params
18
+ # beyond_canvas_parameter_sanitizer.permit(:attribute1, :attribute2)
19
+ # end
20
+
21
+ # The path used after creating the shop in the database
22
+ # def after_create_path
23
+ # resource.return_url
24
+ # end
25
+
26
+ # def handle_active_record_exception(exception)
27
+ # super
28
+ # end
29
+
30
+ # def handle_beyond_api_exception(exception)
31
+ # super
32
+ # end
33
+
34
+ # def handle_standard_error_exception(exception)
35
+ # super
36
+ # end
37
+ end
@@ -0,0 +1,153 @@
1
+ // ************************************************************
2
+ // Colors
3
+ // ************************************************************
4
+
5
+ // $palette-primary: rgb(78, 183, 168);
6
+ // $palette-secondary: rgb(28, 53, 69);
7
+ // $palette-cancel: rgb(153, 153, 153);
8
+ // $palette-danger: rgb(218, 60, 60);
9
+
10
+ // ************************************************************
11
+ // General styles
12
+ // ************************************************************
13
+
14
+ // $main-background: rgb(233, 232, 220);
15
+
16
+ // ************************************************************
17
+ // Typography
18
+ // ************************************************************
19
+
20
+ // $main-color: rgb(62, 62, 62);
21
+
22
+ // ************************************************************
23
+ // Headlines
24
+ // ************************************************************
25
+
26
+ // $headline-color: rgb(122, 118, 76);
27
+
28
+ // ************************************************************
29
+ // Links
30
+ // ************************************************************
31
+
32
+ // $link-primary-color: darken($palette-primary, 10%);
33
+ // $link-secondary-color: darken($palette-cancel, 10%);
34
+ // $link-danger-color: darken($palette-danger, 10%);
35
+
36
+ // ************************************************************
37
+ // Buttons
38
+ // ************************************************************
39
+
40
+ // $button-primary-background: $palette-primary;
41
+ // $button-primary-color: $white;
42
+
43
+ // $button-secondary-background: $palette-cancel;
44
+ // $button-secondary-color: $white;
45
+
46
+ // $button-danger-background: $palette-danger;
47
+ // $button-danger-color: $white;
48
+
49
+ // $button-border-radius: 3px;
50
+ // $button-box-shadow: true;
51
+
52
+ // ************************************************************
53
+ // Cards
54
+ // ************************************************************
55
+
56
+ // $card-border-radius: 3px;
57
+ // $card-box-shadow: 0 2px 7px rgba($black, 0.2);
58
+ // $card-separator-color: rgb(222, 222, 222);
59
+ // $card-title-color: rgb(247, 133, 96);
60
+
61
+ // ************************************************************
62
+ // Containers
63
+ // ************************************************************
64
+
65
+
66
+ // ************************************************************
67
+ // Labels
68
+ // ************************************************************
69
+
70
+ // $label-color: rgb(128, 128, 128);
71
+
72
+ // ************************************************************
73
+ // Inputs
74
+ // ************************************************************
75
+
76
+ // $input-border-color: rgb(217, 216, 195);
77
+ // $input-border-color-focus: $palette-primary;
78
+ // $input-errors-color: $palette-danger;
79
+
80
+ // ************************************************************
81
+ // Checkboxes
82
+ // ************************************************************
83
+
84
+ // $checkbox-checked-color: #97C344;
85
+ // $checkbox-checked-background: #ffffff;
86
+ // $checkbox-unchecked-color: #C2BF9D;
87
+ // $checkbox-unchecked-background: #ffffff;
88
+
89
+ // ************************************************************
90
+ // Radiobuttons
91
+ // ************************************************************
92
+
93
+ // $radio-checked-color: #97C344;
94
+ // $radio-checked-background: #ffffff;
95
+ // $radio-unchecked-color: #C2BF9D;
96
+ // $radio-unchecked-background: #ffffff;
97
+
98
+ // ************************************************************
99
+ // Hints
100
+ // ************************************************************
101
+
102
+ // $hint-color: rgb(158, 158, 158);
103
+
104
+ // ************************************************************
105
+ // Logo
106
+ // ************************************************************
107
+
108
+ // $logo-margin-top-public: 0;
109
+ // $logo-margin-bottom-public: 34px;
110
+
111
+ // ************************************************************
112
+ // Flash
113
+ // ************************************************************
114
+
115
+ // $flash-success: rgb(123, 170, 81);
116
+ // $flash-notice: rgb(123, 170, 81);
117
+ // $flash-info: rgb(153, 153, 153);
118
+ // $flash-warning: rgb(243, 181, 71);
119
+ // $flash-error: rgb(218, 60, 60);
120
+ // $flash-border-radius: 4px;
121
+ // $flash-box-shadow: 0 1px 2px 0 rgba($black, 0.2);
122
+ // $flash-color: rgb(128, 128, 128);
123
+
124
+ // ************************************************************
125
+ // Tables
126
+ // ************************************************************
127
+
128
+ // $table-header-background: rgb(243, 242, 236);
129
+ // $table-border-color: rgb(233, 232, 220);
130
+
131
+ // ************************************************************
132
+ // Comments
133
+ // ************************************************************
134
+
135
+ // $comment-background: rgb(246, 246, 243);
136
+ // $comment-color: rgb(170, 169, 156);
137
+
138
+ // ************************************************************
139
+ // Notices
140
+ // ************************************************************
141
+
142
+ // $notice-success-background: rgb(123, 170, 81);
143
+ // $notice-notice-background: rgb(123, 170, 81);
144
+ // $notice-info-background: rgb(153, 153, 153);
145
+ // $notice-warning-background: rgb(243, 181, 71);
146
+ // $notice-error-background: rgb(218, 60, 60);
147
+ // $notice-border-radius: 4px;
148
+ // $notice-color: rgb(153, 153, 153);
149
+
150
+ // ************************************************************
151
+ // Markdown
152
+ // ************************************************************
153
+