forcast 0.0.110
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 +7 -0
- data/.gitignore +1 -0
- data/COPYRIGHTS +11 -0
- data/Gemfile +22 -0
- data/Gemfile.lock +166 -0
- data/PENDIENTES +39 -0
- data/README.md +35 -0
- data/Rakefile +7 -0
- data/VERSION +1 -0
- data/bin/console +14 -0
- data/bin/rake +28 -0
- data/bin/setup +8 -0
- data/forcast.gemspec +73 -0
- data/lib/forcast.rb +5 -0
- data/lib/forcast/controllers/all.rb +2 -0
- data/lib/forcast/controllers/application/all.rb +12 -0
- data/lib/forcast/controllers/application/crud.rb +91 -0
- data/lib/forcast/controllers/application/database.rb +34 -0
- data/lib/forcast/controllers/application/error.rb +18 -0
- data/lib/forcast/controllers/application/exception_handler.rb +25 -0
- data/lib/forcast/controllers/application/json.rb +83 -0
- data/lib/forcast/controllers/application/locale.rb +30 -0
- data/lib/forcast/controllers/application/log.rb +22 -0
- data/lib/forcast/controllers/application/login.rb +41 -0
- data/lib/forcast/controllers/application/meta.rb +61 -0
- data/lib/forcast/controllers/application/model.rb +17 -0
- data/lib/forcast/controllers/application/permitirtodo.rb +27 -0
- data/lib/forcast/controllers/application/utils.rb +28 -0
- data/lib/forcast/controllers/controller/all.rb +8 -0
- data/lib/forcast/controllers/controller/asociar.rb +121 -0
- data/lib/forcast/controllers/controller/busqueda.rb +151 -0
- data/lib/forcast/controllers/controller/definitor/definitor.rb +322 -0
- data/lib/forcast/controllers/controller/definitor/definitor_page.rb +40 -0
- data/lib/forcast/controllers/controller/definitor_all.rb +2 -0
- data/lib/forcast/controllers/controller/filtro.rb +48 -0
- data/lib/forcast/controllers/controller/login/controllers/base_controller.rb +135 -0
- data/lib/forcast/controllers/controller/login/controllers/login_controller.rb +38 -0
- data/lib/forcast/controllers/controller/login/login.rb +71 -0
- data/lib/forcast/controllers/controller/login_all.rb +1 -0
- data/lib/forcast/controllers/controller/paper_trail/paper_trail.rb +21 -0
- data/lib/forcast/controllers/controller/paper_trail_all.rb +1 -0
- data/lib/forcast/controllers/controller/relacion.rb +157 -0
- data/lib/forcast/controllers/controller/rule_engine/action_engine.rb +92 -0
- data/lib/forcast/controllers/controller/rule_engine/controllers/action_logs_controller.rb +12 -0
- data/lib/forcast/controllers/controller/rule_engine/controllers/actions_controller.rb +10 -0
- data/lib/forcast/controllers/controller/rule_engine/controllers/pollings_controller.rb +13 -0
- data/lib/forcast/controllers/controller/rule_engine/controllers/rule_logs_controller.rb +12 -0
- data/lib/forcast/controllers/controller/rule_engine/controllers/rules_controller.rb +19 -0
- data/lib/forcast/controllers/controller/rule_engine/controllers/webhooks_controller.rb +27 -0
- data/lib/forcast/controllers/controller/rule_engine/polling_engine.rb +76 -0
- data/lib/forcast/controllers/controller/rule_engine/rule_controller.rb +27 -0
- data/lib/forcast/controllers/controller/rule_engine/rule_engine.rb +88 -0
- data/lib/forcast/controllers/controller/rule_engine/webhook_engine.rb +38 -0
- data/lib/forcast/controllers/controller/rule_engine_all.rb +5 -0
- data/lib/forcast/jobs/all.rb +2 -0
- data/lib/forcast/jobs/jobs_assets.rb +25 -0
- data/lib/forcast/jobs/jobs_wrapper.rb +25 -0
- data/lib/forcast/jobs/rule_engine/jobs_actions.rb +73 -0
- data/lib/forcast/jobs/rule_engine/jobs_pollings.rb +77 -0
- data/lib/forcast/jobs/rule_engine/jobs_webhooks.rb +54 -0
- data/lib/forcast/migrations/0_migracion_general.rb +44 -0
- data/lib/forcast/migrations/paper_trail/versions.rb +61 -0
- data/lib/forcast/migrations/rule_engine/action_logs.rb +15 -0
- data/lib/forcast/migrations/rule_engine/actions.rb +15 -0
- data/lib/forcast/migrations/rule_engine/pollings.rb +26 -0
- data/lib/forcast/migrations/rule_engine/rule_logs.rb +22 -0
- data/lib/forcast/migrations/rule_engine/rules.rb +27 -0
- data/lib/forcast/migrations/rule_engine/webhooks.rb +25 -0
- data/lib/forcast/models/all.rb +8 -0
- data/lib/forcast/models/asociar.rb +74 -0
- data/lib/forcast/models/busqueda.rb +17 -0
- data/lib/forcast/models/login/authorization.rb +37 -0
- data/lib/forcast/models/login/json_web_token.rb +15 -0
- data/lib/forcast/models/login/login.rb +26 -0
- data/lib/forcast/models/login_all.rb +2 -0
- data/lib/forcast/models/meta.rb +19 -0
- data/lib/forcast/models/modelo.rb +125 -0
- data/lib/forcast/models/models/meta.rb +6 -0
- data/lib/forcast/models/paper_trail/paper_trail.rb +13 -0
- data/lib/forcast/models/paper_trail_all.rb +1 -0
- data/lib/forcast/models/permitirtodo.rb +57 -0
- data/lib/forcast/models/rule_engine/action_engine.rb +19 -0
- data/lib/forcast/models/rule_engine/models/action.rb +41 -0
- data/lib/forcast/models/rule_engine/models/action_log.rb +40 -0
- data/lib/forcast/models/rule_engine/models/polling.rb +43 -0
- data/lib/forcast/models/rule_engine/models/rule.rb +44 -0
- data/lib/forcast/models/rule_engine/models/rule_log.rb +39 -0
- data/lib/forcast/models/rule_engine/models/webhook.rb +43 -0
- data/lib/forcast/models/rule_engine/polling_engine.rb +113 -0
- data/lib/forcast/models/rule_engine/rule.rb +62 -0
- data/lib/forcast/models/rule_engine/rule_engine.rb +137 -0
- data/lib/forcast/models/rule_engine/rule_scope.rb +35 -0
- data/lib/forcast/models/rule_engine/webhook_engine.rb +55 -0
- data/lib/forcast/models/rule_engine_all.rb +6 -0
- data/lib/forcast/routes/concerns/asociar.rb +15 -0
- data/lib/forcast/routes/concerns/buscar.rb +6 -0
- data/lib/forcast/routes/concerns/definitor.rb +3 -0
- data/lib/forcast/routes/concerns/modelo.rb +6 -0
- data/lib/forcast/routes/database.rb +4 -0
- data/lib/forcast/routes/login.rb +4 -0
- data/lib/forcast/routes/paper_trail/paper_trail.rb +3 -0
- data/lib/forcast/routes/rule_engine/rule.rb +5 -0
- data/lib/forcast/tasks/.giosaveZr0hbs +21 -0
- data/lib/forcast/tasks/.keep +0 -0
- data/lib/forcast/tasks/all.rb +16 -0
- data/lib/forcast/tasks/diagramas.rake +32 -0
- data/lib/forcast/tasks/documentation/documentation.rb +100 -0
- data/lib/forcast/tasks/env.rake +10 -0
- data/lib/forcast/tasks/git.rake +22 -0
- data/lib/forcast/tasks/indy/indy.rb +28 -0
- data/lib/forcast/tasks/logs.rake +13 -0
- data/lib/forcast/tasks/process.rake +18 -0
- data/lib/forcast/tasks/schema.rake +17 -0
- data/lib/forcast/tasks/schema/recips/action.rb +18 -0
- data/lib/forcast/tasks/schema/recips/controller.rb +29 -0
- data/lib/forcast/tasks/schema/recips/definitor.rb +38 -0
- data/lib/forcast/tasks/schema/recips/model.rb +43 -0
- data/lib/forcast/tasks/schema/recips/project.rb +19 -0
- data/lib/forcast/tasks/schema/recips/route.rb +9 -0
- data/lib/forcast/tasks/schema/recips/tmp/controller.rb +0 -0
- data/lib/forcast/tasks/schema/recips/tmp/model.rb +0 -0
- data/lib/forcast/tasks/schema/recips/tmp/model/.keep +0 -0
- data/lib/forcast/tasks/schema/rule_engine/rule_engine.rb +98 -0
- data/lib/forcast/tasks/schema/schema.rb +346 -0
- data/lib/forcast/tasks/test.rake +129 -0
- data/lib/forcast/tasks/utils.rake +68 -0
- data/lib/forcast/tasks/yard.rake +18 -0
- data/lib/forcast/utils/all.rb +4 -0
- data/lib/forcast/utils/email.rb +49 -0
- data/lib/forcast/utils/email/forcast/utils/email/layout/default.html.erb +3 -0
- data/lib/forcast/utils/email/forcast/utils/email/layout/default.text.erb +5 -0
- data/lib/forcast/utils/firebase.rb +60 -0
- data/lib/forcast/utils/firebase_database.rb +117 -0
- data/lib/forcast/utils/measure.rb +44 -0
- data/lib/forcast/utils/qvo.rb +141 -0
- data/lib/forcast/utils/server.rb +296 -0
- data/lib/forcast/utils/telegram.rb +82 -0
- data/lib/forcast/utils/thing.rb +180 -0
- data/lib/forcast/version.rb +15 -0
- data/lib/lib/backup.rb +42 -0
- data/lib/lib/deploy.sh +40 -0
- data/lib/lib/forcast_servidor_rails.sh +11 -0
- data/lib/lib/pg_generator.sh +10 -0
- data/lib/lib/production.rb +66 -0
- data/lib/lib/production/production.rb +30 -0
- data/lib/lib/systemctl_maker.rb +88 -0
- data/lib/lib/update.rb +100 -0
- data/production.rb +31 -0
- metadata +301 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
module Forcast
|
|
2
|
+
module Controller
|
|
3
|
+
module DefinitorPage
|
|
4
|
+
include Definitor
|
|
5
|
+
|
|
6
|
+
def definitor
|
|
7
|
+
raise Application::Error::General.new(t("operation_notValid")) unless permitted_definition.include?(@operation)
|
|
8
|
+
@response = send("#{@operation}_view_definitor")
|
|
9
|
+
json_response(@response, 'ok')
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def permitted_definition
|
|
13
|
+
%w[wizard]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def wizard_create_resources_definitor(*args)
|
|
17
|
+
wizard = Hash.new
|
|
18
|
+
wizard[:type] = 'stepper'
|
|
19
|
+
wizard[:name] = 'stepper'
|
|
20
|
+
wizard[:label] = 'test stepper'
|
|
21
|
+
wizard[:inputType] = 'string'
|
|
22
|
+
wizard[:wizard] = *args.map {|e| constructor_wizard_create(e) }
|
|
23
|
+
wizard[:wizard].push({labelStepper: 'Done',fields: []})
|
|
24
|
+
wizard
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def constructor_wizard_create(e)
|
|
28
|
+
key = e.keys.first
|
|
29
|
+
selected = e[key]
|
|
30
|
+
result = Hash.new
|
|
31
|
+
result[:labelStepper] = "Create #{key.to_s.pluralize.capitalize}"
|
|
32
|
+
result[:resource] = key.to_s.pluralize
|
|
33
|
+
result[:fields] = serializer_mongo(all_definitions(key.to_s).slice(*selected))
|
|
34
|
+
result
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
module Forcast
|
|
2
|
+
module Controller
|
|
3
|
+
module Filtro
|
|
4
|
+
|
|
5
|
+
def index
|
|
6
|
+
filtrar_por if params_present?('filtrar_modelo') && params_present?('filtrar_por') && params_present?('filtro') && !params_present?('relacion')
|
|
7
|
+
deep_filtrar_por if params_present?('relacion') && params_present?('filtrar_modelo') && params_present?('filtrar_por') && params_present?('filtro')
|
|
8
|
+
super()
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def filtrar_por
|
|
14
|
+
|
|
15
|
+
comprobar_filtrar_por
|
|
16
|
+
@response ||= @model.all
|
|
17
|
+
@response = @model.where(id: @response.pluck(:id))
|
|
18
|
+
@response = @response.includes(params[:filtrar_modelo])
|
|
19
|
+
.where( params[:filtrar_modelo]+'s' => { params[:filtrar_por] => params[:filtro] })
|
|
20
|
+
.last(@limit).pluck_to_hash(*params_permitidos_enviar)
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
#Si el metodo tiene la relacion activada
|
|
25
|
+
#Se trabaja con el array que proviene de buscar incluyendo relacion
|
|
26
|
+
#Se verifican las id que cumplan el criterio y luego se limpia el array de buscar / relacion
|
|
27
|
+
def deep_filtrar_por
|
|
28
|
+
|
|
29
|
+
comprobar_filtrar_por
|
|
30
|
+
paso = @model.where( :id => @response.map { |e| e[@model.to_s]['id'] })
|
|
31
|
+
paso2 = paso.includes(params[:filtrar_modelo])
|
|
32
|
+
.where( params[:filtrar_modelo]+'s' => { params[:filtrar_por] => params[:filtro] })
|
|
33
|
+
.last(@limit).pluck(:id)
|
|
34
|
+
|
|
35
|
+
@response = @response.map{ |e| e if paso2.include?(e[@model.to_s]['id']) }
|
|
36
|
+
@response = @response.compact
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def comprobar_filtrar_por
|
|
40
|
+
raise Application::Error::General.new(t("error_relation_param", param: params[:filtrar_modelo])) unless comprobar_relacion(:has_many, params[:filtrar_modelo])
|
|
41
|
+
params_permitidos_filtrar = abstract_controller(params[:filtrar_modelo]).new.send (:params_permitidos_buscar)
|
|
42
|
+
raise Application::Error::General.new(t("error_filter_model_param", param: [params[:filtrar_modelo],params[:filtrar_por]])) unless params_permitidos_filtrar.include?(params[:filtrar_por])
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
module Api
|
|
2
|
+
class BaseController < ActionController::Base
|
|
3
|
+
|
|
4
|
+
before_action :check_authorization_presence, only: [:check_token_format,:authenticate]
|
|
5
|
+
before_action :authenticate, except: [:sign_in,:sign_out]
|
|
6
|
+
before_action :check_token_presence, except: [:sign_in,:sign_out]
|
|
7
|
+
before_action :check_token_revoke_format, only: [:sign_out]
|
|
8
|
+
#before_action :init_user
|
|
9
|
+
|
|
10
|
+
attr_reader :current_user
|
|
11
|
+
|
|
12
|
+
protected
|
|
13
|
+
|
|
14
|
+
# Check Methods
|
|
15
|
+
def check_content_type_presence
|
|
16
|
+
if request.method == "POST" || request.method == "PATCH"
|
|
17
|
+
unless request.env['CONTENT_TYPE'].present?
|
|
18
|
+
render json:{errors:[{title:'Content Type',
|
|
19
|
+
detail:"The content type is not present",
|
|
20
|
+
source:"Header content type" }]}, status: :bad_request
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def check_token_presence
|
|
26
|
+
#Check presence and format token
|
|
27
|
+
if !(request.env['HTTP_AUTHORIZATION'].present?) && !(request.env['HTTP_AUTHORIZATION'].is_a?(String)) && ((request.env['HTTP_AUTHORIZATION'] =~ /Bearer [.]*/) != 0)
|
|
28
|
+
render json:{errors:[{title:'Http Authorization',
|
|
29
|
+
detail:"The http authorization is not present or the format is not valid, please check your header or the expected format",
|
|
30
|
+
source:"Header AUTHORIZATION" }]}, status: :bad_request
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def check_token_format
|
|
35
|
+
#Authorization: Bearer e9629c2a-6763-45f4-9d3a-1b2c7822febe
|
|
36
|
+
authorization = request.env['HTTP_AUTHORIZATION']
|
|
37
|
+
|
|
38
|
+
#TODO: Manage the error when the split can't be executed
|
|
39
|
+
options = authorization.split(' ')
|
|
40
|
+
|
|
41
|
+
bearer_param = options.first
|
|
42
|
+
token_param = options.second
|
|
43
|
+
|
|
44
|
+
if bearer_param.blank? && token_param.blank? && (bearer_param != "Bearer" || token_param.blank?)
|
|
45
|
+
render json:{errors:[{title:'Authorization Format Token',
|
|
46
|
+
detail:"The format token is not valid, please check it",
|
|
47
|
+
source:"data/attributes/authorization" }]}, status: :bad_request
|
|
48
|
+
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def check_authorization_presence
|
|
53
|
+
unless request.env['HTTP_AUTHORIZATION'].present?
|
|
54
|
+
render json:{errors:[{title:'Authorization Header',
|
|
55
|
+
detail:"The Authorization header is not present",
|
|
56
|
+
source:"data/headers" }]}, status: :bad_request
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def check_token_revoke_format
|
|
62
|
+
token = token_in_params
|
|
63
|
+
|
|
64
|
+
if !(token.present?) && !(token.is_a?(String)) && ((token =~ /Bearer [.]*/) != 0)
|
|
65
|
+
render json:{errors:[{title:'Token presence or format',
|
|
66
|
+
detail:"The token is not present or the format is not valid, please check your params or the expected format",
|
|
67
|
+
source:"Token Params" }]}, status: :bad_request
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
#Action Methods
|
|
72
|
+
def authenticate
|
|
73
|
+
authorization = request.env['HTTP_AUTHORIZATION']
|
|
74
|
+
|
|
75
|
+
options = authorization.split(' ')
|
|
76
|
+
|
|
77
|
+
bearer_param = options.first
|
|
78
|
+
token_param = options.last
|
|
79
|
+
|
|
80
|
+
decoded_token = JsonWebToken.decode(token_param)
|
|
81
|
+
|
|
82
|
+
if decoded_token.blank?
|
|
83
|
+
render_access_denied
|
|
84
|
+
else
|
|
85
|
+
user_id = decoded_token[:user_id]
|
|
86
|
+
created_at_format = decoded_token[:birthday_date]
|
|
87
|
+
expiration_time = Time.at(decoded_token[:exp])
|
|
88
|
+
|
|
89
|
+
#Trick to located the token, it should be created_at
|
|
90
|
+
created_at = Time.parse(created_at_format)
|
|
91
|
+
auth = Authorization.find_by(user_id:user_id,active_time_stamp:created_at.to_i)
|
|
92
|
+
|
|
93
|
+
#TODO: Check how to use the operative attribute in Authorization model
|
|
94
|
+
|
|
95
|
+
if auth.blank? || !auth.try(:operative)
|
|
96
|
+
render json:{errors: [{message:"No estás autorizado a entrar", code:401}]}, status: :unauthorized
|
|
97
|
+
else
|
|
98
|
+
@current_user = User.find(decoded_token[:user_id])
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
#Methods for authorizations
|
|
105
|
+
def get_plants
|
|
106
|
+
if @current_user.rol_5s == User::USER_ROLES[:auditor] || @current_user.rol_5s == User::USER_ROLES[:supervisor]
|
|
107
|
+
[PlantService.get_plant(@current_user)["object"]]
|
|
108
|
+
elsif @current_user.rol_5s == User::USER_ROLES[:corporative]
|
|
109
|
+
PlantService.get_plants["object"]
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
#Methods for authorizations
|
|
114
|
+
def current_plant_id
|
|
115
|
+
puts "#NOMBRE => #{@current_user.userccu}, ROL => #{@current_user.rol_5s}"
|
|
116
|
+
if @current_user.rol_5s == User::USER_ROLES[:auditor] || @current_user.rol_5s == User::USER_ROLES[:supervisor]
|
|
117
|
+
@current_user.plant_id
|
|
118
|
+
elsif @current_user.rol_5s == User::USER_ROLES[:corporative]
|
|
119
|
+
puts "SOY CORPORATIVO"
|
|
120
|
+
params[:id]
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
#Acordarse de borrar
|
|
125
|
+
def init_user
|
|
126
|
+
puts controller_name
|
|
127
|
+
if params[:user_id]
|
|
128
|
+
@current_user = User.find_by(:instance_id => params[:user_id])
|
|
129
|
+
elsif params[:id] && controller_name == 'users'
|
|
130
|
+
@current_user = User.find_by(:instance_id => params[:id]) if params[:id]
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
end
|
|
135
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
class Api::LoginController < ApplicationController
|
|
2
|
+
|
|
3
|
+
include Forcast::Controller::Login
|
|
4
|
+
|
|
5
|
+
private
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def validaciones_login (user)
|
|
9
|
+
|
|
10
|
+
#raise Application::Error::General.new(t("error_bad_custom")) if user.class.name == "Administrador"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def modelo_a_validar
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
return modelo_a_validar = []
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def modelo_login
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
return modelo_login = []
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
require 'active_support'
|
|
2
|
+
module Forcast
|
|
3
|
+
module Controller
|
|
4
|
+
module Login
|
|
5
|
+
|
|
6
|
+
extend ActiveSupport::Concern
|
|
7
|
+
|
|
8
|
+
included do
|
|
9
|
+
|
|
10
|
+
before_action :set_user, only: [:create_login, :destroy_login]
|
|
11
|
+
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def create_login
|
|
15
|
+
raise Application::Error::General.new(t("error_bad_password")) unless @user.is_password?(params[:password])
|
|
16
|
+
validaciones_login(@user)
|
|
17
|
+
h = Hash.new
|
|
18
|
+
h["auth_token"] = @user.generate_auth_token
|
|
19
|
+
h["token_telefono"] = @token_telefono
|
|
20
|
+
json_response(h,t("login_ok"))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def destroy_login
|
|
25
|
+
raise Application::Error::General.new(t("error_bad_password")) unless @user.is_password?(params[:password])
|
|
26
|
+
validaciones_login(@user)
|
|
27
|
+
@user.invalidate_auth_token
|
|
28
|
+
json_response('',t("logout_ok"))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def set_user
|
|
35
|
+
|
|
36
|
+
@variable = params[:variable]
|
|
37
|
+
@dato = params[:dato]
|
|
38
|
+
#raise Application::Error::General.new(t("error_present_mail")) unless params[:mail].present?
|
|
39
|
+
raise Application::Error::General.new(t("error_present_password")) unless params[:password].present?
|
|
40
|
+
set_user_using_model if params[:model].present?
|
|
41
|
+
mm = ''
|
|
42
|
+
modelo_a_validar.each do |x|
|
|
43
|
+
mm = c_to_m(x)
|
|
44
|
+
next unless mm.exists?("#{@variable}" => @dato)
|
|
45
|
+
user = mm.find_by("#{@variable}" => @dato)
|
|
46
|
+
@user ||= user
|
|
47
|
+
end
|
|
48
|
+
raise Application::Error::General.new(t("error_not_user")) if @user === nil
|
|
49
|
+
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def set_user_using_model
|
|
53
|
+
|
|
54
|
+
raise Application::Error::General.new(t("error_present_modelo", params: params[:model])) unless modelo_a_validar.include?(params[:model])
|
|
55
|
+
mm = ''
|
|
56
|
+
modelo_a_validar.each do |x|
|
|
57
|
+
next if params[:model] != x
|
|
58
|
+
mm = c_to_m(x)
|
|
59
|
+
end
|
|
60
|
+
raise Application::Error::General.new(t("error_not_user", params: @dato)) unless mm.exists?("#{@variable}" => @dato)
|
|
61
|
+
@user ||= mm.find_by("#{@variable}" => @dato)
|
|
62
|
+
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def c_to_m(str)
|
|
66
|
+
str.classify.constantize
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "forcast/controllers/controller/login/login"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'active_support'
|
|
2
|
+
module Forcast
|
|
3
|
+
module Controller
|
|
4
|
+
module PaperTrail
|
|
5
|
+
extend ActiveSupport::Concern
|
|
6
|
+
|
|
7
|
+
included do
|
|
8
|
+
before_action :set_model_id_version, only: [:versions]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def versions
|
|
12
|
+
json_response(@model_id.versions,'ok')
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def set_model_id_version
|
|
16
|
+
set_model_id
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "forcast/controllers/controller/paper_trail/paper_trail"
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
require 'active_support'
|
|
2
|
+
module Forcast
|
|
3
|
+
module Controller
|
|
4
|
+
module Relacion
|
|
5
|
+
|
|
6
|
+
## Este módulo hace que cuando se pregunta por los has_many de un modelo los entregue
|
|
7
|
+
## EJ: GET api/camaras/1/alarmas => entregaria las alarmas de camaras_id = 1
|
|
8
|
+
## EJ: GET api/camaras/1/alarmas/1 => entregaria la alarma_id = 1 de camaras_id = 1
|
|
9
|
+
|
|
10
|
+
extend ActiveSupport::Concern
|
|
11
|
+
|
|
12
|
+
included do
|
|
13
|
+
before_action :set_relation
|
|
14
|
+
before_action :set_relation_id, only: [:show, :update, :destroy]
|
|
15
|
+
|
|
16
|
+
def set_relation
|
|
17
|
+
##Compruebo que exista el parametro de relacion
|
|
18
|
+
##Porque el metodo relation arroja un Array en caso de no encontrar nada
|
|
19
|
+
relation.is_a?(Array) ? deep_relation : @model = relacion_directa_indirecta
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def set_relation_id
|
|
23
|
+
relation.is_a?(Array) ? @model_id : @model_id = relation.find(params[:id])
|
|
24
|
+
relacion_padre if params[:padre]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def c_to_m(str)
|
|
30
|
+
return str.classify.constantize
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def relation
|
|
34
|
+
controller = controller_name.chomp('s')
|
|
35
|
+
model.reflect_on_all_associations(:belongs_to).each do |x|
|
|
36
|
+
n = x.name.to_s
|
|
37
|
+
nid = n +"_id"
|
|
38
|
+
if params.include?(nid)
|
|
39
|
+
return c_to_m(n).find(params[nid]).send(controller)
|
|
40
|
+
break
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def comprobar_relacion(re,var)
|
|
46
|
+
|
|
47
|
+
model.reflect_on_all_associations(re).each do |x|
|
|
48
|
+
n = x.name.to_s
|
|
49
|
+
if n == var
|
|
50
|
+
return true
|
|
51
|
+
break
|
|
52
|
+
else
|
|
53
|
+
next
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
return false
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def relacion_padre
|
|
60
|
+
|
|
61
|
+
raise Application::Error::General.new(t("error_relation_param", param: params[:padre])) unless comprobar_relacion(:belongs_to, params[:padre])
|
|
62
|
+
params_permitidos_enviar = abstract_controller(params[:padre]).new.send (:params_permitidos_enviar)
|
|
63
|
+
@response = c_to_m(params[:padre]).find(@model_id.send(params[:padre]+'_id'))
|
|
64
|
+
@response = @response.attributes.slice(*params_permitidos_enviar)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def relacion_directa_indirecta
|
|
68
|
+
##Fijarse en estos parametros ya que nos interesan solo los de la url original, quizas sea mejor parsear la url.split("/")
|
|
69
|
+
return @model unless request.request_method_symbol == :get
|
|
70
|
+
rel = []
|
|
71
|
+
params.keys.map { |val| rel.push(val.chomp('_id')) if val.include?('_id') && val != 'model_id' && val != 'modelo_id' && val != 'modelo'}
|
|
72
|
+
if rel.length > 1
|
|
73
|
+
return deep_relacion_directa_indirecta(rel)
|
|
74
|
+
end
|
|
75
|
+
relation
|
|
76
|
+
end
|
|
77
|
+
#Comprueba que el recurso anidado corresponda al orden del request
|
|
78
|
+
#EX: localhost:4000/api/usuarios/20/recintos/2/ubicacions
|
|
79
|
+
#El metodo comprueba que la ubicacion pertenezca al recinto 2 asociado al usuario 20
|
|
80
|
+
def deep_relacion_directa_indirecta(rel)
|
|
81
|
+
init = c_to_m(rel[0]).find(params[rel[0]+'_id'])
|
|
82
|
+
rel.each_with_index do |ar,index|
|
|
83
|
+
next if index + 1 == rel.length
|
|
84
|
+
#Lanza un error si no encuentra el recurso en alguno de los padres
|
|
85
|
+
begin
|
|
86
|
+
init = init.send(rel[index+1]).find(params[rel[index+1]+'_id'])
|
|
87
|
+
rescue => e
|
|
88
|
+
raise Application::Error::General.new(t("error_deep_relation_doesnt_exist", param: e))
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
relation
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def deep_relation
|
|
95
|
+
|
|
96
|
+
return @model unless request.request_method_symbol == :get
|
|
97
|
+
rel = []
|
|
98
|
+
params.keys.map { |val| rel.push(val.chomp('_id')) if val.include?('_id') && val != 'model_id' && val != 'modelo_id' && val != 'modelo'}
|
|
99
|
+
rel = rel.last
|
|
100
|
+
#Entender bien cuando se produce el Trigger, si la url es
|
|
101
|
+
#/usuarios el last es action, si es /usuario/:id es id
|
|
102
|
+
#ademas si /usuario limit=1 envio parametos el last key es igual a usuario
|
|
103
|
+
#entonces compruebo el largo de los params
|
|
104
|
+
return @model if rel == nil || rel == 'action' || rel == 'id' || params.keys.length > 7
|
|
105
|
+
return @model if comprobar_relacion(:belongs_to, rel)
|
|
106
|
+
init = c_to_m(rel)
|
|
107
|
+
i = 0
|
|
108
|
+
arr = []
|
|
109
|
+
arr.push(rel)
|
|
110
|
+
comp = model.name.to_s.downcase!
|
|
111
|
+
catch :exit do
|
|
112
|
+
deep_deep_relation(init,comp,i,arr)
|
|
113
|
+
end
|
|
114
|
+
return fetch_from_relations_model(arr,rel)
|
|
115
|
+
end
|
|
116
|
+
def deep_deep_relation (init,comp,i,arr)
|
|
117
|
+
#Encuentra la cadena de relaciones entre un modelo padre y otro hijo
|
|
118
|
+
#EX: [usuario,recinto,ubicacion,camara,grabacions]
|
|
119
|
+
#Toma usuario y grabacions y encuentra la cadena
|
|
120
|
+
init.reflect_on_all_associations(:has_many).each do |x|
|
|
121
|
+
x = x.name.to_s
|
|
122
|
+
#p x
|
|
123
|
+
#p i
|
|
124
|
+
arr.push(x)
|
|
125
|
+
if x == comp
|
|
126
|
+
throw :exit
|
|
127
|
+
else
|
|
128
|
+
i = i + 1
|
|
129
|
+
deep_deep_relation(c_to_m(x),comp,i,arr)
|
|
130
|
+
end
|
|
131
|
+
i = i - 1
|
|
132
|
+
arr.delete(x)
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
def fetch_from_relations_model(arr,rel)
|
|
136
|
+
#Recibe un array con las relaciones ordenadas de mayor a menos
|
|
137
|
+
#[usuario,recinto,ubicacion,camara]
|
|
138
|
+
#Recibe rel que es igual al primer valor del parametro mayor
|
|
139
|
+
#params[:usuario_id]
|
|
140
|
+
rel = params[rel+'_id']
|
|
141
|
+
|
|
142
|
+
init = c_to_m(arr[0]).find(rel)
|
|
143
|
+
arr.each_with_index do |ar,index|
|
|
144
|
+
next if index + 1 == arr.length
|
|
145
|
+
init = c_to_m(arr[index+1]).send(:where, { ar + '_id' => init})
|
|
146
|
+
end
|
|
147
|
+
@model = init
|
|
148
|
+
# @model = Grabacion.where(camara_id:
|
|
149
|
+
# Camara.where(hub_id:
|
|
150
|
+
# Hub.where(recinto_id:
|
|
151
|
+
# Recinto.where(usuario_id:
|
|
152
|
+
# Usuario.find(rel)))))
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|