tramway-api 1.5.0.2 → 1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c7c200630528335bf88fc29901da97260dcff159e14d4e05f948a25211fdffb3
4
- data.tar.gz: f777b6bbc9ff7288e1b49d123b27ba7350a904305650cbbeafd738e31a64b828
3
+ metadata.gz: 01e0eb09865cd422bd78339ed17061bcb3e9783109877faf8c7d284386152696
4
+ data.tar.gz: 805e37d122674f9a95e71b8dcc98b819b34dfcf3e5ab8f1b6fc34883f65e6c1a
5
5
  SHA512:
6
- metadata.gz: 8a1ceb5825fb2ddccc0155b98de5ebd1cc36c707a61c9b61729eb6d23437ca7fd6fd2440fcaba32eb7e700f86054ffb77ce1258d6a26ef601002da0e7a86764f
7
- data.tar.gz: 891faee71e40942898a9848b01fe61dbe203f6af05efb32d9029b7a9aa771ce694784dde8076b3164ad2dfdd1878def1683beab1d790551aef5d41e83585faa2
6
+ metadata.gz: ee33d4fd776288f9ebe9159e8ac7387aa001a8687e32a6e91dff1b32d0786e7cc0ec58a19d597e0600c117785c234801fdad0adf9616cb00dc05f381af0314ce
7
+ data.tar.gz: f8b7b35880e8f509cdd73d9d9e9a41f3c9f3bd7d669c399a8b97b649494d444323188493e9abcc768b490242792bf2d88c6cd63268cef3ade7f4aca6e2ca8b7f
data/README.md CHANGED
@@ -43,6 +43,7 @@ coming soon...
43
43
  gem 'tramway-core'
44
44
  gem 'state_machine', github: 'seuros/state_machine'
45
45
  gem 'knock'
46
+ gem 'audited'
46
47
  ```
47
48
 
48
49
  ## Usage
@@ -18,7 +18,7 @@ module Tramway
18
18
  protected
19
19
 
20
20
  def authenticate
21
- return unauthorized unless current_user
21
+ return unauthorized if current_user.nil? || !params[:user_based_model].in?(Tramway::Api.user_based_models)
22
22
  end
23
23
 
24
24
  def auth_token
@@ -30,17 +30,19 @@ module Tramway
30
30
  end
31
31
 
32
32
  def entity
33
+ user_based_model = params[:user_based_model].constantize
33
34
  @entity ||=
34
- if Tramway::Api.user_based_model.respond_to? :from_token_request
35
- Tramway::Api.user_based_model.active.from_token_request request
35
+ if user_based_model.respond_to? :from_token_request
36
+ user_based_model.active.from_token_request request
36
37
  else
37
38
  params[:auth] && find_user_by_auth_attributes
38
39
  end
39
40
  end
40
41
 
41
42
  def find_user_by_auth_attributes
42
- Tramway::Api.auth_attributes.each do |attribute|
43
- object = Tramway::Api.user_based_model.active.where.not(attribute => nil).find_by(attribute => auth_params[:login])
43
+ user_based_model = params[:user_based_model].constantize
44
+ Tramway::Api.auth_attributes[user_based_model].each do |attribute|
45
+ object = user_based_model.active.where.not(attribute => nil).find_by(attribute => auth_params[:login])
44
46
  return object if object
45
47
  end
46
48
  nil
@@ -68,7 +68,9 @@ module Tramway::Api::V1
68
68
 
69
69
  def authenticate_user_if_needed
70
70
  if action_name.in? Tramway::Api::available_models[model_class.to_s][:closed]&.map(&:to_s) || []
71
- authenticate_user
71
+ Tramway::Api.user_based_models.map do |user_based_model|
72
+ send("current_#{user_based_model.name.underscore}").present?
73
+ end.include? true
72
74
  end
73
75
  end
74
76
 
@@ -1,5 +1,5 @@
1
1
  module Tramway
2
2
  module Api
3
- VERSION = '1.5.0.2'
3
+ VERSION = '1.6'
4
4
  end
5
5
  end
data/lib/tramway/api.rb CHANGED
@@ -4,19 +4,27 @@ module Tramway
4
4
  module Api
5
5
  class << self
6
6
  def auth_config
7
- @@auth_config ||= { user_model: ::Tramway::User::User, auth_attributes: :email }
7
+ @@auth_config ||= [{ user_model: ::Tramway::User::User, auth_attributes: :email }]
8
8
  end
9
9
 
10
- def auth_config=(**params)
11
- @@auth_config = params
10
+ def auth_config=(params)
11
+ if params.is_a? Hash
12
+ @@auth_config = [params]
13
+ elsif params.is_a? Array
14
+ @@auth_config = params
15
+ end
12
16
  end
13
17
 
14
- def user_based_model
15
- @@auth_config[:user_model]
18
+ def user_based_models
19
+ @@auth_config.map do |conf|
20
+ conf[:user_model]
21
+ end
16
22
  end
17
23
 
18
24
  def auth_attributes
19
- @@auth_config[:auth_attributes]
25
+ @@auth_config.reduce({}) do |hash, conf|
26
+ hash.merge! conf[:user_model] => conf[:auth_attributes]
27
+ end
20
28
  end
21
29
 
22
30
  def set_available_models(**models)
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.5.0.2
4
+ version: '1.6'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-07 00:00:00.000000000 Z
11
+ date: 2019-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: knock