aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXinhui2016-10-17 13:22:51 +0200
committerXinhui2016-10-17 13:22:51 +0200
commitf282672d581a826411271dc37fd173bf49f30f76 (patch)
tree03b965e3736756c686e54dbd7ae4034fd9718503
parent972c635d3ac481989d3a7da8326d192cf34116ee (diff)
downloadchouette-core-f282672d581a826411271dc37fd173bf49f30f76.tar.bz2
Store organisation functional_scope
Refs #1785
-rw-r--r--app/models/organisation.rb4
-rw-r--r--app/models/user.rb8
-rw-r--r--db/migrate/20161017091304_add_sso_attributes_to_organisations.rb5
-rw-r--r--db/schema.rb5
-rw-r--r--spec/fixtures/organizations.json1
-rw-r--r--spec/models/organisation_spec.rb6
-rw-r--r--spec/models/user_spec.rb9
7 files changed, 33 insertions, 5 deletions
diff --git a/app/models/organisation.rb b/app/models/organisation.rb
index 3b8ad7e57..86386772c 100644
--- a/app/models/organisation.rb
+++ b/app/models/organisation.rb
@@ -43,6 +43,10 @@ class Organisation < ActiveRecord::Base
self.portail_api_request.each do |el|
Organisation.find_or_create_by(code: el['code']).tap do |org|
org.name = el['name']
+ if el['functional_scope']
+ org.sso_attributes ||= {}
+ org.sso_attributes[:functional_scope] = el['functional_scope'].delete('\\"')
+ end
if org.changed?
org.synced_at = Time.now
org.save
diff --git a/app/models/user.rb b/app/models/user.rb
index 31fda6aed..27adb3b34 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -35,7 +35,9 @@ class User < ActiveRecord::Base
self.username = extra[:username]
self.organisation = Organisation.find_or_create_by(code: extra[:organisation_code]).tap do |org|
- org.name = extra[:organisation_name]
+ org.name = extra[:organisation_name]
+ org.sso_attributes ||= {}
+ org.sso_attributes[:functional_scope] = extra[:functional_scope].delete('\\"') if extra[:functional_scope]
org.synced_at = Time.now
end
end
@@ -66,7 +68,9 @@ class User < ActiveRecord::Base
# Set organisation
user.organisation = Organisation.find_or_create_by(code: el['organization_code']).tap do |org|
- org.name = el['organization_name']
+ org.name = el['organization_name']
+ org.sso_attributes ||= {}
+ org.sso_attributes[:functional_scope] = el['functional_scope'].delete('\\"') if el['functional_scope']
org.synced_at = Time.now
end
diff --git a/db/migrate/20161017091304_add_sso_attributes_to_organisations.rb b/db/migrate/20161017091304_add_sso_attributes_to_organisations.rb
new file mode 100644
index 000000000..608e78ef6
--- /dev/null
+++ b/db/migrate/20161017091304_add_sso_attributes_to_organisations.rb
@@ -0,0 +1,5 @@
+class AddSsoAttributesToOrganisations < ActiveRecord::Migration
+ def change
+ add_column :organisations, :sso_attributes, :hstore
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index e8a0c8c7a..9b2c3f1b2 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20161010135256) do
+ActiveRecord::Schema.define(version: 20161017091304) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -356,9 +356,10 @@ ActiveRecord::Schema.define(version: 20161010135256) do
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
- t.string "data_format", default: "neptune"
+ t.string "data_format", default: "neptune"
t.string "code"
t.datetime "synced_at"
+ t.hstore "sso_attributes"
end
create_table "pt_links", force: true do |t|
diff --git a/spec/fixtures/organizations.json b/spec/fixtures/organizations.json
index b70f99520..6b507b445 100644
--- a/spec/fixtures/organizations.json
+++ b/spec/fixtures/organizations.json
@@ -84,6 +84,7 @@
"code": "RATP",
"created_at": "2016-05-17T16:04:26.646Z",
"updated_at": "2016-05-31T10:06:39.349Z",
+ "functional_scope": "[\"STIF:CODIFLIGNE:Line:C00840\", \"STIF:CODIFLIGNE:Line:C00086\"]",
"organization_users": {
"users_count": 8
},
diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb
index 9d9749b18..c0aba2eb8 100644
--- a/spec/models/organisation_spec.rb
+++ b/spec/models/organisation_spec.rb
@@ -33,6 +33,12 @@ describe Organisation, :type => :model do
expect(Organisation.all.map(&:name)).to include 'ALBATRANS', 'OPTILE', 'SNCF', 'STIF'
end
+ it 'should retrieve functional scope' do
+ Organisation.portail_sync
+ org = Organisation.find_by(code: 'RATP')
+ expect(org.sso_attributes['functional_scope']).to eq "[STIF:CODIFLIGNE:Line:C00840, STIF:CODIFLIGNE:Line:C00086]"
+ end
+
it 'should update existing organisations' do
create :organisation, name: 'dummy_name', code:'RATP', updated_at: 10.days.ago
Organisation.portail_sync
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 16fbed27a..c20e80ca1 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -12,7 +12,8 @@ describe User, :type => :model do
:username => 'john.doe',
:email => 'john.doe@af83.com',
:organisation_code => '0083',
- :organisation_name => 'af83'
+ :organisation_name => 'af83',
+ :functional_scope => "[\"STIF:CODIFLIGNE:Line:C00840\", \"STIF:CODIFLIGNE:Line:C00086\"]"
}
ticket.user = "john.doe"
ticket.success = true
@@ -32,6 +33,12 @@ describe User, :type => :model do
expect(Organisation.find_by(code: ticket.extra_attributes[:organisation_code])).to be_truthy
end
+ it 'should store organisation functional_scope' do
+ User.authenticate_with_cas_ticket(ticket)
+ org = Organisation.find_by(code: ticket.extra_attributes[:organisation_code])
+ expect(org.sso_attributes['functional_scope']).to eq "[STIF:CODIFLIGNE:Line:C00840, STIF:CODIFLIGNE:Line:C00086]"
+ end
+
it 'should not create a new organisation if organisation is already present' do
ticket.extra_attributes[:organisation_code] = create(:organisation).code
expect{User.authenticate_with_cas_ticket(ticket)}.not_to change{ Organisation.count }