Skip to content

Commit f72963d

Browse files
Simplified validation (#71)
* dropped support for laravel 6
1 parent 6a2b207 commit f72963d

File tree

6 files changed

+18
-69
lines changed

6 files changed

+18
-69
lines changed

.github/workflows/ci.yml

+1-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ jobs:
99
strategy:
1010
matrix:
1111
php: [7.3, 7.4, 8.0, 8.1, 8.2]
12-
laravel: [6.*, 7.*, 8.*, 9.*, 10.*, 11.*]
12+
laravel: [7.*, 8.*, 9.*, 10.*, 11.*]
1313
exclude:
14-
- laravel: 6.*
15-
php: 8.1
16-
- laravel: 6.*
17-
php: 8.2
1814
- laravel: 7.*
1915
php: 8.1
2016
- laravel: 7.*

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
],
1212
"require": {
1313
"php": "^7.0|^8.0",
14-
"illuminate/database": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
15-
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
16-
"illuminate/console": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
14+
"illuminate/database": "^7.0|^8.0|^9.0|^10.0|^11.0",
15+
"illuminate/support": "^7.0|^8.0|^9.0|^10.0|^11.0",
16+
"illuminate/console": "^7.0|^8.0|^9.0|^10.0|^11.0",
1717
"tmilos/scim-schema": "^0.1.0",
1818
"tmilos/scim-filter-parser": "^1.3"
1919
},

composer.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Http/Controllers/ResourceController.php

+5-52
Original file line numberDiff line numberDiff line change
@@ -27,72 +27,25 @@ protected static function isAllowed(PolicyDecisionPoint $pdp, Request $request,
2727
return $pdp->isAllowed($request, $operation, $attributes, $resourceType, $resourceObject, $isMe);
2828
}
2929

30-
protected static function replaceKeys(array $input)
31-
{
32-
$return = array();
33-
foreach ($input as $key => $value) {
34-
if (strpos($key, '_') > 0) {
35-
$key = str_replace('___', '.', $key);
36-
}
37-
38-
if (is_array($value)) {
39-
$value = self::replaceKeys($value);
40-
}
41-
42-
$return[$key] = $value;
43-
}
44-
return $return;
45-
}
46-
4730
protected static function validateScim(ResourceType $resourceType, $flattened, ?Model $resourceObject)
4831
{
49-
$objectPreparedForValidation = [];
5032
$validations = $resourceType->getValidations();
51-
$simpleValidations = [];
52-
53-
/**
54-
* Dots have a different meaning in SCIM and in Laravel's validation logic
55-
*/
56-
foreach ($flattened as $key => $value) {
57-
$objectPreparedForValidation[preg_replace('/([^*])\.([^*])/', '${1}___${2}', $key)] = $value;
58-
}
5933

6034
foreach ($validations as $key => $value) {
61-
$simpleValidations[
62-
preg_replace('/([^*])\.([^*])/', '${1}___${2}', $key)
63-
] = !is_string($value) ? $value : ($resourceObject != null ? preg_replace('/,\[OBJECT_ID\]/', ',' . $resourceObject->id, $value) : str_replace(',[OBJECT_ID]', '', $value));
35+
if (is_string($value)) {
36+
$validations[$key] = $resourceObject ? preg_replace('/,\[OBJECT_ID\]/', ',' . $resourceObject->id, $value) : str_replace(',[OBJECT_ID]', '', $value);
37+
}
6438
}
6539

66-
$validator = Validator::make($objectPreparedForValidation, $simpleValidations);
40+
$validator = Validator::make($flattened, $validations);
6741

6842
if ($validator->fails()) {
6943
$e = $validator->errors();
70-
$e = self::replaceKeys($e->toArray());
7144

7245
throw (new SCIMException('Invalid data!'))->setCode(400)->setScimType('invalidSyntax')->setErrors($e);
7346
}
7447

75-
$validTemp = $validator->validate();
76-
$valid = [];
77-
78-
$keys = collect($simpleValidations)->keys()->map(
79-
function ($rule) {
80-
return explode('.', $rule)[0];
81-
}
82-
)->unique()->toArray();
83-
84-
foreach ($keys as $key) {
85-
if (array_key_exists($key, $validTemp)) {
86-
$valid[$key] = $validTemp[$key];
87-
}
88-
}
89-
90-
$flattened = [];
91-
foreach ($valid as $key => $value) {
92-
$flattened[str_replace(['___'], ['.'], $key)] = $value;
93-
}
94-
95-
return $flattened;
48+
return $validator->validate();
9649
}
9750

9851
public static function createFromSCIM($resourceType, $input, PolicyDecisionPoint $pdp = null, Request $request = null, $allowAlways = false, $isMe = false)

src/SCIMConfig.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ public function getUserConfig()
3434

3535
'validations' => [
3636

37-
'urn:ietf:params:scim:schemas:core:2.0:User:userName' => 'required',
38-
'urn:ietf:params:scim:schemas:core:2.0:User:password' => 'nullable',
39-
'urn:ietf:params:scim:schemas:core:2.0:User:active' => 'boolean',
40-
'urn:ietf:params:scim:schemas:core:2.0:User:emails' => 'required|array',
41-
'urn:ietf:params:scim:schemas:core:2.0:User:emails.*.value' => 'required|email',
42-
'urn:ietf:params:scim:schemas:core:2.0:User:roles' => 'nullable|array',
43-
'urn:ietf:params:scim:schemas:core:2.0:User:roles.*.value' => 'required',
37+
'urn:ietf:params:scim:schemas:core:2\.0:User:userName' => 'required',
38+
'urn:ietf:params:scim:schemas:core:2\.0:User:password' => 'nullable',
39+
'urn:ietf:params:scim:schemas:core:2\.0:User:active' => 'boolean',
40+
'urn:ietf:params:scim:schemas:core:2\.0:User:emails' => 'required|array',
41+
'urn:ietf:params:scim:schemas:core:2\.0:User:emails.*.value' => 'required|email',
42+
'urn:ietf:params:scim:schemas:core:2\.0:User:roles' => 'nullable|array',
43+
'urn:ietf:params:scim:schemas:core:2\.0:User:roles.*.value' => 'required',
4444

4545
],
4646

tests/CustomSchemaTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function getUserConfig()
1414
$config = parent::getUserConfig();
1515

1616
$config['schemas'][] = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User';
17-
$config['validations']['urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber'] = 'nullable';
17+
$config['validations']['urn:ietf:params:scim:schemas:extension:enterprise:2\.0:User:employeeNumber'] = 'nullable';
1818

1919
$config['mapping']['urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'] = [
2020
'employeeNumber' => AttributeMapping::eloquent("employeeNumber")

0 commit comments

Comments
 (0)