Skip to content

Commit 48592dd

Browse files
committed
support for roles attribute
1 parent a6da5ec commit 48592dd

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

Dockerfile

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,32 @@ class UserFactory extends Factory
169169
$suffix = $this->faker->unique()->numberBetween(100, 999999);
170170
$username = $base . $suffix;
171171

172+
$roleTemplates = [
173+
['value' => 'admin', 'display' => 'Admin', 'type' => 'system'],
174+
['value' => 'developer', 'display' => 'Developer', 'type' => 'system'],
175+
['value' => 'manager', 'display' => 'Manager', 'type' => 'business'],
176+
['value' => 'support', 'display' => 'Support', 'type' => 'business'],
177+
['value' => 'auditor', 'display' => 'Auditor', 'type' => 'governance'],
178+
];
179+
180+
$selectedRoles = $this->faker->randomElements(
181+
$roleTemplates,
182+
$this->faker->numberBetween(1, count($roleTemplates))
183+
);
184+
$selectedRoles = array_values($selectedRoles);
185+
$primaryIndex = array_rand($selectedRoles);
186+
foreach ($selectedRoles as $index => &$role) {
187+
$role['primary'] = $index === $primaryIndex;
188+
}
189+
unset($role);
190+
172191
return [
173192
'name' => $username, // login username
174193
'formatted' => $formatted, // full name
175194
'email' => "{$username}@example.test",
176195
'password' => bcrypt('test'),
177196
'active' => $this->faker->boolean(),
197+
'roles' => $selectedRoles,
178198
];
179199
}
180200
}
@@ -229,6 +249,7 @@ class User extends Authenticatable
229249
'email',
230250
'password',
231251
'active',
252+
'roles',
232253

233254
];
234255

@@ -240,6 +261,7 @@ class User extends Authenticatable
240261
protected $casts = [
241262
'email_verified_at' => 'datetime',
242263
'active' => 'boolean',
264+
'roles' => 'array',
243265

244266
];
245267

@@ -284,4 +306,3 @@ RUN php artisan vendor:publish --tag=laravel-scim-migrations
284306
RUN php artisan migrate && php artisan db:seed --class=Database\\Seeders\\DemoSeeder
285307

286308
CMD ["php","artisan","serve","--host=0.0.0.0","--port=8000"]
287-

database/migrations/2021_01_01_000003_add_scim_fields_to_users_table.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public function up(): void
1212
if (!Schema::hasColumn('users', 'formatted')) {
1313
$table->string('formatted')->nullable();
1414
}
15+
if (!Schema::hasColumn('users', 'displayName')) {
16+
$table->string('displayName')->nullable();
17+
}
1518
if (!Schema::hasColumn('users', 'active')) {
1619
$table->boolean('active')->default(false);
1720
}
@@ -29,6 +32,9 @@ public function down(): void
2932
if (Schema::hasColumn('users', 'formatted')) {
3033
$table->dropColumn('formatted');
3134
}
35+
if (Schema::hasColumn('users', 'displayName')) {
36+
$table->dropColumn('displayName');
37+
}
3238
if (Schema::hasColumn('users', 'active')) {
3339
$table->dropColumn('active');
3440
}

src/Attribute/JSONCollection.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ public function replace($value, Model &$object, ?Path $path = null)
2929

3030
public function doRead(&$object, $attributes = [])
3131
{
32-
return $object->{$this->attribute}?->values()->all();
32+
$value = $object->{$this->attribute};
33+
34+
if ($value === null) {
35+
return null;
36+
}
37+
38+
return collect($value)->values()->all();
3339
}
3440

3541
public function remove($value, Model &$object, Path $path = null)

0 commit comments

Comments
 (0)