itg 0.1.8 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -1
- data/lib/itg/controllers/itg_api_keys_controller_base.rb +76 -0
- data/lib/itg/controllers/itg_controller_set_current_request_details.rb +18 -0
- data/lib/itg/controllers/itg_entities_controller_base.rb +43 -0
- data/lib/itg/controllers/itg_generic_controller_base.rb +309 -0
- data/lib/itg/itg_api_key_authenticatable.rb +2 -0
- data/lib/itg/itg_current_base.rb +27 -0
- data/lib/itg/itg_printable.rb +2 -1
- data/lib/itg/itg_sec.rb +3 -3
- data/lib/itg/models/itg_context_model_base.rb +11 -1
- data/lib/itg/models/itg_entity_model_base.rb +3 -1
- data/lib/itg/models/itg_user_model_base.rb +14 -13
- data/lib/itg/version.rb +2 -2
- data/lib/itg.rb +5 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe781750f5aa0333312d6a9fd50522d349b25d4c33459c82e9db8417c60e2ab1
|
4
|
+
data.tar.gz: 112e6866a3e64dfa33ecc755d69c40d44c4f460e8b91a8bbf0a3a19e14daacf7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 569b0ef8c9e406792bbe4491bf841ce1558d26b6cfa206395b4d62368170a2741048f527cde8406666f973cbf2ad9b262fbd4e4cee3d54f53d3a2f0d00c1746e
|
7
|
+
data.tar.gz: 20df9342f243bb409fd467c97ef55a781abc3e1198142739488603c1625a81a485603da738131b684ea8c4bb388a49caa88665e72829b731763f79389cd7b4cb
|
data/README.md
CHANGED
@@ -1,4 +1,22 @@
|
|
1
|
-
# Itg
|
1
|
+
# Itg gem
|
2
|
+
|
3
|
+
### Context
|
4
|
+
|
5
|
+
The `Context` describes the isolated storage for the users data
|
6
|
+
|
7
|
+
Every time a user can see and manage only the data of a single `context` in which he has access
|
8
|
+
|
9
|
+
User has a list of contexts (memberships) where has access.
|
10
|
+
Each `context membership` has: `context`, `user`, `access` (`ro`, `rw`, `admin`)
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
2
20
|
|
3
21
|
TODO: Delete this and the text below, and describe your gem
|
4
22
|
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Itg
|
4
|
+
module ApiKeysControllerBase
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include Itg::ApiKeyAuthenticatable
|
9
|
+
|
10
|
+
# Require API key authentication
|
11
|
+
prepend_before_action :authenticate_with_api_key!, only: %i[index destroy]
|
12
|
+
before_action :switch_database
|
13
|
+
|
14
|
+
def index
|
15
|
+
render json: current_bearer.api_keys
|
16
|
+
end
|
17
|
+
|
18
|
+
def current
|
19
|
+
authenticate_with_http_basic do |email, password|
|
20
|
+
user = User.find_by email: email.downcase
|
21
|
+
|
22
|
+
# if user&.authenticate(password)
|
23
|
+
if user&.valid_password?(password)
|
24
|
+
data = {}
|
25
|
+
data['token'] = user.api_keys.create!(token: SecureRandom.hex).token
|
26
|
+
# data['token'] = if user.api_keys.empty?
|
27
|
+
# user.api_keys.create!(token: SecureRandom.hex).token
|
28
|
+
# else
|
29
|
+
# user.api_keys.first.token
|
30
|
+
# end
|
31
|
+
data['user'] = user
|
32
|
+
render json: data, status: :ok
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def create
|
38
|
+
ITG_LOGGER.info 'ApiKeysController - create....'
|
39
|
+
authenticate_with_http_basic do |email, password|
|
40
|
+
ITG_LOGGER.info 'ApiKeysController - create - bef find user'
|
41
|
+
user = User.find_by email: email.downcase
|
42
|
+
|
43
|
+
ITG_LOGGER.info 'ApiKeysController - create - bef auth user'
|
44
|
+
# if user&.authenticate(password)
|
45
|
+
if user&.valid_password?(password)
|
46
|
+
ITG_LOGGER.info 'ApiKeysController - create - user authenticated - create api_key'
|
47
|
+
api_key = user.api_keys.create! token: SecureRandom.hex
|
48
|
+
|
49
|
+
ITG_LOGGER.info 'ApiKeysController - create - user authenticated - return'
|
50
|
+
render json: api_key, status: :created and return
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
render status: :unauthorized
|
55
|
+
end
|
56
|
+
|
57
|
+
def destroy
|
58
|
+
api_key = current_bearer.api_keys.find(params[:id])
|
59
|
+
|
60
|
+
api_key.destroy
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def switch_database
|
66
|
+
db_name = if params.has_key? 'test'
|
67
|
+
# db_name = "md-test"
|
68
|
+
'itg_api_test'
|
69
|
+
else
|
70
|
+
'md'
|
71
|
+
end
|
72
|
+
# Mongoid.override_database(db_name)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Itg
|
2
|
+
module ControllerSetCurrentRequestDetails
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_action do
|
7
|
+
Current.request_id = request.uuid
|
8
|
+
Current.user_agent = request.user_agent
|
9
|
+
Current.ip_address = request.ip
|
10
|
+
Current.host = request.host
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class_methods do
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Itg
|
4
|
+
module EntitiesControllerBase
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include Itg::ApiKeyAuthenticatable
|
9
|
+
|
10
|
+
prepend_before_action :authenticate_with_api_key!
|
11
|
+
|
12
|
+
before_action :init
|
13
|
+
before_action :set_model_instance, only: [:show, :update, :destroy]
|
14
|
+
before_action :require_permission # from generic_controller, fix execution order
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def init
|
19
|
+
ITG_LOGGER.info '*** [EntitiesController.set_variables] ...'
|
20
|
+
# @g_model_class = Entity
|
21
|
+
@g_model_class = self.class.model_class
|
22
|
+
@g_search_field = 'kind'
|
23
|
+
@g_permitted_params = {kind: nil, tags: nil, context: nil, attrs: {}}
|
24
|
+
end
|
25
|
+
|
26
|
+
def set_model_instance
|
27
|
+
# puts '******** entities_controller#set_model_instance......'
|
28
|
+
ITG_LOGGER.info '*** [Entities.set_model_instance] ...'
|
29
|
+
@model_instance = @g_model_class.find(params[:id])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class_methods do
|
34
|
+
attr_reader :model_class
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def itg_entities_controller_base(model_class:)
|
39
|
+
@model_class = model_class
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,309 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Itg
|
4
|
+
module GenericControllerBase
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include Itg::Response
|
9
|
+
|
10
|
+
before_action :g_model_class, :g_parent_model_class, :g_search_field, :g_permitted_params
|
11
|
+
# before_action :set_model_instance, only: [:show, :update, :destroy]
|
12
|
+
before_action :require_permission
|
13
|
+
before_action :switch_database
|
14
|
+
after_action :reset_database
|
15
|
+
|
16
|
+
def curr_user_db
|
17
|
+
# @current_bearer ? @current_bearer.db : 'mainaaa'
|
18
|
+
# 'test'
|
19
|
+
end
|
20
|
+
|
21
|
+
def index
|
22
|
+
# puts "[GenericController.index] current_bearer: #{@current_bearer}, current_api_key: #{@current_api_key}"
|
23
|
+
# puts "[GenericController.index] current_bearer.db: #{@current_bearer.db}, current_bearer.kind: #{@current_bearer.kind}"
|
24
|
+
# puts "[GenericController.index] curr_user_db: #{curr_user_db}, params[:filter]: #{params[:filter]}"
|
25
|
+
# pp "*** Api::V1::Cultivation::Chamber.count: #{Api::V1::Cultivation::Chamber.count}"
|
26
|
+
ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.index] params: #{params}\n params[:filter]: #{params[:filter]}\n db: #{@g_model_class.database_name}\n=========================="
|
27
|
+
collection = get_collection
|
28
|
+
# data = if (filter = params[:filter])
|
29
|
+
# collection.where(@g_search_field => /.*#{filter}.*/i)
|
30
|
+
# elsif (view_name = params[:view])
|
31
|
+
# # collection.fetch_view(view_name)
|
32
|
+
# collection.respond_to?(view_name) ? collection.send(view_name) : "View '#{view_name}' does not exist for collection '#{collection}'"
|
33
|
+
# else
|
34
|
+
# collection.all
|
35
|
+
# end
|
36
|
+
data = if (filter = params[:filter])
|
37
|
+
# puts '----- with filter'
|
38
|
+
collection.where(@g_search_field => /.*#{filter}.*/i)
|
39
|
+
else
|
40
|
+
# puts '----- all'
|
41
|
+
# pp collection.with(curr_user_db).all.count
|
42
|
+
# pp collection.all.count
|
43
|
+
# pp "*** Api::V1::Cultivation::Chamber.count: #{Api::V1::Cultivation::Chamber.count}"
|
44
|
+
collection.all
|
45
|
+
# Api::V1::Link.with(database: curr_user_db) { |klass| klass.all.to_a }
|
46
|
+
# collection.with(database: curr_user_db) { |klass| klass.all.to_a }
|
47
|
+
# Api::V1::Link.with(database: curr_user_db) do |klass|
|
48
|
+
# # klass.create!(title: 'tetstttt', url: 'ttttt', owner: @current_bearer);
|
49
|
+
# klass.all
|
50
|
+
# end
|
51
|
+
end
|
52
|
+
# puts ">>>> collection '#{collection}' data (#{data.count}): #{data}"
|
53
|
+
if (view_name = params[:view])
|
54
|
+
data = data.respond_to?(view_name) ? data.send(view_name) : "View '#{view_name}' does not exist for collection '#{collection}'"
|
55
|
+
end
|
56
|
+
# ITG_LOGGER.info ">>> params[:limit]: #{params[:limit]}, data: #{data}"
|
57
|
+
if (limit = params[:limit]) && !data.is_a?(String)
|
58
|
+
# ITG_LOGGER.info ">>> limit: #{limit}, data.count (bef): #{data.count}"
|
59
|
+
data = data.limit(limit)
|
60
|
+
# ITG_LOGGER.info ">>> limit: #{limit}, data.count (aft): #{data.count}"
|
61
|
+
# TODO: data.count does not return the correct value! Why?????
|
62
|
+
end
|
63
|
+
# data = params[:filter] ? collection.where(@g_search_field => /.*#{params[:filter]}.*/i) : collection.all
|
64
|
+
# ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.index] data to return (#{data.count}):\n#{JSON.pretty_generate data}\n============================"
|
65
|
+
# render json: params[:filter] ? @g_model_class.where(@g_search_field => /.*#{params[:filter]}.*/i) : @g_model_class.all
|
66
|
+
# puts '>>>>>>> [GenericController.index] ....'
|
67
|
+
# puts ">>>>>>> [GenericController.index] data (#{data.count if data})"
|
68
|
+
# pp data
|
69
|
+
# pp data
|
70
|
+
render json: data
|
71
|
+
end
|
72
|
+
|
73
|
+
def show
|
74
|
+
# puts '>>>>>>> [GenericController.show] ...'
|
75
|
+
ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.show] params: #{params}\n params[:id]: #{params[:id]}\n db: #{@g_model_class.database_name}\n========================="
|
76
|
+
collection = get_collection
|
77
|
+
data = collection.find(params[:id])
|
78
|
+
ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.show] data to return:\n#{JSON.pretty_generate data.to_s}\n============================"
|
79
|
+
# puts ">>>>>>> [GenericController.show] collection: #{collection}"
|
80
|
+
# pp data
|
81
|
+
if data
|
82
|
+
render json: data
|
83
|
+
else
|
84
|
+
json_response("Couldn't find #{@g_model_class} with 'id'=#{params[:id]}", :not_found)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def create
|
89
|
+
# puts '>>>>>>> [GenericController.create] ...'
|
90
|
+
ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.create] params:\n#{JSON.pretty_generate params}\n db: #{@g_model_class.database_name}\n============================"
|
91
|
+
# g_check_variables
|
92
|
+
# # ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.create] model_instance_params:\n#{JSON.pretty_generate model_instance_params}\n============================"
|
93
|
+
# @model_instance = @g_model_class.create!(model_instance_params)
|
94
|
+
collection = get_collection
|
95
|
+
|
96
|
+
# @model_instance = collection.create!(model_instance_params(true))
|
97
|
+
# @model_instance = collection.create(model_instance_params(true))
|
98
|
+
@model_instance = collection.new(model_instance_params(true))
|
99
|
+
@model_instance.owner = @current_bearer if @model_instance.respond_to?(:owner)
|
100
|
+
# puts ">>>> @model_instance: #{@model_instance}"
|
101
|
+
# json_response(@model_instance, :created)
|
102
|
+
# puts ">>>>>>> create...."
|
103
|
+
# pp @model_instance
|
104
|
+
if @model_instance.save
|
105
|
+
# render json: @model_instance, status: :created, location: @model_instance
|
106
|
+
json_response(@model_instance, :created)
|
107
|
+
else
|
108
|
+
# render json: @model_instance.errors, status: :unprocessable_entity
|
109
|
+
json_response(@model_instance.errors, :unprocessable_entity)
|
110
|
+
end
|
111
|
+
|
112
|
+
# @post = Post.new(post_params)
|
113
|
+
#
|
114
|
+
# if @post.save
|
115
|
+
# render json: @post, status: :created, location: @post
|
116
|
+
# else
|
117
|
+
# render json: @post.errors, status: :unprocessable_entity
|
118
|
+
# end
|
119
|
+
|
120
|
+
# ITG_LOGGER.info "======== EventsController create ======= params:\n#{JSON.pretty_generate params}\n======================="
|
121
|
+
# @event = Event.new(event_params)
|
122
|
+
# respond_to do |format|
|
123
|
+
# if @event.save
|
124
|
+
# format.json { render :show, status: :created, location: @event }
|
125
|
+
# else
|
126
|
+
# format.json { render json: @event.errors, status: :unprocessable_entity }
|
127
|
+
# end
|
128
|
+
# end
|
129
|
+
end
|
130
|
+
|
131
|
+
def update
|
132
|
+
# puts '>>>>>>> [GenericController.update] ...'
|
133
|
+
ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.update] params:\n#{JSON.pretty_generate params}\n db: #{@g_model_class.database_name}\n============================"
|
134
|
+
g_check_variables
|
135
|
+
# @model_instance.update(model_instance_params)
|
136
|
+
# head :no_content
|
137
|
+
|
138
|
+
@model_instance.owner = @current_bearer if @model_instance.respond_to?(:owner)
|
139
|
+
if @model_instance
|
140
|
+
if @model_instance.update(model_instance_params)
|
141
|
+
# head :no_content
|
142
|
+
json_response(@model_instance)
|
143
|
+
else
|
144
|
+
json_response(@model_instance.errors, :unprocessable_entity)
|
145
|
+
end
|
146
|
+
else
|
147
|
+
json_response("Couldn't find #{@g_model_class} with 'id'=#{params[:id]}", :not_found)
|
148
|
+
end
|
149
|
+
|
150
|
+
# if @post.update(post_params)
|
151
|
+
# render json: @post
|
152
|
+
# else
|
153
|
+
# render json: @post.errors, status: :unprocessable_entity
|
154
|
+
# end
|
155
|
+
end
|
156
|
+
|
157
|
+
def destroy
|
158
|
+
g_check_variables
|
159
|
+
# puts "*** [GenericController/#{@g_model_class}.destroy] params:\n#{JSON.pretty_generate params}\n db: #{@g_model_class.database_name}\n============================"
|
160
|
+
ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.destroy] params:\n#{JSON.pretty_generate params}\n db: #{@g_model_class.database_name}\n============================"
|
161
|
+
if @model_instance
|
162
|
+
@model_instance.destroy
|
163
|
+
head :no_content
|
164
|
+
else
|
165
|
+
json_response("Couldn't find #{@g_model_class} with 'id'=#{params[:id]}", :not_found)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
private
|
170
|
+
|
171
|
+
def switch_database
|
172
|
+
# puts ">>> [GenericController.switch_database] curr_user_db: #{curr_user_db}"
|
173
|
+
# if params.has_key? 'test'
|
174
|
+
# # db_name = "md-test"
|
175
|
+
# db_name = 'itg_api_test'
|
176
|
+
# else
|
177
|
+
# db_name = 'md'
|
178
|
+
# end
|
179
|
+
# Mongoid.override_database(db_name)
|
180
|
+
Mongoid.override_database(curr_user_db)
|
181
|
+
end
|
182
|
+
|
183
|
+
def reset_database
|
184
|
+
Mongoid.override_database(nil)
|
185
|
+
end
|
186
|
+
|
187
|
+
def g_check_variables
|
188
|
+
ITG_LOGGER.info '*** [GenericController.g_check_variables] ...'
|
189
|
+
raise '[GenericController] @g_model_class is nil!' unless @g_model_class
|
190
|
+
raise "[GenericController/#{@g_model_class}] @g_search_field is nil!" unless @g_search_field
|
191
|
+
raise "[GenericController/#{@g_model_class}] @g_permitted_params is nil!" unless @g_permitted_params
|
192
|
+
end
|
193
|
+
|
194
|
+
def g_model_class
|
195
|
+
ITG_LOGGER.info '*** [GenericController.g_model_class] ...'
|
196
|
+
@g_model_class ||= nil
|
197
|
+
end
|
198
|
+
|
199
|
+
def g_parent_model_class
|
200
|
+
ITG_LOGGER.info '*** [GenericController.g_parent_model_class] ...'
|
201
|
+
@g_parent_model_class ||= nil
|
202
|
+
end
|
203
|
+
|
204
|
+
def g_search_field
|
205
|
+
ITG_LOGGER.info '*** [GenericController.g_search_field] ...'
|
206
|
+
@g_search_field ||= nil
|
207
|
+
end
|
208
|
+
|
209
|
+
def g_permitted_params
|
210
|
+
ITG_LOGGER.info '*** [GenericController.g_permitted_params] ...'
|
211
|
+
@g_permitted_params ||= nil
|
212
|
+
end
|
213
|
+
|
214
|
+
def g_only_show
|
215
|
+
ITG_LOGGER.info '*** [GenericController.g_only_show] ...'
|
216
|
+
@g_only_show ||= false
|
217
|
+
end
|
218
|
+
|
219
|
+
# Issue: when this is called, the child controller does not have set the g_ variables...
|
220
|
+
# This must be run in the child coltroller...
|
221
|
+
# def set_model_instance
|
222
|
+
# ITG_LOGGER.info "*** [GenericController.set_model_instance] ..."
|
223
|
+
# @model_instance = @g_model_class.find(params[:id])
|
224
|
+
# end
|
225
|
+
|
226
|
+
def model_instance_params(create = false)
|
227
|
+
# puts '>>>>>>> [GenericController.model_instance_params] ...'
|
228
|
+
ITG_LOGGER.info '*** [GenericController.model_instance_params] ...'
|
229
|
+
ITG_LOGGER.info ">>>>> params: #{params}"
|
230
|
+
ITG_LOGGER.info ">>>>> @g_model_class: #{@g_model_class}"
|
231
|
+
# params_model_sym = @g_model_class.name.parameterize.underscore.to_sym
|
232
|
+
params_model_sym = @g_model_class.name.underscore.gsub('/', '_').to_sym
|
233
|
+
ITG_LOGGER.info ">>>>> params_model_sym: #{params_model_sym}"
|
234
|
+
ITG_LOGGER.info ">>>>> @g_permitted_params: #{@g_permitted_params}"
|
235
|
+
# ITG_LOGGER.info "*** [GenericController.model_instance_params] g_permitted_params:\n#{@g_permitted_params}"
|
236
|
+
# ret = params.require(@g_model_class.name.underscore.to_sym).permit(*@g_permitted_params)
|
237
|
+
|
238
|
+
case @g_permitted_params
|
239
|
+
when Array
|
240
|
+
ret = params.require(params_model_sym).permit(*@g_permitted_params)
|
241
|
+
when Hash
|
242
|
+
ret = params.require(params_model_sym).permit(*@g_permitted_params, **@g_permitted_params)
|
243
|
+
else
|
244
|
+
raise "[GenericController.model_instance_params] unhandled @g_permitted_params: #{@g_permitted_params.inspect}"
|
245
|
+
end
|
246
|
+
|
247
|
+
ITG_LOGGER.info "*** [GenericController.model_instance_params] return:\n#{ret}"
|
248
|
+
if create
|
249
|
+
# ITG_LOGGER.info "*** [GenericController.model_instance_params] @g_model_class.fields: #{@g_model_class.fields}"
|
250
|
+
ret.each do |k, v|
|
251
|
+
ITG_LOGGER.info "*** [GenericController.model_instance_params] k:#{k}, v:#{v}"
|
252
|
+
field = @g_model_class.fields[k]
|
253
|
+
raise "[GenericController.model_instance_params] field #{k} does not exists in fields..." if field.nil?
|
254
|
+
# ITG_LOGGER.info "*** [GenericController.model_instance_params] field: #{field.inspect}"
|
255
|
+
# ITG_LOGGER.info "*** [GenericController.model_instance_params] field.options: #{field.options.inspect}"
|
256
|
+
# ITG_LOGGER.info "*** [GenericController.model_instance_params] field.options[:type]: #{field.options[:type].inspect}, field.default_val: #{field.default_val.inspect}"
|
257
|
+
ret.delete(k) if v == '' and !field.default_val.nil?
|
258
|
+
end
|
259
|
+
ITG_LOGGER.info "*** [GenericController.model_instance_params] (create) changed return:\n#{ret}"
|
260
|
+
end
|
261
|
+
ret
|
262
|
+
end
|
263
|
+
|
264
|
+
def get_collection
|
265
|
+
# puts "*** [GenericController/#{@g_model_class}.get_collection] params: #{params}"
|
266
|
+
ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.get_collection] params: #{params}"
|
267
|
+
g_check_variables
|
268
|
+
if @g_parent_model_class
|
269
|
+
ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.index] With parent: #{@g_parent_model_class}"
|
270
|
+
# puts "*** [GenericController/#{@g_model_class}.index] With parent: #{@g_parent_model_class}"
|
271
|
+
collection_name = @g_model_class.to_s.parameterize.pluralize
|
272
|
+
parent_field_id = @g_parent_model_class.to_s.parameterize + '_id'
|
273
|
+
parent_id = params[parent_field_id.to_sym]
|
274
|
+
raise "[GenericController/#{@g_model_class}.index] Parent id is empty!" if parent_id.nil? || parent_id.empty?
|
275
|
+
ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.index] collection_name: #{collection_name}, parent_field_id: #{parent_field_id}, parent_id: #{parent_id}"
|
276
|
+
parent_rec = @g_parent_model_class.find(parent_id)
|
277
|
+
raise "[GenericController/#{@g_model_class}.index] Parent record for id '#{parent_id}' was not found!" unless parent_rec
|
278
|
+
if parent_rec.respond_to?(collection_name.to_sym)
|
279
|
+
collection = parent_rec.send(collection_name.to_sym)
|
280
|
+
ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.index] collection: #{collection}"
|
281
|
+
else
|
282
|
+
raise "[GenericController/#{@g_model_class}.index] Parent record '#{parent_rec}' does not has '#{collection_name}' method!"
|
283
|
+
end
|
284
|
+
else
|
285
|
+
ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.index] NO parent...."
|
286
|
+
# puts "*** [GenericController/#{@g_model_class}.index] NO parent...."
|
287
|
+
collection = @g_model_class
|
288
|
+
# data = params[:filter] ? @g_model_class.where(@g_search_field => /.*#{params[:filter]}.*/i) : @g_model_class.all
|
289
|
+
end
|
290
|
+
collection
|
291
|
+
end
|
292
|
+
|
293
|
+
def require_permission
|
294
|
+
if @g_model_class.attribute_names.include?('owner_id')
|
295
|
+
# puts "******** generic_controller#require_permission @model_instance.owner: #{@model_instance&.owner}, @model_instance: #{@model_instance}"
|
296
|
+
if @model_instance && @model_instance.owner_id != @current_bearer.id
|
297
|
+
json_response('Not allowed!', :forbidden)
|
298
|
+
end
|
299
|
+
else
|
300
|
+
# puts "******** generic_controller#require_permission @current_bearer: #{@current_bearer.inspect}, @model_instance: #{@model_instance.inspect}"
|
301
|
+
unless @current_bearer.kind == 'one'
|
302
|
+
json_response('Not allowed!', :forbidden)
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
end
|
308
|
+
end
|
309
|
+
end
|
@@ -15,11 +15,13 @@ module Itg
|
|
15
15
|
# code when API key authentication fails
|
16
16
|
def authenticate_with_api_key!
|
17
17
|
@current_bearer = authenticate_or_request_with_http_token(&method(:authenticator))
|
18
|
+
Current.user = @current_bearer
|
18
19
|
end
|
19
20
|
|
20
21
|
# Use this for optional API key authentication
|
21
22
|
def authenticate_with_api_key
|
22
23
|
@current_bearer = authenticate_with_http_token(&method(:authenticator))
|
24
|
+
Current.user = @current_bearer
|
23
25
|
end
|
24
26
|
|
25
27
|
private
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Itg
|
2
|
+
module CurrentBase
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
attribute :user
|
7
|
+
attribute :context
|
8
|
+
attribute :config
|
9
|
+
attribute :request_id, :user_agent, :ip_address, :host
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
ret_hash = {}
|
13
|
+
ret_hash[:user] = user.username if user
|
14
|
+
ret_hash[:request_id] = request_id if request_id
|
15
|
+
ret_hash[:user_agent] = user_agent if user_agent
|
16
|
+
ret_hash[:host] = host if host
|
17
|
+
ret_hash[:ip_address] = ip_address if ip_address
|
18
|
+
if ret_hash.empty?
|
19
|
+
"Current singleton is empty"
|
20
|
+
else
|
21
|
+
# ret_hash.to_a.join(', ')
|
22
|
+
"Current is #{ret_hash.inspect}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/itg/itg_printable.rb
CHANGED
data/lib/itg/itg_sec.rb
CHANGED
@@ -19,12 +19,12 @@ module Itg
|
|
19
19
|
|
20
20
|
included do
|
21
21
|
begin
|
22
|
-
puts ">>>>>> itg_sec - include it....."
|
22
|
+
# puts ">>>>>> itg_sec - include it....."
|
23
23
|
unless Rails.env.test?
|
24
24
|
store_in database: DB_NAME, client: CLIENT_NAME
|
25
|
-
puts "[itg_sec] set db details: #{DB_NAME}, #{CLIENT_NAME}"
|
25
|
+
# puts "[itg_sec] set db details: #{DB_NAME}, #{CLIENT_NAME}"
|
26
26
|
end
|
27
|
-
self.itg_print_db_info
|
27
|
+
# self.itg_print_db_info
|
28
28
|
rescue NameError => e
|
29
29
|
# Ignored - Used only because the gem does not have the Rails initialized!
|
30
30
|
raise if e.message != "uninitialized constant Itg::Sec::Rails"
|
@@ -11,10 +11,20 @@ module Itg
|
|
11
11
|
field :code, type: String
|
12
12
|
field :name, type: String
|
13
13
|
field :descr, type: String
|
14
|
-
field :
|
14
|
+
field :db_name, type: String
|
15
|
+
field :db_client, type: String, default: "default"
|
15
16
|
|
16
17
|
validates_presence_of(:code, :name)
|
17
18
|
validates_uniqueness_of :code
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
[code, name, descr]
|
22
|
+
end
|
23
|
+
|
24
|
+
# def itg_print(header: nil, prefix: '', allow_nested: true)
|
25
|
+
# puts header if header
|
26
|
+
# puts "#{prefix}#{[attrs['name'] || attrs['descr'] || attrs['code']]}"
|
27
|
+
# end
|
18
28
|
end
|
19
29
|
end
|
20
30
|
end
|
@@ -19,8 +19,10 @@ module Itg
|
|
19
19
|
|
20
20
|
validates_presence_of :kind
|
21
21
|
validates_presence_of :attrs
|
22
|
+
validates_presence_of :context
|
22
23
|
|
23
|
-
belongs_to :owner, class_name: 'User'
|
24
|
+
# belongs_to :owner, class_name: 'User'
|
25
|
+
belongs_to :owner, class_name: User
|
24
26
|
|
25
27
|
def to_s
|
26
28
|
attrs['name'] || attrs['descr'] || attrs['code']
|
@@ -44,9 +44,9 @@ module Itg
|
|
44
44
|
|
45
45
|
field :email, type: String
|
46
46
|
# field :password_digest, type: String
|
47
|
-
field :db, type: String
|
47
|
+
# field :db, type: String
|
48
48
|
field :kind, type: String, default: 'user'
|
49
|
-
field :contexts, type: Object, default: {}
|
49
|
+
# field :contexts, type: Object, default: {}
|
50
50
|
|
51
51
|
index({ email: 1 }, { unique: true, name: 'email_index' })
|
52
52
|
|
@@ -67,19 +67,20 @@ module Itg
|
|
67
67
|
super
|
68
68
|
end
|
69
69
|
|
70
|
-
def add_context(context, role: :user)
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
end
|
70
|
+
# def add_context(context, role: :user)
|
71
|
+
# if context.persisted?
|
72
|
+
# unless contexts.keys.include?(context.code.to_sym)
|
73
|
+
# contexts[context.code.to_sym] = {role: role.to_sym}
|
74
|
+
# contexts[context.code.to_sym][:name] = context[:name] if context[:name]
|
75
|
+
# contexts[context.code.to_sym][:descr] = context[:descr] if context[:descr]
|
76
|
+
# contexts[context.code.to_sym][:db] = context[:db] if context[:db]
|
77
|
+
# end
|
78
|
+
# end
|
79
|
+
# end
|
80
80
|
|
81
81
|
def to_s
|
82
|
-
attributes.symbolize_keys.slice(:_id, :email, :db, :role).to_s
|
82
|
+
# attributes.symbolize_keys.slice(:_id, :email, :db, :role).to_s
|
83
|
+
attributes.symbolize_keys.slice(:_id, :email, :role).to_s
|
83
84
|
end
|
84
85
|
|
85
86
|
def itg_print(header: nil, prefix: '', allow_nested: true)
|
data/lib/itg/version.rb
CHANGED
data/lib/itg.rb
CHANGED
@@ -10,6 +10,11 @@ require_relative "itg/itg_mongo_base"
|
|
10
10
|
require_relative "itg/itg_printable"
|
11
11
|
require_relative "itg/itg_api_key_authenticatable"
|
12
12
|
require_relative "itg/itg_response"
|
13
|
+
require_relative "itg/itg_current_base"
|
14
|
+
require_relative "itg/controllers/itg_generic_controller_base"
|
15
|
+
require_relative "itg/controllers/itg_api_keys_controller_base"
|
16
|
+
require_relative "itg/controllers/itg_entities_controller_base"
|
17
|
+
require_relative "itg/controllers/itg_controller_set_current_request_details"
|
13
18
|
require_relative "itg/itg_sec"
|
14
19
|
|
15
20
|
module Itg
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: itg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- aAon
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -29,7 +29,12 @@ files:
|
|
29
29
|
- config/mongoid.yml
|
30
30
|
- itg.gemspec
|
31
31
|
- lib/itg.rb
|
32
|
+
- lib/itg/controllers/itg_api_keys_controller_base.rb
|
33
|
+
- lib/itg/controllers/itg_controller_set_current_request_details.rb
|
34
|
+
- lib/itg/controllers/itg_entities_controller_base.rb
|
35
|
+
- lib/itg/controllers/itg_generic_controller_base.rb
|
32
36
|
- lib/itg/itg_api_key_authenticatable.rb
|
37
|
+
- lib/itg/itg_current_base.rb
|
33
38
|
- lib/itg/itg_mongo_base.rb
|
34
39
|
- lib/itg/itg_printable.rb
|
35
40
|
- lib/itg/itg_response.rb
|