checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
---
2
2
SHA256:
3
- metadata.gz: b2a134ace39764a8b805342beba2420b7234d7478f61555561a6aef25d859d56
4
- data.tar.gz: af53226c426b2ceb419a505d939bad28246dcb96290c7c74cf7a9cc8002fe0c5
3
+ metadata.gz: 48f0c775bc9c06fa4941bd20b903cf38dca11a86ac7c258f809773e701e9805d
4
+ data.tar.gz: 4fdc93ba99325be6512429b40b2accdce93096091734119df9baa418455763c1
5
5
SHA512:
6
- metadata.gz: 52ed2c22e3ba6ccac43022e7b34045c592bb9f65bb3e17d9cf088aa094cb386ba764f1a0da9194ba28087bd7669671faeecc7a2ba799b06d1ef2409ab396028b
7
- data.tar.gz: 037d92ad51a0e3acb2c7487f4ef95590e37c504f72695643e54f044067b6fed9b64212d1c62cd78e31c321590727f4a6a5c439c3c3b768c122cc808ed82cce98
6
+ metadata.gz: 721f12023eea66b9eb26f86926d667b614b9b696865cdef97b05916b01f6635f0a830909b0653d7b78b37dcbbf56c4e26142d8b93685e98e805891b15c7f1263
7
+ data.tar.gz: 443d5b04f1849d595aec927d934c9638c4a7cd169305e368b0c451322680b89c95005dc093850976418ea6eeadc9daacbbdcf67a7dce9cec21c59c54ee3f1aa3
data/README.md CHANGED
@@ -89,7 +89,7 @@ Create file `config/initializers/tramway.rb`
89
89
90
90
```ruby
91
91
::Tramway::Api.auth_config = { user_model: User, auth_attributes: %i[email username] }
92
- ::Tramway::Api.set_available_models user: %i[create update]
92
+ ::Tramway::Api.set_available_models user: { open: %i[create], closed: %i[update] }
93
93
```
94
94
95
95
Run `rails g tramway:core:install`
@@ -271,7 +271,10 @@ this model must have field `password_digest`, because we use `bcrypt` gem for au
271
271
272
272
Sets ActiveRecord models which will be used in API
273
273
274
- Argument is a hash. Keys are underscored models names, values are arrays of available methods for every model.
274
+ Argument is a hash. Keys are underscored models names, values are hashes with actions of available methods for every model.
275
+ * `open` key means that this action will be used without authentication
276
+ * `closed` key means that this action will be used with authentication
277
+
275
278
276
279
Enabled methods:
277
280
data/app/controllers/tramway/api/application_controller.rb CHANGED
@@ -25,7 +25,7 @@ module Tramway
25
25
if entity.respond_to? :to_token_payload
26
26
::Knock::AuthToken.new payload: entity.to_token_payload
27
27
else
28
- ::Knock::AuthToken.new payload: { sub: entity.id }
28
+ ::Knock::AuthToken.new payload: { sub: entity.uid }
29
29
end
30
30
end
31
31
data/app/controllers/tramway/api/v1/records_controller.rb CHANGED
@@ -2,7 +2,7 @@ module Tramway::Api::V1
2
2
class RecordsController < ::Tramway::Api::V1::ApplicationController
3
3
before_action :check_available_model_class
4
4
before_action :check_available_model_action
5
- before_action :authenticate_user
5
+ before_action :authenticate_user_if_needed
6
6
7
7
def index
8
8
records = model_class.active.order(id: :desc).send params[:scope] || :all
@@ -37,6 +37,7 @@ module Tramway::Api::V1
37
37
record = model_class.active.find params[:id]
38
38
render json: record,
39
39
serializer: serializer_class,
40
+ include: '*',
40
41
status: :ok
41
42
end
42
43
@@ -55,7 +56,15 @@ module Tramway::Api::V1
55
56
end
56
57
57
58
def check_available_model_action
58
- head :unprocessable_entity and return unless action_name.in? Tramway::Api.available_models[model_class.to_s].map(&:to_s)
59
+ open_actions = Tramway::Api.available_models[model_class.to_s][:open]&.map(&:to_s) || []
60
+ closed_actions = Tramway::Api.available_models[model_class.to_s][:closed]&.map(&:to_s) || []
61
+ head :unprocessable_entity and return unless action_name.in? open_actions + closed_actions
62
+ end
63
+
64
+ def authenticate_user_if_needed
65
+ if action_name.in? Tramway::Api::available_models[model_class.to_s][:closed]&.map(&:to_s) || []
66
+ authenticate_user
67
+ end
59
68
end
60
69
61
70
def model_class
data/app/controllers/tramway/api/v1/users_controller.rb CHANGED
@@ -8,7 +8,7 @@ class Tramway::Api::V1::UsersController < ::Tramway::Api::V1::ApplicationControl
8
8
user_form = sign_up_form_class_name(Tramway::Api.user_based_model).new Tramway::Api.user_based_model.new
9
9
# Implement JSON API spec here
10
10
if user_form.submit params[Tramway::Api.user_based_model.name.underscore]
11
- token = ::Knock::AuthToken.new(payload: { sub: user_form.model.id }).token
11
+ token = ::Knock::AuthToken.new(payload: { sub: user_form.model.uid }).token
12
12
# FIXME refactor this bullshit
13
13
serialized_user = OpenStruct.new(
14
14
user_form.model.attributes.merge(
data/lib/tramway/api/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
module Tramway
2
2
module Api
3
- VERSION = '1.3'
3
+ VERSION = '1.4.2'
4
4
end
5
5
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.3'
4
+ version: 1.4.2
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-09-07 00:00:00.000000000 Z
11
+ date: 2019-09-11 00:00:00.000000000 Z
12
12
dependencies:
13
13
- !ruby/object:Gem::Dependency
14
14
name: knock