Move lib folder inside the app folder
The purpose of the lib folder is to have code that doesn't necessary belong in the application but can be shared with other applications. However, we don't have other applications and, if we did, the way to share code between them would be using a gem or even a git submodule. So having both the `app/` and the `lib/` folders is confusing IMHO, and it causes unnecessary problems with autoloading. So we're moving the `lib/` folder to `app/lib/`. Originally, some of these files were in the `app/services/` folder and then they were moved to the `lib/` folder. We're using `app/lib/` instead of `app/services/` so the upgrade is less confusing. There's an exception, though. The `OmniAuth::Strategies::Wordpress` class needs to be available in the Devise initializer. Since this is an initializer and trying to autoload a class here will be problematic when switching to Zeitwerk, we'll keep the `require` clause on top of the Devise initializer in order to load the file and so it will be loaded even if it isn't in the autoload paths anymore.
This commit is contained in:
58
app/lib/manager_authenticator.rb
Normal file
58
app/lib/manager_authenticator.rb
Normal file
@@ -0,0 +1,58 @@
|
||||
class ManagerAuthenticator
|
||||
def initialize(data = {})
|
||||
@manager = {
|
||||
login: data[:login],
|
||||
user_key: data[:clave_usuario],
|
||||
date: data[:fecha_conexion]
|
||||
}.with_indifferent_access
|
||||
end
|
||||
|
||||
def auth
|
||||
return false unless [@manager[:login], @manager[:user_key], @manager[:date]].all?(&:present?)
|
||||
return @manager if manager_exists? && application_authorized?
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def manager_exists?
|
||||
response = client.call(
|
||||
:get_status_user_data,
|
||||
message: { ub: { user_key: @manager[:user_key], date: @manager[:date] }}
|
||||
).body
|
||||
|
||||
parsed_response = parser.parse((response[:get_status_user_data_response][:get_status_user_data_return]))
|
||||
@manager[:login] == parsed_response["USUARIO"]["LOGIN"]
|
||||
rescue
|
||||
false
|
||||
end
|
||||
|
||||
def application_authorized?
|
||||
response = client.call(
|
||||
:get_applications_user_list,
|
||||
message: { ub: { user_key: @manager[:user_key] }}
|
||||
).body
|
||||
|
||||
user_list_return = response[:get_applications_user_list_response][:get_applications_user_list_return]
|
||||
parsed_response = parser.parse(user_list_return)
|
||||
aplication_value = parsed_response["APLICACIONES"]["APLICACION"]
|
||||
# aplication_value from UWEB can be an array of hashes or a hash
|
||||
aplication_value.include?("CLAVE_APLICACION" => application_key) ||
|
||||
aplication_value["CLAVE_APLICACION"] == application_key
|
||||
rescue
|
||||
false
|
||||
end
|
||||
|
||||
def client
|
||||
@client ||= Savon.client(wsdl: Tenant.current_secrets.managers_url)
|
||||
end
|
||||
|
||||
def parser
|
||||
@parser ||= Nori.new
|
||||
end
|
||||
|
||||
def application_key
|
||||
Tenant.current_secrets.managers_application_key.to_s
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user