Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First integration with the gem personnummer #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ GEM
activerecord (>= 4.0, < 5.2)
parser (2.5.0.5)
ast (~> 2.4.0)
personnummer (0.1.0)
pg (0.21.0)
pg_search (2.0.1)
activerecord (>= 4.2)
Expand Down Expand Up @@ -572,6 +573,7 @@ DEPENDENCIES
omniauth-twitter (~> 1.4.0)
paperclip (~> 5.2.1)
paranoia (~> 2.4.0)
personnummer
pg (~> 0.21.0)
pg_search (~> 2.0.1)
puma (~> 3.12.0)
Expand Down
4 changes: 3 additions & 1 deletion Gemfile_custom
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ gem 'puma', '~> 3.12.0'
gem 'aws-sdk', '~> 2'
#gem 'rack-timeout'
gem 'rails_12factor', :group => :production

gem 'heroku-deflater', '~> 0.6.3', :group => :production
gem 'personnummer'

group :test do
gem 'fuubar'
end

ruby '2.3.2'
ruby '2.3.2'
15 changes: 13 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,23 @@ def public_comments
public_activity? ? comments : User.none
end

# overwritting of Devise method to allow login using email OR username
# overwritting of Devise method to allow login using email OR username OR personal number
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:login)
begin
personal_number = Personnummer.new(login)
if personal_number.valid?
personal_number = personal_number.to_s.delete '-'
else
personal_number = nil
end
rescue ArgumentError
personal_number = nil
end
where(conditions.to_hash).where(["lower(email) = ?", login.downcase]).first ||
where(conditions.to_hash).where(["username = ?", login]).first
where(conditions.to_hash).where(["username = ?", login]).first ||
where(conditions.to_hash).where(document_type: '1').where(["document_number = ?", personal_number]).first
end

def interests
Expand Down
Empty file removed config/locales/custom/.keep
Empty file.
5 changes: 5 additions & 0 deletions config/locales/custom/en/activerecord.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
en:
activerecord:
attributes:
user:
login: "Email or username or personal number"
5 changes: 5 additions & 0 deletions config/locales/custom/en/devise_views.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
en:
devise_views:
sessions:
new:
login_label: Email or username or personal number
7 changes: 7 additions & 0 deletions config/locales/custom/en/verification.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sv:
verification:
residence:
new:
document_number_help_text_html: '<strong>Personnummer</strong>: 199206239176<br> <strong>Pass</strong>: AAA000001<br> <strong>Övrig identitetshandling</strong>: X1234567P'
document_type:
spanish_id: Personnummer
5 changes: 5 additions & 0 deletions config/locales/custom/sv/activerecord.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sv:
activerecord:
attributes:
user:
login: "E-post eller användarnamn eller personnummer"
5 changes: 5 additions & 0 deletions config/locales/custom/sv/devise_views.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sv:
devise_views:
sessions:
new:
login_label: "E-post eller användarnamn eller personnummer"
7 changes: 7 additions & 0 deletions config/locales/custom/sv/verification.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
en:
verification:
residence:
new:
document_number_help_text_html: '<strong>Personal Number</strong>: 199206239176<br> <strong>Passport</strong>: AAA000001<br> <strong>Residence card</strong>: X1234567P'
document_type:
spanish_id: Personal number
1 change: 1 addition & 0 deletions lib/census_caller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class CensusCaller
def call(document_type, document_number)
response = CensusApi.new.call(document_type, document_number)
response = LocalCensus.new.call(document_type, document_number) unless response.valid?
response = PersonnummerApi.new.call(document_type, document_number) unless response.valid?

response
end
Expand Down
64 changes: 64 additions & 0 deletions lib/personnummer_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class PersonnummerApi

def call(document_type, document_number)
Response.new(document_type, document_number)
end

class Response
def initialize(document_type, document_number)
@document_type = document_type
@document_number = document_number
if personnummer?
begin
@personnummer = Personnummer.new(document_number)
end
end
end

def valid?
if personnummer?
valid_personnummer?
else
true
end
end

def date_of_birth
if valid_personnummer?
@personnummer.born
else
nil
end
end

def region
if valid_personnummer?
@personnummer.region
else
nil
end
end

def gender
if valid_personnummer?
if @personnummer.female?
"female"
else
"male"
end
else
nil
end
end

private

def personnummer?
@document_type.to_s == "1"
end

def valid_personnummer?
personnummer? && @personnummer != nil && @personnummer.valid?
end
end
end
65 changes: 65 additions & 0 deletions spec/features/custom/users_auth_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'rails_helper'

feature 'Users' do

context 'Regular authentication' do

context 'Sign in' do

scenario 'with short personal number' do
create(:user, email: 'manuela@consul.dev', document_type: '1',
document_number: '9305249170', password: 'judgementday')

visit '/'
click_link 'Sign in'
fill_in 'user_login', with: '9305249170'
fill_in 'user_password', with: 'judgementday'
click_button 'Enter'

expect(page).to have_content 'You have been signed in successfully.'
end

scenario 'with long personal number' do
create(:user, email: 'manuela@consul.dev', document_type: '1',
document_number: '9305249170', password: 'judgementday')

visit '/'
click_link 'Sign in'
fill_in 'user_login', with: '199305249170'
fill_in 'user_password', with: 'judgementday'
click_button 'Enter'

expect(page).to have_content 'You have been signed in successfully.'
end

scenario 'with long hyphenated personal number' do
create(:user, email: 'manuela@consul.dev', document_type: '1',
document_number: '9305249170', password: 'judgementday')

visit '/'
click_link 'Sign in'
fill_in 'user_login', with: '19930524-9170'
fill_in 'user_password', with: 'judgementday'
click_button 'Enter'

expect(page).to have_content 'You have been signed in successfully.'
end

scenario 'with incomplete personal number' do
create(:user, email: 'manuela@consul.dev', document_type: '1',
document_number: '9305249170', password: 'judgementday')

visit '/'
click_link 'Sign in'
fill_in 'user_login', with: '930524917'
fill_in 'user_password', with: 'judgementday'
click_button 'Enter'

expect(page).to have_content 'Invalid login or password'
end

end

end

end