aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/organisation.rb2
-rw-r--r--app/models/workbench.rb2
-rw-r--r--lib/stif/my_workbench_scopes.rb22
-rw-r--r--spec/models/organisation_spec.rb2
-rw-r--r--spec/models/user_spec.rb2
5 files changed, 26 insertions, 4 deletions
diff --git a/app/models/organisation.rb b/app/models/organisation.rb
index f98de567e..31443d1c7 100644
--- a/app/models/organisation.rb
+++ b/app/models/organisation.rb
@@ -43,7 +43,7 @@ class Organisation < ActiveRecord::Base
org = Organisation.find_or_initialize_by(code: code)
if scope
org.sso_attributes ||= {}
- org.sso_attributes[:functional_scope] = scope.delete('\\"')
+ org.sso_attributes[:functional_scope] = scope
end
org.name = name
org.synced_at = Time.now
diff --git a/app/models/workbench.rb b/app/models/workbench.rb
index adc0041dd..f02c6a5a1 100644
--- a/app/models/workbench.rb
+++ b/app/models/workbench.rb
@@ -3,7 +3,7 @@ class Workbench < ActiveRecord::Base
belongs_to :line_referential
belongs_to :stop_area_referential
- has_many :lines, through: :line_referential
+ has_many :lines, -> (workbench) { Stif::MyWorkbenchScopes.new(workbench).line_scope }, through: :line_referential
has_many :networks, through: :line_referential
has_many :companies, through: :line_referential
has_many :group_of_lines, through: :line_referential
diff --git a/lib/stif/my_workbench_scopes.rb b/lib/stif/my_workbench_scopes.rb
new file mode 100644
index 000000000..ed3432ad3
--- /dev/null
+++ b/lib/stif/my_workbench_scopes.rb
@@ -0,0 +1,22 @@
+class Stif::MyWorkbenchScopes
+ attr_accessor :organisation
+
+ def initialize(workbench)
+ @workbench = workbench
+ end
+
+ def line_scope
+ scope = Chouette::Line
+ ids = self.parse_functional_scope
+ ids ? scope.where(objectid: ids) : scope.all
+ end
+
+ def parse_functional_scope
+ return false unless @workbench.organisation.sso_attributes
+ begin
+ line_ids = JSON.parse @workbench.organisation.sso_attributes['functional_scope']
+ rescue Exception => e
+ Rails.logger.error "MyWorkbenchScopes : #{e}"
+ end
+ end
+end
diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb
index c0aba2eb8..823ee7ea6 100644
--- a/spec/models/organisation_spec.rb
+++ b/spec/models/organisation_spec.rb
@@ -36,7 +36,7 @@ describe Organisation, :type => :model do
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]"
+ expect(org.sso_attributes['functional_scope']).to eq "[\"STIF:CODIFLIGNE:Line:C00840\", \"STIF:CODIFLIGNE:Line:C00086\"]"
end
it 'should update existing organisations' do
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index c20e80ca1..bb43be63e 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -36,7 +36,7 @@ describe User, :type => :model do
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]"
+ 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