tramway-api 1.7.1.3 → 1.8

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: 53451b4721b855866ba93863ecf3cb03ddd935a9d899c9e62f8bb541f8b42eb7
4
- data.tar.gz: 2f690a27dc53bf3237f55502c82c6a4a1cd94be1992fe1b9832241bbcd746718
3
+ metadata.gz: fdcaae068ee8dbb36b6cfd6a85c414d5356c4e25d5d9404d8340f1093e18a786
4
+ data.tar.gz: 893e1421c8296743740e969d7740a536b98cca1c3f308b3b84c2959c717996c7
5
5
  SHA512:
6
- metadata.gz: 813983c93febd1d95e5d515feac93241d23c3d4954e525c64d366c33245c6fca98b0e5ca254ff9640f4b546084987e9f992d8d3d58fb39995d0b509a774e63b0
7
- data.tar.gz: d9ea1ba2d67f3565de42a73805230a0ccff7effab2592482ff8d5745cebd0925b9332e61b19738f45751916da1520e623498259f6b96ea20993678e929e8a4dc
6
+ metadata.gz: 68691b7551fb533604c7aafb6cb87d0b6770940c6da3d053800b55c6aeb0d191d8c377fe0a00581fb5dcd20206fa0483a1b47205403c9f781da88a72a1161bb7
7
+ data.tar.gz: b7988845eb7752b6e1f82d3956d52f2d13f419a221ee4aa7f1a4e0bea984417d61b84446b32d4b12cae68ffdf27b28ba0db16d4ff28901b6cc00171ce27d386a
@@ -29,7 +29,7 @@ module Tramway::Api::V1
29
29
  end
30
30
 
31
31
  def update
32
- record_form = form_class.new model_class.active.find_by! uuid: params[:id]
32
+ record_form = form_class.new record
33
33
  if record_form.submit snake_case params[:data][:attributes]
34
34
  render json: record_form.model,
35
35
  serializer: serializer_class,
@@ -41,7 +41,6 @@ module Tramway::Api::V1
41
41
  end
42
42
 
43
43
  def show
44
- record = model_class.active.find_by! uuid: params[:id]
45
44
  render json: record,
46
45
  serializer: serializer_class,
47
46
  include: '*',
@@ -49,7 +48,6 @@ module Tramway::Api::V1
49
48
  end
50
49
 
51
50
  def destroy
52
- record = model_class.active.find_by! uuid: params[:id]
53
51
  record.remove
54
52
  render json: record,
55
53
  serializer: serializer_class,
@@ -58,25 +56,46 @@ module Tramway::Api::V1
58
56
  end
59
57
 
60
58
  private
59
+
60
+ def record
61
+ if params[:id].present?
62
+ @record = model_class.find_by! uuid: params[:id]
63
+ end
64
+ end
61
65
 
62
66
  def check_available_model_class
63
- head(:unprocessable_entity) && return unless model_class
67
+ unless model_class
68
+ head(:unauthorized) && return unless current_user
69
+ head(:unprocessable_entity) && return
70
+ end
64
71
  end
65
72
 
66
73
  def check_available_model_action
67
- open_actions = Tramway::Api.available_models[model_class.to_s][:open]&.map(&:to_s) || []
68
- closed_actions = Tramway::Api.available_models[model_class.to_s][:closed]&.map(&:to_s) || []
69
- head(:unprocessable_entity) && return unless action_name.in? open_actions + closed_actions
74
+ action_is_available = checking_roles.map do |role|
75
+ Tramway::Api.action_is_available?(
76
+ record: record,
77
+ action: action_name.to_sym,
78
+ project: (@application_engine || @application.name),
79
+ role: role,
80
+ model_name: params[:model],
81
+ current_user: current_user
82
+ )
83
+ end.include? true
84
+
85
+ head(:unprocessable_entity) && return unless action_is_available
70
86
  end
71
87
 
72
88
  def authenticate_user_if_needed
73
- if action_name.in?(Tramway::Api.available_models[model_class.to_s][:closed]&.map(&:to_s) || []) && !current_user
74
- head(:unauthorized) && return
75
- end
89
+ action_is_open = Tramway::Api.action_is_available?(
90
+ action: action_name.to_sym,
91
+ project: (@application_engine || @application.name),
92
+ model_name: params[:model]
93
+ )
94
+ head(:unauthorized) && return if !current_user && !action_is_open
76
95
  end
77
96
 
78
97
  def model_class
79
- if params[:model].to_s.in? ::Tramway::Api.available_models.keys.map(&:to_s)
98
+ if params[:model].to_s.in? available_models_for_current_user
80
99
  begin
81
100
  params[:model].constantize
82
101
  rescue ActiveSupport::Concern::MultipleIncludedBlocks => e
@@ -85,6 +104,16 @@ module Tramway::Api::V1
85
104
  end
86
105
  end
87
106
 
107
+ def available_models_for_current_user
108
+ checking_roles.reduce([]) do |models, role|
109
+ models += ::Tramway::Api.available_models(role: role).map(&:to_s)
110
+ end
111
+ end
112
+
113
+ def checking_roles
114
+ [ :open, current_user&.role ].compact
115
+ end
116
+
88
117
  def decorator_class(model_name = nil)
89
118
  "#{model_name || model_class}Decorator".constantize
90
119
  end
@@ -1,10 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'tramway/api/engine'
4
+ require 'tramway/api/records_models'
5
+ require 'tramway/api/singleton_models'
4
6
 
5
7
  module Tramway
6
8
  module Api
7
9
  class << self
10
+ include ::Tramway::Api::RecordsModels
11
+ include ::Tramway::Api::SingletonModels
12
+
8
13
  def auth_config
9
14
  @@auth_config ||= [{ user_model: ::Tramway::User::User, auth_attributes: :email }]
10
15
  end
@@ -31,16 +36,48 @@ module Tramway
31
36
  end
32
37
  end
33
38
 
34
- def set_available_models(**models)
35
- @@available_models ||= {}
36
- models = models.reduce({}) do |hash, pair|
37
- hash.merge! pair[0].to_s.camelize => pair[1]
39
+ def get_models_by_key(checked_models, project, role)
40
+ unless project.present?
41
+ error = Tramway::Error.new(
42
+ plugin: :admin,
43
+ method: :get_models_by_key,
44
+ message: "Looks like you have not create at lease one instance of #{Tramway::Core.application.model_class} model"
45
+ )
46
+ raise error.message
38
47
  end
39
- @@available_models.merge! models
48
+ checked_models && checked_models != [] && checked_models[project][role]&.keys || []
49
+ end
50
+
51
+ def models_array(models_type:, role:)
52
+ instance_variable_get("@#{models_type}_models")&.map do |projects|
53
+ projects.last[role]&.keys
54
+ end&.flatten || []
55
+ end
56
+
57
+ def action_is_available?(record: nil, project:, role: :open, model_name:, action:, current_user: nil)
58
+ actions = select_actions(project: project, role: role, model_name: model_name)
59
+ availability = actions&.select do |a|
60
+ if a.is_a? Symbol
61
+ a == action.to_sym
62
+ elsif a.is_a? Hash
63
+ a.keys.first.to_sym == action.to_sym
64
+ end
65
+ end&.first
66
+
67
+ return false unless availability.present?
68
+ return true if availability.is_a? Symbol
69
+
70
+ availability.values.first.call record, current_user
40
71
  end
41
72
 
42
- def available_models
43
- @@available_models ||= {}
73
+ def select_actions(project:, role:, model_name:)
74
+ stringify_keys(@singleton_models&.dig(project, role))&.dig(model_name) || stringify_keys(@available_models&.dig(project, role))&.dig(model_name)
75
+ end
76
+
77
+ def stringify_keys(hash)
78
+ hash&.reduce({}) do |new_hash, pair|
79
+ new_hash.merge! pair[0].to_s => pair[1]
80
+ end
44
81
  end
45
82
  end
46
83
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tramway::Api::RecordsModels
4
+ def set_available_models(*models, project:, role: :open)
5
+ @available_models ||= {}
6
+ @available_models[project] ||= {}
7
+ @available_models[project][role] ||= {}
8
+ models.each do |model|
9
+ if model.class == Class
10
+ @available_models[project][role].merge! model => %i[index show update create destroy]
11
+ elsif model.class == Hash
12
+ @available_models[project][role].merge! model
13
+ end
14
+ end
15
+ @available_models = @available_models.with_indifferent_access
16
+ end
17
+
18
+ def available_models_for(project, role: :open)
19
+ models = get_models_by_key(@available_models, project, role)
20
+ if project_is_engine?(project)
21
+ models += engine_class(project).dependencies.map do |dependency|
22
+ if @available_models&.dig(dependency, role).present?
23
+ @available_models&.dig(dependency, role).keys
24
+ else
25
+ error = Tramway::Error.new(
26
+ plugin: :admin,
27
+ method: :available_models_for,
28
+ message: "There is no dependency `#{dependency}` for plugin: #{project}. Please, check file `tramway-#{project}/lib/tramway/#{project}/#{project}.rb`"
29
+ )
30
+ raise error
31
+ end
32
+ end.flatten.compact
33
+ end
34
+ models
35
+ end
36
+
37
+ def available_models(role:)
38
+ models_array models_type: :available, role: role
39
+ end
40
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tramway::Api::SingletonModels
4
+ def set_singleton_models(*models, project:, role: :open)
5
+ @singleton_models ||= {}
6
+ @singleton_models[project] ||= {}
7
+ @singleton_models[project][role] ||= {}
8
+ models.each do |model|
9
+ if model.class == Class
10
+ @singleton_models[project][role].merge! model => %i[index show update create destroy]
11
+ elsif model.class == Hash
12
+ @singleton_models[project][role].merge! model
13
+ end
14
+ end
15
+ @singleton_models = @singleton_models.with_indifferent_access
16
+ end
17
+
18
+ def singleton_models_for(project, role: :open)
19
+ models = get_models_by_key(@singleton_models, project, role)
20
+ if project_is_engine?(project)
21
+ models += engine_class(project).dependencies.map do |dependency|
22
+ @singleton_models&.dig(dependency, role)&.keys
23
+ end.flatten.compact
24
+ end
25
+ models
26
+ end
27
+
28
+ def singleton_models(role:)
29
+ models_array models_type: :singleton, role: role
30
+ end
31
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Tramway
4
4
  module Api
5
- VERSION = '1.7.1.3'
5
+ VERSION = '1.8'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tramway-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.1.3
4
+ version: '1.8'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-16 00:00:00.000000000 Z
11
+ date: 2020-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -81,6 +81,8 @@ files:
81
81
  - lib/tasks/tramway/api_tasks.rake
82
82
  - lib/tramway/api.rb
83
83
  - lib/tramway/api/engine.rb
84
+ - lib/tramway/api/records_models.rb
85
+ - lib/tramway/api/singleton_models.rb
84
86
  - lib/tramway/api/version.rb
85
87
  homepage: https://github.com/kalashnikovisme/tramway-dev
86
88
  licenses: []