Merge pull request #612 from AyuntamientoMadrid/login-uweb
Login en management
This commit is contained in:
@@ -12,7 +12,7 @@ class Management::BaseController < ActionController::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def current_manager
|
def current_manager
|
||||||
@current_manager ||= Manager.find(session["manager_id"]) if session["manager_id"]
|
session["manager"]
|
||||||
end
|
end
|
||||||
|
|
||||||
def managed_user
|
def managed_user
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
|
require "manager_authenticator"
|
||||||
|
|
||||||
class Management::SessionsController < ActionController::Base
|
class Management::SessionsController < ActionController::Base
|
||||||
|
|
||||||
def create
|
def create
|
||||||
destroy_session
|
destroy_session
|
||||||
if manager = Manager.valid_manager(params[:login], params[:clave_usuario])
|
if manager = ManagerAuthenticator.new(params).auth
|
||||||
session["manager_id"] = manager.id
|
session["manager"] = manager
|
||||||
redirect_to management_root_path
|
redirect_to management_root_path
|
||||||
else
|
else
|
||||||
raise ActionController::RoutingError.new('Not Found')
|
raise ActionController::RoutingError.new('Not Found')
|
||||||
@@ -18,7 +20,7 @@ class Management::SessionsController < ActionController::Base
|
|||||||
private
|
private
|
||||||
|
|
||||||
def destroy_session
|
def destroy_session
|
||||||
session["manager_id"] = nil
|
session["manager"] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
class Manager < ActiveRecord::Base
|
|
||||||
validates :username, presence: true, uniqueness: true
|
|
||||||
validates :password_digest, presence: true
|
|
||||||
|
|
||||||
has_secure_password
|
|
||||||
|
|
||||||
def self.valid_manager(username = nil, password = nil)
|
|
||||||
return false unless username.present? && password.present?
|
|
||||||
Manager.find_by(username: username).try(:authenticate, password)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
16
db/migrate/20151015135154_destroy_manager.rb
Normal file
16
db/migrate/20151015135154_destroy_manager.rb
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
class DestroyManager < ActiveRecord::Migration
|
||||||
|
def self.up
|
||||||
|
drop_table :managers
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.down
|
||||||
|
create_table :managers do |t|
|
||||||
|
t.string :username, null: false
|
||||||
|
t.string :password_digest, null: false
|
||||||
|
t.timestamp :last_login_at
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index :managers, [:username]
|
||||||
|
end
|
||||||
|
end
|
||||||
12
db/schema.rb
12
db/schema.rb
@@ -11,7 +11,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20151013145757) do
|
ActiveRecord::Schema.define(version: 20151015135154) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
@@ -168,16 +168,6 @@ ActiveRecord::Schema.define(version: 20151013145757) do
|
|||||||
|
|
||||||
add_index "locks", ["user_id"], name: "index_locks_on_user_id", using: :btree
|
add_index "locks", ["user_id"], name: "index_locks_on_user_id", using: :btree
|
||||||
|
|
||||||
create_table "managers", force: :cascade do |t|
|
|
||||||
t.string "username", null: false
|
|
||||||
t.string "password_digest", null: false
|
|
||||||
t.datetime "last_login_at"
|
|
||||||
t.datetime "created_at"
|
|
||||||
t.datetime "updated_at"
|
|
||||||
end
|
|
||||||
|
|
||||||
add_index "managers", ["username"], name: "index_managers_on_username", using: :btree
|
|
||||||
|
|
||||||
create_table "moderators", force: :cascade do |t|
|
create_table "moderators", force: :cascade do |t|
|
||||||
t.integer "user_id"
|
t.integer "user_id"
|
||||||
end
|
end
|
||||||
|
|||||||
44
lib/manager_authenticator.rb
Normal file
44
lib/manager_authenticator.rb
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
class ManagerAuthenticator
|
||||||
|
|
||||||
|
def initialize(data={})
|
||||||
|
@manager = {login: data[:login], user_key: data[:clave_usuario], date: data[:fecha_conexion]}
|
||||||
|
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
|
||||||
|
parsed_response = parser.parse((response[:get_applications_user_list_response][:get_applications_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: Rails.application.secrets.managers_url)
|
||||||
|
end
|
||||||
|
|
||||||
|
def parser
|
||||||
|
@parser ||= Nori.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def application_key
|
||||||
|
Rails.application.secrets.managers_application_key.to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -2,36 +2,28 @@ require 'rails_helper'
|
|||||||
|
|
||||||
describe Management::SessionsController do
|
describe Management::SessionsController do
|
||||||
|
|
||||||
before(:all) do
|
|
||||||
create(:manager, username: "supermanager" , password: "secret")
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'Sign in' do
|
describe 'Sign in' do
|
||||||
it "should return 404 if not username/password" do
|
it "should return 404 if wrong credentials" do
|
||||||
expect { get :create }.to raise_error "Not Found"
|
allow_any_instance_of(ManagerAuthenticator).to receive(:auth).and_return(false)
|
||||||
end
|
expect { get :create, login: "nonexistent" , clave_usuario: "wrong"}.to raise_error "Not Found"
|
||||||
|
|
||||||
it "should return 404 if wrong username" do
|
|
||||||
expect { get :create, login: "nonexistent" , clave_usuario: "secret" }.to raise_error "Not Found"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return 404 if wrong password" do
|
|
||||||
expect { get :create, login: "supermanager" , clave_usuario: "wrong" }.to raise_error "Not Found"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should redirect to management root path if right credentials" do
|
it "should redirect to management root path if right credentials" do
|
||||||
get :create, login: "supermanager" , clave_usuario: "secret"
|
manager = {login: "JJB033", user_key: "31415926" , date: "20151031135905"}
|
||||||
|
allow_any_instance_of(ManagerAuthenticator).to receive(:auth).and_return(manager)
|
||||||
|
|
||||||
|
get :create, login: "JJB033" , clave_usuario: "31415926", fecha_conexion: "20151031135905"
|
||||||
expect(response).to be_redirect
|
expect(response).to be_redirect
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'Sign out' do
|
describe 'Sign out' do
|
||||||
it "should destroy the session and redirect" do
|
it "should destroy the session and redirect" do
|
||||||
session[:manager_id] = 1
|
session[:manager] = {user_key: "31415926" , date: "20151031135905", login: "JJB033"}
|
||||||
|
|
||||||
delete :destroy
|
delete :destroy
|
||||||
|
|
||||||
expect(session[:manager_id]).to be_nil
|
expect(session[:manager]).to be_nil
|
||||||
expect(response).to be_redirect
|
expect(response).to be_redirect
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -223,11 +223,6 @@ FactoryGirl.define do
|
|||||||
user
|
user
|
||||||
end
|
end
|
||||||
|
|
||||||
factory :manager do
|
|
||||||
sequence(:username) { |n| "manager#{n}" }
|
|
||||||
password 'supersecret'
|
|
||||||
end
|
|
||||||
|
|
||||||
factory :organization do
|
factory :organization do
|
||||||
user
|
user
|
||||||
responsible_name "Johnny Utah"
|
responsible_name "Johnny Utah"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ require 'rails_helper'
|
|||||||
feature 'DocumentVerifications' do
|
feature 'DocumentVerifications' do
|
||||||
|
|
||||||
background do
|
background do
|
||||||
login_as_manager(create(:manager))
|
login_as_manager
|
||||||
end
|
end
|
||||||
|
|
||||||
scenario 'Verifying a level 3 user shows an "already verified" page' do
|
scenario 'Verifying a level 3 user shows an "already verified" page' do
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ require 'rails_helper'
|
|||||||
feature 'EmailVerifications' do
|
feature 'EmailVerifications' do
|
||||||
|
|
||||||
scenario 'Verifying a level 1 user via email' do
|
scenario 'Verifying a level 1 user via email' do
|
||||||
login_as_manager(create(:manager))
|
login_as_manager
|
||||||
|
|
||||||
user = create(:user)
|
user = create(:user)
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ require 'rails_helper'
|
|||||||
feature 'Managed User' do
|
feature 'Managed User' do
|
||||||
|
|
||||||
background do
|
background do
|
||||||
login_as_manager(create(:manager))
|
login_as_manager
|
||||||
end
|
end
|
||||||
|
|
||||||
context "Currently managed user" do
|
context "Currently managed user" do
|
||||||
@@ -47,7 +47,7 @@ feature 'Managed User' do
|
|||||||
end
|
end
|
||||||
|
|
||||||
scenario "User becomes verified as level two (pending email confirmation for level three)" do
|
scenario "User becomes verified as level two (pending email confirmation for level three)" do
|
||||||
login_as_manager(create(:manager))
|
login_as_manager
|
||||||
|
|
||||||
user = create(:user)
|
user = create(:user)
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ feature 'Managed User' do
|
|||||||
end
|
end
|
||||||
|
|
||||||
scenario "User is created as level three from scratch" do
|
scenario "User is created as level three from scratch" do
|
||||||
login_as_manager(create(:manager))
|
login_as_manager
|
||||||
|
|
||||||
visit management_document_verifications_path
|
visit management_document_verifications_path
|
||||||
fill_in 'document_verification_document_number', with: '1234'
|
fill_in 'document_verification_document_number', with: '1234'
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ require 'rails_helper'
|
|||||||
feature 'Proposals' do
|
feature 'Proposals' do
|
||||||
|
|
||||||
background do
|
background do
|
||||||
manager = create(:manager)
|
login_as_manager
|
||||||
login_as_manager(manager)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context "Create" do
|
context "Create" do
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ feature 'users' do
|
|||||||
|
|
||||||
scenario 'Creating a level 3 user from scratch' do
|
scenario 'Creating a level 3 user from scratch' do
|
||||||
|
|
||||||
login_as_manager(create(:manager))
|
login_as_manager
|
||||||
|
|
||||||
visit management_document_verifications_path
|
visit management_document_verifications_path
|
||||||
fill_in 'document_verification_document_number', with: '1234'
|
fill_in 'document_verification_document_number', with: '1234'
|
||||||
|
|||||||
65
spec/lib/manager_authenticator_spec.rb
Normal file
65
spec/lib/manager_authenticator_spec.rb
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe ManagerAuthenticator do
|
||||||
|
describe 'initialization params' do
|
||||||
|
it 'should cause auth to return false if blank login' do
|
||||||
|
authenticator = ManagerAuthenticator.new({login: "", clave_usuario: "31415926", fecha_conexion: "20151031135905"})
|
||||||
|
expect(authenticator.auth).to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should cause auth to return false if blank user_key' do
|
||||||
|
authenticator = ManagerAuthenticator.new({login: "JJB033", clave_usuario: "", fecha_conexion: "20151031135905"})
|
||||||
|
expect(authenticator.auth).to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should cause auth to return false if blank date' do
|
||||||
|
authenticator = ManagerAuthenticator.new({login: "JJB033", clave_usuario: "31415926", fecha_conexion: ""})
|
||||||
|
expect(authenticator.auth).to be false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#auth' do
|
||||||
|
before(:all) do
|
||||||
|
@authenticator = ManagerAuthenticator.new({login: "JJB033", clave_usuario: "31415926", fecha_conexion: "20151031135905"})
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should return false if not manager_exists' do
|
||||||
|
allow(@authenticator).to receive(:manager_exists?).and_return(false)
|
||||||
|
allow(@authenticator).to receive(:application_authorized?).and_return(true)
|
||||||
|
|
||||||
|
expect(@authenticator.auth).to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should return false if not application_authorized' do
|
||||||
|
allow(@authenticator).to receive(:manager_exists?).and_return(true)
|
||||||
|
allow(@authenticator).to receive(:application_authorized?).and_return(false)
|
||||||
|
|
||||||
|
expect(@authenticator.auth).to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should return ok if manager_exists and application_authorized' do
|
||||||
|
allow(@authenticator).to receive(:manager_exists?).and_return(true)
|
||||||
|
allow(@authenticator).to receive(:application_authorized?).and_return(true)
|
||||||
|
|
||||||
|
expect(@authenticator.auth).to be_truthy
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'SOAP' do
|
||||||
|
before(:all) do
|
||||||
|
@authenticator = ManagerAuthenticator.new({login: "JJB033", clave_usuario: "31415926", fecha_conexion: "20151031135905"})
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should call the verification user method' do
|
||||||
|
allow(@authenticator).to receive(:application_authorized?).and_return(true)
|
||||||
|
expect(@authenticator.send(:client)).to receive(:call).with(:get_status_user_data, message: { ub: {user_key: "31415926", date: "20151031135905"} })
|
||||||
|
@authenticator.auth
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should call the permissions check method' do
|
||||||
|
allow(@authenticator).to receive(:manager_exists?).and_return(true)
|
||||||
|
expect(@authenticator.send(:client)).to receive(:call).with(:get_applications_user_list, message: { ub: {user_key: "31415926"} })
|
||||||
|
@authenticator.auth
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
require 'rails_helper'
|
|
||||||
|
|
||||||
describe Manager do
|
|
||||||
|
|
||||||
describe "valid?" do
|
|
||||||
|
|
||||||
let(:manager) { create(:manager) }
|
|
||||||
|
|
||||||
it "is false when username is blank" do
|
|
||||||
manager.username = nil
|
|
||||||
expect(manager).to_not be_valid
|
|
||||||
end
|
|
||||||
it "is false when password is blank" do
|
|
||||||
manager.password_digest = nil
|
|
||||||
expect(manager).to_not be_valid
|
|
||||||
end
|
|
||||||
|
|
||||||
it "is true if username and password present" do
|
|
||||||
expect(manager).to be_valid
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "self.valid_manager" do
|
|
||||||
before(:all) { create(:manager, username: "Silvia" ,password: "supersecret") }
|
|
||||||
|
|
||||||
it "is false when username is blank" do
|
|
||||||
expect(Manager.valid_manager(nil, "supersecret")).to be_blank
|
|
||||||
end
|
|
||||||
it "is false when password is blank" do
|
|
||||||
expect(Manager.valid_manager("Silvia", nil)).to be_blank
|
|
||||||
end
|
|
||||||
|
|
||||||
it "is false if manager unexistent" do
|
|
||||||
expect(Manager.valid_manager("Manager", "supersecret")).to be_blank
|
|
||||||
end
|
|
||||||
|
|
||||||
it "is false if wrong password unexistent" do
|
|
||||||
expect(Manager.valid_manager("Silvia", "wrong")).to be_blank
|
|
||||||
end
|
|
||||||
|
|
||||||
it "is true if right username/password combination" do
|
|
||||||
expect(Manager.valid_manager("Silvia", "supersecret")).to be_present
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
@@ -24,8 +24,10 @@ module CommonActions
|
|||||||
click_button 'Log in'
|
click_button 'Log in'
|
||||||
end
|
end
|
||||||
|
|
||||||
def login_as_manager(manager)
|
def login_as_manager
|
||||||
visit management_sign_in_path(login: manager.username, clave_usuario: manager.password)
|
login, user_key, date = "JJB042", "31415926", Time.now.strftime("%Y%m%d%H%M%S")
|
||||||
|
allow_any_instance_of(ManagerAuthenticator).to receive(:auth).and_return({login: login, user_key: user_key, date: date})
|
||||||
|
visit management_sign_in_path(login: login, clave_usuario: user_key, fecha_conexion: date)
|
||||||
end
|
end
|
||||||
|
|
||||||
def login_managed_user(user)
|
def login_managed_user(user)
|
||||||
|
|||||||
Reference in New Issue
Block a user