itg 0.1.8 → 0.1.9

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: 3d053cf4b8d8c39243e96253436bb4287d51c0f05c350e4974274547a3054ccd
4
- data.tar.gz: ca430ed912df96c4d35439f78b40b4a041649430f3fdeecb13f29440a9e4a6f1
3
+ metadata.gz: c60773e9c8ca502b677976a3dcc42507f2a380c656aaa428234926cade8089c3
4
+ data.tar.gz: f7b47a3d333b4213c025184f735d2c4db202c2b12baaf23d582b902180cdad64
5
5
  SHA512:
6
- metadata.gz: be3e9fa71c749a1413158cdf1152d75bd2a6891bce296bb337ae86a005132df6d5b0113ced3750130e764006f3236e5f48f9cec41d0b6a73d0ec6958e05d9535
7
- data.tar.gz: 9e59e76d5deea88cd5d5ce33a3538288411974623852ff8c0f45249a4e12bd9d15ff1df6114be98c6ffbf4891ff03c121d1169c81da3ccf5b1dc0ac66ee6845d
6
+ metadata.gz: 3747664586c161f37074adba3f32a6b4f2309c2eb2b26e06f9209926d06b98f107b351a81e09c465a4b041b3d29331ecad623354187f92b4d94ecd036a0599a4
7
+ data.tar.gz: 7d63370e7fc1f8169cfe8baa69263aefde490f21ee3737f5506da5520873d15ae3c9624b974142bc0255b7f66e5555678356c0aa6483d011c8f4da47e1fb0455
@@ -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,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_permited_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,308 @@
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_permited_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
+ end
19
+
20
+ def index
21
+ puts "[GenericController.index] current_bearer: #{@current_bearer}, current_api_key: #{@current_api_key}"
22
+ puts "[GenericController.index] current_bearer.db: #{@current_bearer.db}, current_bearer.kind: #{@current_bearer.kind}"
23
+ puts "[GenericController.index] curr_user_db: #{curr_user_db}, params[:filter]: #{params[:filter]}"
24
+ pp "*** Api::V1::Cultivation::Chamber.count: #{Api::V1::Cultivation::Chamber.count}"
25
+ ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.index] params: #{params}\n params[:filter]: #{params[:filter]}\n db: #{@g_model_class.database_name}\n=========================="
26
+ collection = get_collection
27
+ # data = if (filter = params[:filter])
28
+ # collection.where(@g_search_field => /.*#{filter}.*/i)
29
+ # elsif (view_name = params[:view])
30
+ # # collection.fetch_view(view_name)
31
+ # collection.respond_to?(view_name) ? collection.send(view_name) : "View '#{view_name}' does not exist for collection '#{collection}'"
32
+ # else
33
+ # collection.all
34
+ # end
35
+ data = if (filter = params[:filter])
36
+ puts '----- with filter'
37
+ collection.where(@g_search_field => /.*#{filter}.*/i)
38
+ else
39
+ puts '----- all'
40
+ # pp collection.with(curr_user_db).all.count
41
+ pp collection.all.count
42
+ pp "*** Api::V1::Cultivation::Chamber.count: #{Api::V1::Cultivation::Chamber.count}"
43
+ collection.all
44
+ # Api::V1::Link.with(database: curr_user_db) { |klass| klass.all.to_a }
45
+ # collection.with(database: curr_user_db) { |klass| klass.all.to_a }
46
+ # Api::V1::Link.with(database: curr_user_db) do |klass|
47
+ # # klass.create!(title: 'tetstttt', url: 'ttttt', owner: @current_bearer);
48
+ # klass.all
49
+ # end
50
+ end
51
+ puts ">>>> collection '#{collection}' data (#{data.count}): #{data}"
52
+ if (view_name = params[:view])
53
+ data = data.respond_to?(view_name) ? data.send(view_name) : "View '#{view_name}' does not exist for collection '#{collection}'"
54
+ end
55
+ # ITG_LOGGER.info ">>> params[:limit]: #{params[:limit]}, data: #{data}"
56
+ if (limit = params[:limit]) && !data.is_a?(String)
57
+ # ITG_LOGGER.info ">>> limit: #{limit}, data.count (bef): #{data.count}"
58
+ data = data.limit(limit)
59
+ # ITG_LOGGER.info ">>> limit: #{limit}, data.count (aft): #{data.count}"
60
+ # TODO: data.count does not return the correct value! Why?????
61
+ end
62
+ # data = params[:filter] ? collection.where(@g_search_field => /.*#{params[:filter]}.*/i) : collection.all
63
+ # ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.index] data to return (#{data.count}):\n#{JSON.pretty_generate data}\n============================"
64
+ # render json: params[:filter] ? @g_model_class.where(@g_search_field => /.*#{params[:filter]}.*/i) : @g_model_class.all
65
+ puts '>>>>>>> [GenericController.index] ....'
66
+ puts ">>>>>>> [GenericController.index] data (#{data.count if data})"
67
+ pp data
68
+ pp data
69
+ render json: data
70
+ end
71
+
72
+ def show
73
+ puts '>>>>>>> [GenericController.show] ...'
74
+ ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.show] params: #{params}\n params[:id]: #{params[:id]}\n db: #{@g_model_class.database_name}\n========================="
75
+ collection = get_collection
76
+ data = collection.find(params[:id])
77
+ ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.show] data to return:\n#{JSON.pretty_generate data}\n============================"
78
+ puts ">>>>>>> [GenericController.show] collection: #{collection}"
79
+ pp data
80
+ if data
81
+ render json: data
82
+ else
83
+ json_response("Couldn't find #{@g_model_class} with 'id'=#{params[:id]}", :not_found)
84
+ end
85
+ end
86
+
87
+ def create
88
+ puts '>>>>>>> [GenericController.create] ...'
89
+ ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.create] params:\n#{JSON.pretty_generate params}\n db: #{@g_model_class.database_name}\n============================"
90
+ # g_check_variables
91
+ # # ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.create] model_instance_params:\n#{JSON.pretty_generate model_instance_params}\n============================"
92
+ # @model_instance = @g_model_class.create!(model_instance_params)
93
+ collection = get_collection
94
+
95
+ # @model_instance = collection.create!(model_instance_params(true))
96
+ # @model_instance = collection.create(model_instance_params(true))
97
+ @model_instance = collection.new(model_instance_params(true))
98
+ @model_instance.owner = @current_bearer if @model_instance.respond_to?(:owner)
99
+ # puts ">>>> @model_instance: #{@model_instance}"
100
+ # json_response(@model_instance, :created)
101
+ # puts ">>>>>>> create...."
102
+ # pp @model_instance
103
+ if @model_instance.save
104
+ # render json: @model_instance, status: :created, location: @model_instance
105
+ json_response(@model_instance, :created)
106
+ else
107
+ # render json: @model_instance.errors, status: :unprocessable_entity
108
+ json_response(@model_instance.errors, :unprocessable_entity)
109
+ end
110
+
111
+ # @post = Post.new(post_params)
112
+ #
113
+ # if @post.save
114
+ # render json: @post, status: :created, location: @post
115
+ # else
116
+ # render json: @post.errors, status: :unprocessable_entity
117
+ # end
118
+
119
+ # ITG_LOGGER.info "======== EventsController create ======= params:\n#{JSON.pretty_generate params}\n======================="
120
+ # @event = Event.new(event_params)
121
+ # respond_to do |format|
122
+ # if @event.save
123
+ # format.json { render :show, status: :created, location: @event }
124
+ # else
125
+ # format.json { render json: @event.errors, status: :unprocessable_entity }
126
+ # end
127
+ # end
128
+ end
129
+
130
+ def update
131
+ puts '>>>>>>> [GenericController.update] ...'
132
+ ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.update] params:\n#{JSON.pretty_generate params}\n db: #{@g_model_class.database_name}\n============================"
133
+ g_check_variables
134
+ # @model_instance.update(model_instance_params)
135
+ # head :no_content
136
+
137
+ @model_instance.owner = @current_bearer if @model_instance.respond_to?(:owner)
138
+ if @model_instance
139
+ if @model_instance.update(model_instance_params)
140
+ # head :no_content
141
+ json_response(@model_instance)
142
+ else
143
+ json_response(@model_instance.errors, :unprocessable_entity)
144
+ end
145
+ else
146
+ json_response("Couldn't find #{@g_model_class} with 'id'=#{params[:id]}", :not_found)
147
+ end
148
+
149
+ # if @post.update(post_params)
150
+ # render json: @post
151
+ # else
152
+ # render json: @post.errors, status: :unprocessable_entity
153
+ # end
154
+ end
155
+
156
+ def destroy
157
+ g_check_variables
158
+ puts "*** [GenericController/#{@g_model_class}.destroy] params:\n#{JSON.pretty_generate params}\n db: #{@g_model_class.database_name}\n============================"
159
+ ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.destroy] params:\n#{JSON.pretty_generate params}\n db: #{@g_model_class.database_name}\n============================"
160
+ if @model_instance
161
+ @model_instance.destroy
162
+ head :no_content
163
+ else
164
+ json_response("Couldn't find #{@g_model_class} with 'id'=#{params[:id]}", :not_found)
165
+ end
166
+ end
167
+
168
+ private
169
+
170
+ def switch_database
171
+ puts ">>> [GenericController.switch_database] curr_user_db: #{curr_user_db}"
172
+ # if params.has_key? 'test'
173
+ # # db_name = "md-test"
174
+ # db_name = 'itg_api_test'
175
+ # else
176
+ # db_name = 'md'
177
+ # end
178
+ # Mongoid.override_database(db_name)
179
+ Mongoid.override_database(curr_user_db)
180
+ end
181
+
182
+ def reset_database
183
+ Mongoid.override_database(nil)
184
+ end
185
+
186
+ def g_check_variables
187
+ ITG_LOGGER.info '*** [GenericController.g_check_variables] ...'
188
+ raise '[GenericController] @g_model_class is nil!' unless @g_model_class
189
+ raise "[GenericController/#{@g_model_class}] @g_search_field is nil!" unless @g_search_field
190
+ raise "[GenericController/#{@g_model_class}] @g_permited_params is nil!" unless @g_permited_params
191
+ end
192
+
193
+ def g_model_class
194
+ ITG_LOGGER.info '*** [GenericController.g_model_class] ...'
195
+ @g_model_class ||= nil
196
+ end
197
+
198
+ def g_parent_model_class
199
+ ITG_LOGGER.info '*** [GenericController.g_parent_model_class] ...'
200
+ @g_parent_model_class ||= nil
201
+ end
202
+
203
+ def g_search_field
204
+ ITG_LOGGER.info '*** [GenericController.g_search_field] ...'
205
+ @g_search_field ||= nil
206
+ end
207
+
208
+ def g_permited_params
209
+ ITG_LOGGER.info '*** [GenericController.g_permited_params] ...'
210
+ @g_permited_params ||= nil
211
+ end
212
+
213
+ def g_only_show
214
+ ITG_LOGGER.info '*** [GenericController.g_only_show] ...'
215
+ @g_only_show ||= false
216
+ end
217
+
218
+ # Issue: when this is called, the child controller does not have set the g_ variables...
219
+ # This must be run in the child coltroller...
220
+ # def set_model_instance
221
+ # ITG_LOGGER.info "*** [GenericController.set_model_instance] ..."
222
+ # @model_instance = @g_model_class.find(params[:id])
223
+ # end
224
+
225
+ def model_instance_params(create = false)
226
+ puts '>>>>>>> [GenericController.model_instance_params] ...'
227
+ ITG_LOGGER.info '*** [GenericController.model_instance_params] ...'
228
+ ITG_LOGGER.info ">>>>> params: #{params}"
229
+ ITG_LOGGER.info ">>>>> @g_model_class: #{@g_model_class}"
230
+ # params_model_sym = @g_model_class.name.parameterize.underscore.to_sym
231
+ params_model_sym = @g_model_class.name.underscore.gsub('/', '_').to_sym
232
+ ITG_LOGGER.info ">>>>> params_model_sym: #{params_model_sym}"
233
+ ITG_LOGGER.info ">>>>> @g_permited_params: #{@g_permited_params}"
234
+ # ITG_LOGGER.info "*** [GenericController.model_instance_params] g_permited_params:\n#{@g_permited_params}"
235
+ # ret = params.require(@g_model_class.name.underscore.to_sym).permit(*@g_permited_params)
236
+
237
+ case @g_permited_params
238
+ when Array
239
+ ret = params.require(params_model_sym).permit(*@g_permited_params)
240
+ when Hash
241
+ ret = params.require(params_model_sym).permit(*@g_permited_params, **@g_permited_params)
242
+ else
243
+ raise "[GenericController.model_instance_params] unhandled @g_permited_params: #{@g_permited_params.inspect}"
244
+ end
245
+
246
+ ITG_LOGGER.info "*** [GenericController.model_instance_params] return:\n#{ret}"
247
+ if create
248
+ # ITG_LOGGER.info "*** [GenericController.model_instance_params] @g_model_class.fields: #{@g_model_class.fields}"
249
+ ret.each do |k, v|
250
+ ITG_LOGGER.info "*** [GenericController.model_instance_params] k:#{k}, v:#{v}"
251
+ field = @g_model_class.fields[k]
252
+ raise "[GenericController.model_instance_params] field #{k} does not exists in fields..." if field.nil?
253
+ # ITG_LOGGER.info "*** [GenericController.model_instance_params] field: #{field.inspect}"
254
+ # ITG_LOGGER.info "*** [GenericController.model_instance_params] field.options: #{field.options.inspect}"
255
+ # ITG_LOGGER.info "*** [GenericController.model_instance_params] field.options[:type]: #{field.options[:type].inspect}, field.default_val: #{field.default_val.inspect}"
256
+ ret.delete(k) if v == '' and !field.default_val.nil?
257
+ end
258
+ ITG_LOGGER.info "*** [GenericController.model_instance_params] (create) changed return:\n#{ret}"
259
+ end
260
+ ret
261
+ end
262
+
263
+ def get_collection
264
+ puts "*** [GenericController/#{@g_model_class}.get_collection] params: #{params}"
265
+ ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.get_collection] params: #{params}"
266
+ g_check_variables
267
+ if @g_parent_model_class
268
+ ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.index] With parent: #{@g_parent_model_class}"
269
+ puts "*** [GenericController/#{@g_model_class}.index] With parent: #{@g_parent_model_class}"
270
+ collection_name = @g_model_class.to_s.parameterize.pluralize
271
+ parent_field_id = @g_parent_model_class.to_s.parameterize + '_id'
272
+ parent_id = params[parent_field_id.to_sym]
273
+ raise "[GenericController/#{@g_model_class}.index] Parent id is empty!" if parent_id.nil? || parent_id.empty?
274
+ ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.index] collection_name: #{collection_name}, parent_field_id: #{parent_field_id}, parent_id: #{parent_id}"
275
+ parent_rec = @g_parent_model_class.find(parent_id)
276
+ raise "[GenericController/#{@g_model_class}.index] Parent record for id '#{parent_id}' was not found!" unless parent_rec
277
+ if parent_rec.respond_to?(collection_name.to_sym)
278
+ collection = parent_rec.send(collection_name.to_sym)
279
+ ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.index] collection: #{collection}"
280
+ else
281
+ raise "[GenericController/#{@g_model_class}.index] Parent record '#{parent_rec}' does not has '#{collection_name}' method!"
282
+ end
283
+ else
284
+ ITG_LOGGER.info "*** [GenericController/#{@g_model_class}.index] NO parent...."
285
+ puts "*** [GenericController/#{@g_model_class}.index] NO parent...."
286
+ collection = @g_model_class
287
+ # data = params[:filter] ? @g_model_class.where(@g_search_field => /.*#{params[:filter]}.*/i) : @g_model_class.all
288
+ end
289
+ collection
290
+ end
291
+
292
+ def require_permission
293
+ if @g_model_class.attribute_names.include?('owner_id')
294
+ puts "******** generic_controller#require_permission @model_instance.owner: #{@model_instance&.owner}, @model_instance: #{@model_instance}"
295
+ if @model_instance && @model_instance.owner_id != @current_bearer.id
296
+ json_response('Not allowed!', :forbidden)
297
+ end
298
+ else
299
+ puts "******** generic_controller#require_permission @current_bearer: #{@current_bearer.inspect}, @model_instance: #{@model_instance.inspect}"
300
+ unless @current_bearer.kind == 'one'
301
+ json_response('Not allowed!', :forbidden)
302
+ end
303
+ end
304
+ end
305
+
306
+ end
307
+ end
308
+ end
data/lib/itg/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Itg
4
- VERSION = "0.1.8"
4
+ VERSION = "0.1.9"
5
5
  DATE = "18/2/2024"
6
6
 
7
7
  def self.version_info
data/lib/itg.rb CHANGED
@@ -10,6 +10,9 @@ 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/controllers/itg_generic_controller_base"
14
+ require_relative "itg/controllers/itg_api_keys_controller_base"
15
+ require_relative "itg/controllers/itg_entities_controller_base"
13
16
  require_relative "itg/itg_sec"
14
17
 
15
18
  module Itg
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - aAon
@@ -29,6 +29,9 @@ 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_entities_controller_base.rb
34
+ - lib/itg/controllers/itg_generic_controller_base.rb
32
35
  - lib/itg/itg_api_key_authenticatable.rb
33
36
  - lib/itg/itg_mongo_base.rb
34
37
  - lib/itg/itg_printable.rb