Commit 1b737064412c4426029c2fd3a40b94e23a418321
1 parent
25af95e0
Exists in
master
• add users controller
Showing
1 changed file
with
104 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,104 @@ | @@ -0,0 +1,104 @@ | ||
1 | +module ScimRails | ||
2 | + class ScimUsersController < ScimRails::ApplicationController | ||
3 | + def index | ||
4 | + if params[:filter].present? | ||
5 | + query = ScimRails::ScimQueryParser.new(params[:filter]) | ||
6 | + | ||
7 | + users = @company.public_send(ScimRails.config.scim_users_scope).where( | ||
8 | + "#{ScimRails.config.scim_users_model.connection.quote_column_name(query.attribute)} #{query.operator} ?", | ||
9 | + query.parameter | ||
10 | + ) | ||
11 | + | ||
12 | + counts = ScimCount.new( | ||
13 | + start_index: params[:startIndex], | ||
14 | + limit: params[:count], | ||
15 | + total: users.count | ||
16 | + ) | ||
17 | + json_scim_response(object: users, counts: counts) | ||
18 | + else | ||
19 | + users = @company.public_send(ScimRails.config.scim_users_scope) | ||
20 | + | ||
21 | + counts = ScimCount.new( | ||
22 | + start_index: params[:startIndex], | ||
23 | + limit: params[:count], | ||
24 | + total: users.count | ||
25 | + ) | ||
26 | + | ||
27 | + json_scim_response(object: users, counts: counts) | ||
28 | + end | ||
29 | + end | ||
30 | + | ||
31 | + def create | ||
32 | + user = @company.public_send(ScimRails.config.scim_users_scope).new | ||
33 | + user.create!(permitted_user_params) | ||
34 | + | ||
35 | + json_scim_response(object: user, status: :created) | ||
36 | + end | ||
37 | + | ||
38 | + def show | ||
39 | + user = @company.public_send(ScimRails.config.scim_users_scope).find(params[:id]) | ||
40 | + json_scim_response(object: user) | ||
41 | + end | ||
42 | + | ||
43 | + def update | ||
44 | + user = @company.public_send(ScimRails.config.scim_users_scope).find(params[:id]) | ||
45 | + update_status(user) unless params[:active].nil? | ||
46 | + user.update!(permitted_user_params) | ||
47 | + json_scim_response(object: user) | ||
48 | + end | ||
49 | + | ||
50 | + def deprovision | ||
51 | + user = @company.public_send(ScimRails.config.scim_users_scope).find(params[:id]) | ||
52 | + update_status(user) unless params[:active].nil? | ||
53 | + json_scim_response(object: user) | ||
54 | + end | ||
55 | + | ||
56 | + private | ||
57 | + | ||
58 | + def permitted_user_params | ||
59 | + ScimRails.config.mutable_user_attributes.each.with_object({}) do |attribute, hash| | ||
60 | + hash[attribute] = find_value_for(attribute) | ||
61 | + end | ||
62 | + end | ||
63 | + | ||
64 | + def find_value_for(attribute) | ||
65 | + params.dig(*path_for(attribute)) | ||
66 | + end | ||
67 | + | ||
68 | + def path_for(attribute, object = ScimRails.config.mutable_user_attributes_schema, path = []) | ||
69 | + at_path = path.empty? ? object : object.dig(*path) | ||
70 | + return path if at_path == attribute | ||
71 | + | ||
72 | + case at_path | ||
73 | + when Hash | ||
74 | + at_path.each do |key, value| | ||
75 | + found_path = path_for(attribute, object, [*path, key]) | ||
76 | + return found_path if found_path | ||
77 | + end | ||
78 | + nil | ||
79 | + when Array | ||
80 | + at_path.each_with_index do |value, index| | ||
81 | + found_path = path_for(attribute, object, [*path, index]) | ||
82 | + return found_path if found_path | ||
83 | + end | ||
84 | + nil | ||
85 | + end | ||
86 | + end | ||
87 | + | ||
88 | + def update_status(user) | ||
89 | + user.public_send(ScimRails.config.user_reprovision_method) if active? | ||
90 | + user.public_send(ScimRails.config.user_deprovision_method) unless active? | ||
91 | + end | ||
92 | + | ||
93 | + def active? | ||
94 | + case params[:active] | ||
95 | + when true, "true", 1 | ||
96 | + true | ||
97 | + when false, "false", 0 | ||
98 | + false | ||
99 | + else | ||
100 | + raise ActiveRecord::RecordInvalid | ||
101 | + end | ||
102 | + end | ||
103 | + end | ||
104 | +end |