itg 0.1.7 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/itg/controllers/itg_api_keys_controller_base.rb +76 -0
- data/lib/itg/controllers/itg_entities_controller_base.rb +43 -0
- data/lib/itg/controllers/itg_generic_controller_base.rb +308 -0
- data/lib/itg/{itg_api_key_base.rb → models/itg_api_key_model_base.rb} +1 -14
- data/lib/itg/models/itg_context_model_base.rb +20 -0
- data/lib/itg/models/itg_entity_model_base.rb +35 -0
- data/lib/itg/models/itg_user_model_base.rb +95 -0
- data/lib/itg/version.rb +1 -1
- data/lib/itg.rb +8 -3
- metadata +9 -3
- /data/lib/itg/{itg_model_base.rb → models/itg_model_base.rb} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c60773e9c8ca502b677976a3dcc42507f2a380c656aaa428234926cade8089c3
|
4
|
+
data.tar.gz: f7b47a3d333b4213c025184f735d2c4db202c2b12baaf23d582b902180cdad64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
@@ -2,22 +2,9 @@
|
|
2
2
|
|
3
3
|
# ItgApiKeyBase module
|
4
4
|
module Itg
|
5
|
-
module
|
5
|
+
module ApiKeyModelBase
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
# include Itg::MongoBase
|
11
|
-
# include Itg::Sec
|
12
|
-
# include Mongoid::Fields
|
13
|
-
#
|
14
|
-
# field :bearer_id, type: Integer
|
15
|
-
# field :bearer_type, type: String
|
16
|
-
# field :token_digest, type: String
|
17
|
-
#
|
18
|
-
# index({ bearer_id: 1, bearer_type: 1}, { name: "bearer_id_type_index" })
|
19
|
-
# index({ token_digest: 1 }, { unique: true, name: "token_digest_index" })
|
20
|
-
|
21
8
|
included do
|
22
9
|
include Itg::MongoBase
|
23
10
|
include Itg::Sec
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Itg
|
4
|
+
module ContextModelBase
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include Itg::MongoBase
|
9
|
+
include Itg::Sec
|
10
|
+
|
11
|
+
field :code, type: String
|
12
|
+
field :name, type: String
|
13
|
+
field :descr, type: String
|
14
|
+
field :db, type: String
|
15
|
+
|
16
|
+
validates_presence_of(:code, :name)
|
17
|
+
validates_uniqueness_of :code
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Itg
|
4
|
+
module EntityModelBase
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include Itg::MongoBase
|
9
|
+
|
10
|
+
store_in collection: 'entities'
|
11
|
+
|
12
|
+
# field :context, type: Array, default: [AppConfig::DEFAULT_CONTEXT_NAME] # MERGE_WITH_BACKEND
|
13
|
+
field :context, type: Array, default: [:default]
|
14
|
+
field :permissions, type: Object, default: {}
|
15
|
+
field :kind, type: String
|
16
|
+
field :attrs, type: Object
|
17
|
+
field :roles, type: Array, default: []
|
18
|
+
field :tags, type: Array, default: []
|
19
|
+
|
20
|
+
validates_presence_of :kind
|
21
|
+
validates_presence_of :attrs
|
22
|
+
|
23
|
+
belongs_to :owner, class_name: 'User'
|
24
|
+
|
25
|
+
def to_s
|
26
|
+
attrs['name'] || attrs['descr'] || attrs['code']
|
27
|
+
end
|
28
|
+
|
29
|
+
def itg_print(header: nil, prefix: '', allow_nested: true)
|
30
|
+
puts header if header
|
31
|
+
puts "#{prefix}#{[attrs['name'] || attrs['descr'] || attrs['code']]}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Itg
|
4
|
+
module UserModelBase
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include Itg::MongoBase
|
9
|
+
include Itg::Sec
|
10
|
+
|
11
|
+
devise :database_authenticatable, :registerable, :recoverable, :rememberable,
|
12
|
+
:validatable, :confirmable, :lockable, :timeoutable, :trackable
|
13
|
+
|
14
|
+
## Database authenticatable
|
15
|
+
field :encrypted_password, type: String, default: ''
|
16
|
+
|
17
|
+
## Recoverable
|
18
|
+
field :reset_password_token, type: String
|
19
|
+
field :reset_password_sent_at, type: Time
|
20
|
+
|
21
|
+
## Rememberable
|
22
|
+
field :remember_created_at, type: Time
|
23
|
+
|
24
|
+
## Trackable
|
25
|
+
field :sign_in_count, type: Integer, default: 0
|
26
|
+
field :current_sign_in_at, type: Time
|
27
|
+
field :last_sign_in_at, type: Time
|
28
|
+
field :current_sign_in_ip, type: String
|
29
|
+
field :last_sign_in_ip, type: String
|
30
|
+
|
31
|
+
## Confirmable
|
32
|
+
field :confirmation_token, type: String
|
33
|
+
field :confirmed_at, type: Time
|
34
|
+
field :confirmation_sent_at, type: Time
|
35
|
+
field :unconfirmed_email, type: String # Only if using reconfirmable
|
36
|
+
|
37
|
+
## Lockable
|
38
|
+
field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts
|
39
|
+
field :unlock_token, type: String # Only if unlock strategy is :email or :both
|
40
|
+
field :locked_at, type: Time
|
41
|
+
|
42
|
+
|
43
|
+
# include ActiveModel::SecurePassword
|
44
|
+
|
45
|
+
field :email, type: String
|
46
|
+
# field :password_digest, type: String
|
47
|
+
field :db, type: String
|
48
|
+
field :kind, type: String, default: 'user'
|
49
|
+
field :contexts, type: Object, default: {}
|
50
|
+
|
51
|
+
index({ email: 1 }, { unique: true, name: 'email_index' })
|
52
|
+
|
53
|
+
has_many :api_keys, as: :bearer
|
54
|
+
has_many :requests, dependent: :destroy
|
55
|
+
|
56
|
+
# has_secure_password
|
57
|
+
|
58
|
+
validates_presence_of(:email)
|
59
|
+
validates_uniqueness_of(:email)
|
60
|
+
|
61
|
+
def write_attribute(attr_name, value)
|
62
|
+
# puts "write_attribute - attr_name: #{attr_name}, value: #{value}"
|
63
|
+
if attr_name.to_s.downcase == 'kind'
|
64
|
+
value = value.to_s.strip.downcase
|
65
|
+
value = 'user' if ['', nil].include? value
|
66
|
+
end
|
67
|
+
super
|
68
|
+
end
|
69
|
+
|
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
|
+
|
81
|
+
def to_s
|
82
|
+
attributes.symbolize_keys.slice(:_id, :email, :db, :role).to_s
|
83
|
+
end
|
84
|
+
|
85
|
+
def itg_print(header: nil, prefix: '', allow_nested: true)
|
86
|
+
puts header if header
|
87
|
+
puts "#{prefix}#{to_s}"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class_methods do
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/lib/itg/version.rb
CHANGED
data/lib/itg.rb
CHANGED
@@ -1,13 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "itg/version"
|
4
|
-
require_relative "itg/itg_model_base"
|
4
|
+
require_relative "itg/models/itg_model_base"
|
5
|
+
require_relative "itg/models/itg_api_key_model_base"
|
6
|
+
require_relative "itg/models/itg_user_model_base"
|
7
|
+
require_relative "itg/models/itg_context_model_base"
|
8
|
+
require_relative "itg/models/itg_entity_model_base"
|
5
9
|
require_relative "itg/itg_mongo_base"
|
6
10
|
require_relative "itg/itg_printable"
|
7
|
-
# require_relative "itg/itg_sec"
|
8
|
-
require_relative "itg/itg_api_key_base"
|
9
11
|
require_relative "itg/itg_api_key_authenticatable"
|
10
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"
|
11
16
|
require_relative "itg/itg_sec"
|
12
17
|
|
13
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.
|
4
|
+
version: 0.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- aAon
|
@@ -29,13 +29,19 @@ 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
|
-
- lib/itg/itg_api_key_base.rb
|
34
|
-
- lib/itg/itg_model_base.rb
|
35
36
|
- lib/itg/itg_mongo_base.rb
|
36
37
|
- lib/itg/itg_printable.rb
|
37
38
|
- lib/itg/itg_response.rb
|
38
39
|
- lib/itg/itg_sec.rb
|
40
|
+
- lib/itg/models/itg_api_key_model_base.rb
|
41
|
+
- lib/itg/models/itg_context_model_base.rb
|
42
|
+
- lib/itg/models/itg_entity_model_base.rb
|
43
|
+
- lib/itg/models/itg_model_base.rb
|
44
|
+
- lib/itg/models/itg_user_model_base.rb
|
39
45
|
- lib/itg/version.rb
|
40
46
|
- sig/itg.rbs
|
41
47
|
homepage: https://aaon.aggate.gr
|
File without changes
|