Skip to content

Commit 8eba81d

Browse files
authored
Merge pull request #2 from grokability/postprocessing_middleware
Initial stab at middelware that reformats SCIM responses
2 parents 9e7a8fd + 7754ef4 commit 8eba81d

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

config/scim.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

33
return [
4+
"trace" => env('SCIM_TRACE',false),
5+
// below, if we ever get 'sure' that we can change this default to 'true' we should
6+
"standards_compliance" => env('SCIM_STANDARDS_COMPLIANCE', false),
47
"publish_routes" => true
58
];

src/Middleware/SCIMHeaders.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66
use Illuminate\Http\Request;
77
use ArieTimmerman\Laravel\SCIMServer\Exceptions\SCIMException;
88

9+
function undefault_schema(\stdClass $parsed_json)
10+
{
11+
foreach($parsed_json AS $key => $value) {
12+
if (stristr($key,'urn:ietf:params:scim:schemas:core:') !== false) {
13+
unset($parsed_json->{$key}); //yank it out
14+
foreach($value AS $subkey => $subval) { //iterate through *its* subkey/subvals...
15+
// TODO should we check if the original keys exist? Only overwrite them if they don't?
16+
$parsed_json->{$subkey} = $subval;
17+
}
18+
}
19+
}
20+
return $parsed_json;
21+
}
22+
923
class SCIMHeaders
1024
{
1125
public function handle(Request $request, Closure $next)
@@ -15,6 +29,21 @@ public function handle(Request $request, Closure $next)
1529
}
1630

1731
$response = $next($request);
32+
33+
if(config('scim.standards_compliance')) {
34+
$response_content = json_decode($response->content());
35+
36+
if (!$response_content->totalResults) {
37+
$response->setContent(json_encode(undefault_schema($response_content)));
38+
} else {
39+
$final_response = [];
40+
foreach ($response_content->Resources as $index => $object) {
41+
$final_response[] = undefault_schema($object);
42+
}
43+
$response_content->Resources = $final_response;
44+
$response->setContent(json_encode($response_content));
45+
}
46+
}
1847

1948
return $response->header('Content-Type', 'application/scim+json');
2049
}

0 commit comments

Comments
 (0)