Skip to content

Commit 0017ff4

Browse files
author
Scotty Hagan
authored
Merge pull request tractionguest#28 from tractionguest/bugfix-scim-azure
handle SCIM schema extensions on patch_update
2 parents 9c2d9a5 + e0e8978 commit 0017ff4

File tree

8 files changed

+46
-29
lines changed

8 files changed

+46
-29
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
scim_rails (0.3.1)
4+
scim_rails (0.3.2)
55
jwt (>= 1.5, < 3.0)
66
rails (~> 5.0)
77

app/controllers/scim_rails/application_controller.rb

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ def extract_from_inside_quotations(string)
5959
end
6060

6161
def extract_path_params(operation)
62-
operation.key?("path") ? process_path(operation) : nil
62+
return nil unless operation.key?("path")
63+
64+
if operation["path"].include?("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User")
65+
return process_extension_schema_path(operation)
66+
end
67+
68+
process_path(operation)
6369
end
6470

6571
def extract_active_param(operation, path_params)
@@ -102,6 +108,12 @@ def process_path(operation)
102108
end
103109
end
104110

111+
def process_extension_schema_path(operation)
112+
key = operation["path"].split(":").last
113+
114+
{"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User": { "#{key}": operation["value"] }}
115+
end
116+
105117
# `process_filter_path` is a method that, like the `process_path`
106118
# method above, parses the string in the "path" key of a PATCH
107119
# operation. It converts the operation into a Hash that resembles
@@ -238,34 +250,18 @@ def update_status(object)
238250
deprovision_method = ScimRails.config.group_deprovision_method
239251
end
240252

241-
object.public_send(reprovision_method) if active?
242-
object.public_send(deprovision_method) unless active?
243-
end
244-
245-
def active?
246-
active = params[:active]
247-
248-
case active
249-
when true, "true", 1
250-
true
251-
when false, "false", 0
252-
false
253-
else
254-
raise ActiveRecord::RecordInvalid
253+
if patch_status(params[:active])
254+
object.public_send(reprovision_method)
255+
else
256+
object.public_send(deprovision_method)
255257
end
256258
end
257259

258260
def patch_status(active_param)
259-
case active_param
260-
when true, "true", 1
261-
true
262-
when false, "false", 0
263-
false
264-
when nil
265-
nil
266-
else
267-
raise ScimRails::ExceptionHandler::InvalidActiveParam
268-
end
261+
# Must use .to_s.downcase to handle strings "True" and "False"
262+
# Which is sent by Azure AD
263+
# Other possible inputs (true, "true", false, "false", 0, 1, nil)
264+
ActiveModel::Type::Boolean.new.cast(active_param.to_s.downcase)
269265
end
270266
end
271267
end

app/controllers/scim_rails/scim_users_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def patch_update
9393
path_params = extract_path_params(operation)
9494
changed_attributes = permitted_params(path_params || operation["value"], "User").merge(get_multi_value_attrs(operation))
9595

96-
user.update!(changed_attributes.compact)
96+
user.assign_attributes(changed_attributes.compact)
9797

9898
active_param = extract_active_param(operation, path_params)
9999
status = patch_status(active_param)
@@ -104,6 +104,8 @@ def patch_update
104104
user.public_send(provision_method)
105105
end
106106

107+
user.save!
108+
107109
ScimRails.config.after_scim_response.call(user, "UPDATED") unless ScimRails.config.after_scim_response.nil?
108110

109111
json_scim_response(object: user)

lib/scim_rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ScimRails
2-
VERSION = '0.3.1'
2+
VERSION = '0.3.2'
33
end

spec/controllers/scim_rails/scim_users_controller_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,15 @@
800800
end
801801
end
802802
end
803+
804+
context "with urn:ietf:params:scim:schemas:extension:enterprise:2.0:User path" do
805+
let(:patch_path) { "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:department" }
806+
let(:patch_value) { Faker::Commerce.department }
807+
808+
it "updates :department attribute" do
809+
expect(company_user.department).to eq(patch_value)
810+
end
811+
end
803812
end
804813

805814
context "when add operation" do
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddDepaartmentToUser < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :users, :department, :string
4+
end
5+
end

spec/dummy/db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2021_03_31_202326) do
13+
ActiveRecord::Schema.define(version: 2021_08_19_053930) do
1414

1515
create_table "companies", force: :cascade do |t|
1616
t.string "name", null: false
@@ -51,6 +51,7 @@
5151
t.datetime "created_at", null: false
5252
t.datetime "updated_at", null: false
5353
t.boolean "scoped_attribute", default: true
54+
t.string "department"
5455
end
5556

5657
end

spec/support/scim_rails_config.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
:last_name,
3131
:email,
3232
:test_attribute,
33+
:department
3334
]
3435

3536
config.mutable_group_attributes = [
@@ -63,6 +64,9 @@
6364
}
6465
],
6566
testAttribute: :test_attribute,
67+
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User": {
68+
department: :department,
69+
},
6670
}
6771

6872
config.mutable_group_attributes_schema = {

0 commit comments

Comments
 (0)