47 lines
3.5 KiB
PHP
47 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Support;
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Identity\Services\Permissions;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
final class IdentityFixture
|
|
{
|
|
public static function create(string $emailSuffix = ''): array
|
|
{
|
|
\App\Shared\Testing\TestingDatabaseGuard::assertSafe();
|
|
$now = now();
|
|
$tenant = DB::table('tenants')->insertGetId(['name' => 'Cliente de demonstração', 'created_at' => $now, 'updated_at' => $now]);
|
|
$otherTenant = DB::table('tenants')->insertGetId(['name' => 'Outro cliente sintético', 'created_at' => $now, 'updated_at' => $now]);
|
|
$company = DB::table('companies')->insertGetId(['tenant_id' => $tenant, 'name' => 'Empresa Horizonte — Demonstração', 'created_at' => $now, 'updated_at' => $now]);
|
|
$sibling = DB::table('companies')->insertGetId(['tenant_id' => $tenant, 'name' => 'Empresa Maré — Demonstração', 'created_at' => $now, 'updated_at' => $now]);
|
|
$foreign = DB::table('companies')->insertGetId(['tenant_id' => $otherTenant, 'name' => 'Empresa externa sintética', 'created_at' => $now, 'updated_at' => $now]);
|
|
$unit = DB::table('units')->insertGetId(['tenant_id' => $tenant, 'company_id' => $company, 'name' => 'Unidade Central — Demonstração', 'created_at' => $now, 'updated_at' => $now]);
|
|
$hidden = DB::table('units')->insertGetId(['tenant_id' => $tenant, 'company_id' => $company, 'name' => 'Unidade Reservada', 'created_at' => $now, 'updated_at' => $now]);
|
|
$siblingUnit = DB::table('units')->insertGetId(['tenant_id' => $tenant, 'company_id' => $sibling, 'name' => 'Unidade Maré', 'created_at' => $now, 'updated_at' => $now]);
|
|
$password = bin2hex(random_bytes(20));
|
|
$manager = User::create(['name' => 'Gestor de Demonstração', 'email' => 'gestor'.$emailSuffix.'@example.test', 'password' => $password]);
|
|
$reader = User::create(['name' => 'Consulta de Demonstração', 'email' => 'consulta'.$emailSuffix.'@example.test', 'password' => $password]);
|
|
$limited = User::create(['name' => 'Gestor Limitado', 'email' => 'limitado'.$emailSuffix.'@example.test', 'password' => $password]);
|
|
foreach ([$manager, $reader, $limited] as $user) {
|
|
DB::table('company_memberships')->insert(['user_id' => $user->id, 'tenant_id' => $tenant, 'company_id' => $company]);
|
|
self::grant($user->id, $tenant, $company, 'company.view');
|
|
self::grant($user->id, $tenant, $company, 'unit.view', $unit);
|
|
}
|
|
foreach (Permissions::COMPANY as $permission) { self::grant($manager->id, $tenant, $company, $permission); }
|
|
self::grant($manager->id, $tenant, $company, 'unit.edit', $unit);
|
|
self::grant($limited->id, $tenant, $company, 'access.manage');
|
|
DB::table('company_memberships')->insert(['user_id' => $manager->id, 'tenant_id' => $tenant, 'company_id' => $sibling]);
|
|
self::grant($manager->id, $tenant, $sibling, 'company.view');
|
|
self::grant($manager->id, $tenant, $sibling, 'unit.view', $siblingUnit);
|
|
return compact('tenant', 'otherTenant', 'company', 'sibling', 'foreign', 'unit', 'hidden', 'siblingUnit', 'manager', 'reader', 'limited', 'password');
|
|
}
|
|
|
|
public static function grant(int $user, int $tenant, int $company, string $permission, ?int $unit = null): void
|
|
{
|
|
DB::table('permission_grants')->insertOrIgnore(['user_id' => $user, 'tenant_id' => $tenant, 'company_id' => $company,
|
|
'permission' => $permission, 'unit_id' => $unit, 'created_at' => now(), 'updated_at' => now()]);
|
|
}
|
|
}
|