linked_rails 0.0.4.pre.g83aa52ab3 → 0.0.4.pre.g92825d924

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc8a8c53b8422c4f81be3dbb64c8488b6d80a659b0fb585f252c6d66842f240c
4
- data.tar.gz: 83172f0d901f0ad055186902ea9f9b5e9500b16bfa2071cad5533127765b57ec
3
+ metadata.gz: 2dfbd0d96709d5036bcfcb422c88bf39e1c8c006ae4a4e5d6c8dfabc7d45cbb7
4
+ data.tar.gz: b5629f658ccf27bcd7ca9605d001668f55df2d5b311ce170a20c68a7cc895057
5
5
  SHA512:
6
- metadata.gz: 0c00be6915256007188a56ed84b1b485dd5c56fcf6e310ab525d66488f26498b0127abb4bf39f6e6b206192d0eed9410b891ba21407794653349bbcde95fc95b
7
- data.tar.gz: d1488f86b20714eb98e4cbfdf9c8a6765e84be907dae2573274c02adc22047f0a6c206dc10ce2086823ab42028517d7f440e65b121aee53e35ba84b1e57f72b6
6
+ metadata.gz: bd523b32c77f3836257b1821b0a70fe4fcd9e6ab75bf39366501e75104aed3fe92ab9a690468051aacdace53ae5b79182642d3a55147da0d409f107fa47e1e7f
7
+ data.tar.gz: 220bac95f7fc0f3ca454e4dc7e6ca9953afc60e0e8230c497c22a8751cdc78a1c654760fa57908f2b4c716b382e308a371f104b8b48db027d081f4e1db3e8f3b
@@ -21,8 +21,11 @@ module LinkedRails
21
21
  return response_for_wrong_host(opts) if wrong_host?(opts[:iri])
22
22
 
23
23
  include = opts[:include].to_s == 'true'
24
+ resource = LinkedRails.iri_mapper.resource_from_iri(request_path_to_url(opts[:iri]), user_context)
24
25
 
25
- response_from_request(include, RDF::URI(opts[:iri]))
26
+ return response_from_request(include, RDF::URI(opts[:iri])) if resource.blank?
27
+
28
+ response_from_resource(include, opts[:iri], resource)
26
29
  rescue StandardError => e
27
30
  handle_resource_error(opts, e)
28
31
  end
@@ -84,6 +87,13 @@ module LinkedRails
84
87
  false
85
88
  end
86
89
 
90
+ def resource_cache_control(cacheable, status, resource_policy)
91
+ return :private unless status == 200 && cacheable
92
+ return 'no-cache' unless resource_policy.try(:public_resource?)
93
+
94
+ :public
95
+ end
96
+
87
97
  def resource_params(param)
88
98
  params = param.permit(:include, :iri)
89
99
  params[:iri] = URI(params[:iri])
@@ -131,6 +141,12 @@ module LinkedRails
131
141
  }.merge(opts)
132
142
  end
133
143
 
144
+ def resource_status(resource_policy)
145
+ raise(LinkedRails::Errors::Forbidden.new(query: :show?)) unless resource_policy.show?
146
+
147
+ 200
148
+ end
149
+
134
150
  def response_for_wrong_host(opts)
135
151
  iri = opts[:iri]
136
152
  term = term_from_vocab(iri)
@@ -139,6 +155,19 @@ module LinkedRails
139
155
  ontology_term_response(iri, term, opts[:include])
140
156
  end
141
157
 
158
+ def response_from_resource(include, iri, resource)
159
+ resource_policy = policy(resource)
160
+ status = resource_status(resource_policy)
161
+
162
+ resource_response(
163
+ iri,
164
+ body: include && status == 200 ? resource_body(resource) : nil,
165
+ cache: resource_cache_control(resource.try(:cacheable?), status, resource_policy),
166
+ language: I18n.locale,
167
+ status: status
168
+ )
169
+ end
170
+
142
171
  def term_from_vocab(iri)
143
172
  vocab = Vocab.for(iri)
144
173
  tag = iri.to_s.split(vocab.to_s).last
@@ -5,6 +5,7 @@ require 'pundit'
5
5
  module LinkedRails
6
6
  class Form # rubocop:disable Metrics/ClassLength
7
7
  include LinkedRails::Model
8
+ include LinkedRails::Model::Cacheable
8
9
 
9
10
  class_attribute :abstract_form, :pages, :model_class
10
11
 
@@ -5,6 +5,29 @@ module LinkedRails
5
5
  include ActiveModel::Model
6
6
  include LinkedRails::Model
7
7
 
8
+ def save
9
+ Storage.hset(
10
+ :persistent,
11
+ :manifest,
12
+ {
13
+ LinkedRails.iri.to_s => web_manifest.to_json
14
+ }
15
+ )
16
+ end
17
+
18
+ def web_manifest
19
+ web_manifest_base.merge(
20
+ ontola: web_manifest_ontola_section,
21
+ serviceworker: web_manifest_sw_section
22
+ )
23
+ end
24
+
25
+ private
26
+
27
+ def allowed_external_sources
28
+ []
29
+ end
30
+
8
31
  def app_name
9
32
  Rails.application.railtie_name.chomp('_application').humanize
10
33
  end
@@ -17,8 +40,27 @@ module LinkedRails
17
40
  '#eef0f2'
18
41
  end
19
42
 
43
+ def blob_preview_iri
44
+ return unless ActiveStorage::Blob.service.present?
45
+
46
+ LinkedRails.iri(path: 'rails/active_storage/blobs/redirect/{signed_id}/preview')
47
+ end
48
+
49
+ def blob_upload_iri
50
+ return unless ActiveStorage::Blob.service.present?
51
+
52
+ LinkedRails.iri(path: 'rails/active_storage/direct_uploads')
53
+ end
54
+
20
55
  def css_class; end
21
56
 
57
+ def csp_entries
58
+ {
59
+ connectSrc: [ActiveStorage::Blob.service.try(:bucket)&.url].compact,
60
+ scriptSrc: [ActiveStorage::Blob.service.try(:bucket)&.url].compact
61
+ }
62
+ end
63
+
22
64
  def header_background
23
65
  :primary
24
66
  end
@@ -27,17 +69,16 @@ module LinkedRails
27
69
  :white
28
70
  end
29
71
 
30
- def preload_iris
31
- [
32
- scope,
33
- LinkedRails.iri(path: 'ns/core').to_s,
34
- LinkedRails.iri(path: 'c_a').to_s,
35
- LinkedRails.iri(path: 'menus').to_s
36
- ]
72
+ def icons
73
+ []
74
+ end
75
+
76
+ def lang
77
+ :nl
37
78
  end
38
79
 
39
80
  def scope
40
- @scope ||= LinkedRails.iri.to_s
81
+ LinkedRails.iri.to_s
41
82
  end
42
83
 
43
84
  def site_theme_color
@@ -52,43 +93,46 @@ module LinkedRails
52
93
  app_name
53
94
  end
54
95
 
96
+ def start_url
97
+ scope == '/' ? scope : "#{scope}/"
98
+ end
99
+
55
100
  def theme; end
56
101
 
57
102
  def theme_options
58
103
  {}
59
104
  end
60
105
 
61
- def web_manifest
62
- web_manifest_base.merge(
63
- ontola: web_manifest_ontola_section,
64
- serviceworker: web_manifest_sw_section
65
- )
66
- end
67
-
68
106
  def web_manifest_base # rubocop:disable Metrics/MethodLength
69
107
  {
70
108
  background_color: background_color,
71
109
  dir: :rtl,
72
110
  display: :standalone,
73
- lang: :nl,
111
+ icons: icons,
112
+ lang: lang,
74
113
  name: app_name,
75
114
  scope: scope,
76
115
  short_name: app_name,
77
- start_url: scope,
116
+ start_url: start_url,
78
117
  theme_color: site_theme_color
79
118
  }
80
119
  end
81
120
 
82
121
  def web_manifest_ontola_section # rubocop:disable Metrics/MethodLength
83
122
  {
84
- css_class: css_class,
123
+ allowed_external_sources: allowed_external_sources,
124
+ blob_preview_iri: blob_preview_iri,
125
+ blob_upload_iri: blob_upload_iri,
126
+ csp: csp_entries,
85
127
  header_background: header_background,
86
128
  header_text: header_text,
87
- preload: preload_iris,
129
+ preconnect: preconnect,
88
130
  primary_color: site_theme_color,
89
131
  secondary_color: site_secondary_color,
132
+ styled_headers: styled_headers,
90
133
  theme: theme,
91
134
  theme_options: theme_options.to_query,
135
+ tracking: tracking,
92
136
  website_iri: LinkedRails.iri.to_s,
93
137
  websocket_path: websocket_path
94
138
  }
@@ -96,7 +140,7 @@ module LinkedRails
96
140
 
97
141
  def web_manifest_sw_section
98
142
  {
99
- src: "#{scope}/sw.js?manifestLocation=#{Rack::Utils.escape("#{scope}/manifest.json")}",
143
+ src: "#{scope}/sw.js",
100
144
  scope: scope
101
145
  }
102
146
  end
@@ -104,5 +148,31 @@ module LinkedRails
104
148
  def websocket_path
105
149
  Rails.application.config.try(:action_cable).try(:mount_path).try(:[], 1..-1)
106
150
  end
151
+
152
+ def preconnect
153
+ []
154
+ end
155
+
156
+ def styled_headers
157
+ false
158
+ end
159
+
160
+ def tracking
161
+ []
162
+ end
163
+
164
+ class << self
165
+ def destroy(iri)
166
+ Storage.hdel(:persistent, :manifest, iri)
167
+ end
168
+
169
+ def move(from, to)
170
+ data = Storage.hget(:persistent, :manifest, from)
171
+
172
+ Storage.hset(:persistent, :manifest, to, data) if data
173
+
174
+ destroy(from)
175
+ end
176
+ end
107
177
  end
108
178
  end
@@ -26,7 +26,7 @@ module LinkedRails
26
26
  end
27
27
 
28
28
  def menus
29
- @menus ||= available_menus.map(&method(:memoised_menu_item))
29
+ @menus ||= available_menus.map(&method(:memoized_menu_item))
30
30
  end
31
31
 
32
32
  def menu(tag)
@@ -4,6 +4,7 @@ module LinkedRails
4
4
  class Ontology
5
5
  include ActiveModel::Model
6
6
  include LinkedRails::Model
7
+ include LinkedRails::Model::Cacheable
7
8
 
8
9
  def classes
9
10
  @classes ||= LinkedRails.linked_models.map do |klass|
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LinkedRails
4
+ class FormPolicy < LinkedRails.policy_parent_class
5
+ def show?
6
+ true
7
+ end
8
+
9
+ def public_resource?
10
+ true
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LinkedRails
4
+ class OntologyPolicy < LinkedRails.policy_parent_class
5
+ def show?
6
+ true
7
+ end
8
+
9
+ def public_resource?
10
+ true
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LinkedRails
4
+ class InvalidationStreamWorker < ActiveJob::Base
5
+ def perform(type, iri, resource_type)
6
+ entry = {
7
+ type: type,
8
+ resource: iri,
9
+ resourceType: resource_type
10
+ }
11
+ id = Storage.xadd(:stream, entry)
12
+
13
+ raise('No message id returned, implies failure') if id.blank?
14
+ end
15
+ end
16
+ end
@@ -11,6 +11,8 @@ en:
11
11
  update:
12
12
  label: "Edit"
13
13
  success: 'Updated successfully'
14
+ errors:
15
+ access_denied: "You're not authorized for this action. (%{action})"
14
16
  linked_rails:
15
17
  status:
16
18
  400: "Error in request"
@@ -6,6 +6,10 @@ module LinkedRails
6
6
  extend ActiveSupport::Concern
7
7
  include ActiveSupport::Rescuable
8
8
 
9
+ included do
10
+ rescue_from LinkedRails::Errors::Forbidden, with: :handle_error
11
+ end
12
+
9
13
  private
10
14
 
11
15
  def add_error_snackbar(error)
@@ -69,6 +73,7 @@ module LinkedRails
69
73
  'Doorkeeper::Errors::InvalidGrantReuse' => 422,
70
74
  'LinkedRails::Auth::Errors::Expired' => 410,
71
75
  'LinkedRails::Auth::Errors::Unauthorized' => 401,
76
+ 'LinkedRails::Errors::Forbidden' => 403,
72
77
  'Pundit::NotAuthorizedError' => 403
73
78
  }
74
79
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LinkedRails
4
+ module Errors
5
+ class Forbidden < StandardError
6
+ attr_reader :query, :record, :policy, :action
7
+
8
+ # @param [Hash] options
9
+ # @option options [String] query The action of the request
10
+ # @option options [ActiveRecord::Base] record The record that was requested
11
+ # @option options [Policy] policy The policy that raised the exception
12
+ # @option options [String] message Override the default error message
13
+ # @return [String] the message
14
+ def initialize(**options)
15
+ @query = options.fetch(:query).to_s
16
+ @record = options[:record]
17
+ @policy = options[:policy]
18
+ @action = @query[-1] == '?' ? @query[0..-2] : @query
19
+ @message = options[:message]
20
+
21
+ raise StandardError if @query.blank? && @message.blank?
22
+
23
+ super(@message || default_message)
24
+ end
25
+
26
+ private
27
+
28
+ def default_message
29
+ I18n.t(
30
+ "pundit.#{@policy.class.to_s.underscore}.#{@query}",
31
+ action: @action,
32
+ default: I18n.t('errors.access_denied')
33
+ )
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'errors/forbidden'
@@ -46,6 +46,13 @@ module LinkedRails
46
46
 
47
47
  private
48
48
 
49
+ def request_path_to_url(path)
50
+ return path unless path.present? && URI(path).relative?
51
+
52
+ port = [80, 443].include?(request.port) ? nil : request.port
53
+ URI::Generic.new(request.scheme, nil, request.host, port, nil, path, nil, nil, nil).to_s
54
+ end
55
+
49
56
  def build_new_resource
50
57
  controller_class.build_new(user_context: user_context)
51
58
  end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LinkedRails
4
+ module Model
5
+ module Cacheable
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ if respond_to?(:after_commit)
10
+ after_commit :publish_create, on: :create, if: :should_publish_changes
11
+ after_commit :publish_update, on: :update, if: :should_publish_changes
12
+ after_commit :publish_delete, on: :destroy, if: :should_publish_changes
13
+ end
14
+ end
15
+
16
+ def cacheable?
17
+ true
18
+ end
19
+
20
+ private
21
+
22
+ def publish_create
23
+ publish_message('io.ontola.transactions.Created')
24
+ end
25
+
26
+ def publish_update
27
+ publish_message('io.ontola.transactions.Updated')
28
+ end
29
+
30
+ def publish_delete
31
+ publish_message('io.ontola.transactions.Deleted')
32
+ end
33
+
34
+ def publish_message(type)
35
+ LinkedRails::InvalidationStreamWorker.perform_now(type, iri.to_s, self.class.iri.to_s)
36
+ rescue StandardError
37
+ LinkedRails::InvalidationStreamWorker.perform_later(type, iri.to_s, self.class.iri.to_s)
38
+ end
39
+
40
+ private
41
+
42
+ def should_publish_changes
43
+ cacheable? && !Rails.env.test?
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'model/actionable'
4
+ require_relative 'model/cacheable'
4
5
  require_relative 'model/collections'
5
6
  require_relative 'model/dirty'
6
7
  require_relative 'model/enhancements'
@@ -46,6 +46,10 @@ module LinkedRails
46
46
  self.class.policy_class
47
47
  end
48
48
 
49
+ def public_resource?
50
+ false
51
+ end
52
+
49
53
  def show?
50
54
  false
51
55
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'redis'
4
+
5
+ module LinkedRails
6
+ class Storage
7
+ CLIENT = {
8
+ cache: Redis.new(db: LinkedRails.cache_redis_database),
9
+ persistent: Redis.new(db: LinkedRails.persistent_redis_database),
10
+ stream: Redis.new(db: LinkedRails.stream_redis_database)
11
+ }.freeze
12
+ KEYS = {
13
+ manifest: 'cache:Manifest',
14
+ redirect: 'cache:Redirect'
15
+ }.freeze
16
+
17
+ class << self
18
+ %i[xadd].each do |method|
19
+ define_method(method) do |db, *args|
20
+ CLIENT.fetch(db).send(method, *args)
21
+ end
22
+ end
23
+
24
+ %i[hset hdel hget].each do |method|
25
+ define_method(method) do |db, key, *args|
26
+ CLIENT.fetch(db).send(method, KEYS.fetch(key), *args)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
data/lib/linked_rails.rb CHANGED
@@ -22,6 +22,10 @@ module LinkedRails
22
22
 
23
23
  mattr_accessor :whitelisted_spi_ips
24
24
  mattr_writer :host, :scheme
25
+ mattr_accessor :persistent_redis_database, default: ENV['PERSISTENT_REDIS_DATABASE'].presence || 6
26
+ mattr_accessor :stream_redis_database, default: ENV['STREAM_REDIS_DATABASE'].presence || 7
27
+ mattr_accessor :cache_redis_database, default: ENV['CACHE_REDIS_DATABASE'].presence || 8
28
+ mattr_accessor :cache_stream, default: ENV['CACHE_STREAM'].presence || 'transactions'
25
29
 
26
30
  def self.configurable_class(parent, klass, default: nil, reader: nil) # rubocop:disable Metrics/AbcSize
27
31
  method = :"#{[parent, klass.to_s.downcase].compact.join('_')}_class"
@@ -84,6 +88,7 @@ ActiveSupport::Inflector.inflections do |inflect|
84
88
  inflect.acronym 'SHACL'
85
89
  end
86
90
 
91
+ require 'linked_rails/errors'
87
92
  require 'linked_rails/uri_template'
88
93
  require 'linked_rails/vocab'
89
94
  require 'linked_rails/cache'
@@ -101,3 +106,4 @@ require 'linked_rails/routes'
101
106
  require 'linked_rails/serializer'
102
107
  require 'linked_rails/translate'
103
108
  require 'linked_rails/railtie'
109
+ require 'linked_rails/storage'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linked_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4.pre.g83aa52ab3
4
+ version: 0.0.4.pre.g92825d924
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arthur Dingemans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-02 00:00:00.000000000 Z
11
+ date: 2022-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_response
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: redis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rdf
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -285,8 +299,10 @@ files:
285
299
  - app/policies/linked_rails/collection/view_policy.rb
286
300
  - app/policies/linked_rails/collection_policy.rb
287
301
  - app/policies/linked_rails/enum_value_policy.rb
302
+ - app/policies/linked_rails/form_policy.rb
288
303
  - app/policies/linked_rails/menus/item_policy.rb
289
304
  - app/policies/linked_rails/menus/list_policy.rb
305
+ - app/policies/linked_rails/ontology_policy.rb
290
306
  - app/policies/linked_rails/sequence_policy.rb
291
307
  - app/serializers/linked_rails/actions/item_serializer.rb
292
308
  - app/serializers/linked_rails/actions/object_serializer.rb
@@ -324,6 +340,7 @@ files:
324
340
  - app/serializers/linked_rails/web_page_serializer.rb
325
341
  - app/serializers/linked_rails/web_site_serializer.rb
326
342
  - app/serializers/linked_rails/widget_serializer.rb
343
+ - app/workers/linked_rails/invalidation_stream_worker.rb
327
344
  - config/initializers/inflections.rb
328
345
  - lib/generators/linked_rails/install/install_generator.rb
329
346
  - lib/generators/linked_rails/install/templates/README
@@ -373,6 +390,8 @@ files:
373
390
  - lib/linked_rails/enhancements/destroyable/controller.rb
374
391
  - lib/linked_rails/enhancements/updatable/controller.rb
375
392
  - lib/linked_rails/enhancements/updatable/serializer.rb
393
+ - lib/linked_rails/errors.rb
394
+ - lib/linked_rails/errors/forbidden.rb
376
395
  - lib/linked_rails/helpers/delta_helper.rb
377
396
  - lib/linked_rails/helpers/ontola_actions_helper.rb
378
397
  - lib/linked_rails/helpers/resource_helper.rb
@@ -381,6 +400,7 @@ files:
381
400
  - lib/linked_rails/middleware/linked_data_params.rb
382
401
  - lib/linked_rails/model.rb
383
402
  - lib/linked_rails/model/actionable.rb
403
+ - lib/linked_rails/model/cacheable.rb
384
404
  - lib/linked_rails/model/collections.rb
385
405
  - lib/linked_rails/model/dirty.rb
386
406
  - lib/linked_rails/model/enhancements.rb
@@ -404,6 +424,7 @@ files:
404
424
  - lib/linked_rails/serializer/actionable.rb
405
425
  - lib/linked_rails/serializer/menuable.rb
406
426
  - lib/linked_rails/serializer/singularable.rb
427
+ - lib/linked_rails/storage.rb
407
428
  - lib/linked_rails/test_methods.rb
408
429
  - lib/linked_rails/translate.rb
409
430
  - lib/linked_rails/types/iri_type.rb