- Introduced `ConnectedAppFactory` for generating connected app data. - Added `MockDataSeeder` to create sample roles, users, and apps with dynamic role-app assignments. - Extended `RoleAndAppsPermissionEnum` with `ConnectApps` permission. - Updated access-manager UI to handle app-connection actions with proper authorization. - Integrated app-role connection logic in Livewire component and updated `roles-and-apps` view for permissions-based actions.
80 lines
2.8 KiB
PHP
80 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Data\ConnectedApp\ConnectAppRequest;
|
|
use App\Data\Role\AssignAppsToRoleData;
|
|
use App\Models\{ConnectedApp, Role, User};
|
|
use App\Services\{AppRoleService, ConnectedAppService};
|
|
use Illuminate\Database\Seeder;
|
|
|
|
class MockDataSeeder extends Seeder
|
|
{
|
|
public function __construct(
|
|
private AppRoleService $appRoleService,
|
|
private ConnectedAppService $connectedAppService,
|
|
) {}
|
|
|
|
public function run(): void
|
|
{
|
|
// 1. Create 5 roles with different priorities
|
|
$roleNames = ['Manager', 'Editor', 'Auditor', 'Developer', 'Support'];
|
|
$priorities = [10, 20, 30, 40, 50];
|
|
$roles = [];
|
|
|
|
foreach ($roleNames as $index => $name) {
|
|
$role = Role::findOrCreate($name, 'web');
|
|
$role->update(['priority' => $priorities[$index]]);
|
|
$roles[] = $role;
|
|
}
|
|
|
|
// 2. Create 5 users and assign them role combinations
|
|
$users = User::factory()->count(5)->create();
|
|
|
|
// Assign combinations
|
|
$users[0]->assignRole(['Manager', 'Editor']);
|
|
$users[1]->assignRole(['Developer', 'Support']);
|
|
$users[2]->assignRole(['Auditor']);
|
|
$users[3]->assignRole(['Manager', 'Developer']);
|
|
$users[4]->assignRole(['Support']);
|
|
|
|
// 3. Create 10 apps using ConnectedAppService to automatically register permissions
|
|
$apps = [];
|
|
$factory = ConnectedApp::factory();
|
|
|
|
for ($i = 0; $i < 10; $i++) {
|
|
$data = $factory->definition();
|
|
$request = new ConnectAppRequest(
|
|
name: $data['name'],
|
|
connectionProviderId: $data['connection_provider_id'],
|
|
connectionProtocolId: $data['connection_protocol_id'],
|
|
slug: $data['slug'],
|
|
connectionStatusId: $data['connection_status_id'],
|
|
);
|
|
|
|
$this->connectedAppService->create($request);
|
|
$apps[] = ConnectedApp::where('slug', $data['slug'])->firstOrFail();
|
|
}
|
|
|
|
// 4. Connect apps to roles using AppRoleService to automatically assign permissions
|
|
$roleAppsMapping = [
|
|
'Manager' => [$apps[0]->id, $apps[1]->id, $apps[2]->id],
|
|
'Editor' => [$apps[2]->id, $apps[3]->id, $apps[4]->id],
|
|
'Auditor' => [$apps[5]->id],
|
|
'Developer' => [$apps[6]->id, $apps[7]->id, $apps[8]->id],
|
|
'Support' => [$apps[8]->id, $apps[9]->id],
|
|
];
|
|
|
|
foreach ($roleAppsMapping as $roleName => $appIds) {
|
|
$role = Role::findByName($roleName, 'web');
|
|
$this->appRoleService->assign(new AssignAppsToRoleData(
|
|
roleId: $role->id,
|
|
validUpto: now()->addYears(5)->toDateTimeString(),
|
|
appIds: $appIds
|
|
));
|
|
}
|
|
}
|
|
}
|