224 lines
8.6 KiB
PHP
224 lines
8.6 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use Illuminate\Database\Seeder;
|
|
use App\Models\DocumentManagerPermission;
|
|
|
|
class DocumentManagerPermissionSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*
|
|
* Creates default permissions for the 3-tier permission system:
|
|
* - read: View files and folders
|
|
* - write: Create, upload, rename, copy, move files and folders
|
|
* - edit: Edit file contents (for supported file types)
|
|
*/
|
|
public function run(): void
|
|
{
|
|
// Clear existing permissions
|
|
DocumentManagerPermission::truncate();
|
|
|
|
// Get all folders dynamically from the documents directory
|
|
$allFolders = $this->getAllFolders();
|
|
|
|
// Define user levels and their permission patterns
|
|
$userLevels = [
|
|
'Admin' => [
|
|
'pattern' => 'full_access',
|
|
'description' => 'Full access to all folders and subfolders'
|
|
],
|
|
'Manager (PTO)' => [
|
|
'pattern' => 'pto_manager',
|
|
'description' => 'PTO folder full access, others read-only'
|
|
],
|
|
'Quality Staff' => [
|
|
'pattern' => 'qa_staff',
|
|
'description' => 'QA folder full access, others read-only'
|
|
],
|
|
'QC Staff' => [
|
|
'pattern' => 'qc_staff',
|
|
'description' => 'Welding Database full access, others read-only'
|
|
],
|
|
'NDT Staff' => [
|
|
'pattern' => 'ndt_staff',
|
|
'description' => 'NDT Release Check List full access, others read-only'
|
|
],
|
|
'Welding Staff' => [
|
|
'pattern' => 'welding_staff',
|
|
'description' => 'Drawings full access, others read-only'
|
|
],
|
|
'Test Pack Engineer' => [
|
|
'pattern' => 'test_pack_engineer',
|
|
'description' => 'Test Pack full access, others read-only'
|
|
],
|
|
'Welding Engineer' => [
|
|
'pattern' => 'welding_engineer',
|
|
'description' => 'Welding Database full access, others read-only'
|
|
],
|
|
'Field Staff' => [
|
|
'pattern' => 'field_staff',
|
|
'description' => 'Limited read access to most folders'
|
|
],
|
|
];
|
|
|
|
// Generate permissions for each user level
|
|
foreach ($userLevels as $userLevel => $config) {
|
|
$permissions = $this->generatePermissions($userLevel, $config['pattern'], $allFolders);
|
|
|
|
foreach ($permissions as $folderPath => $permissionTypes) {
|
|
foreach ($permissionTypes as $permissionType) {
|
|
DocumentManagerPermission::create([
|
|
'user_level' => $userLevel,
|
|
'folder_path' => $folderPath,
|
|
'permission_type' => $permissionType,
|
|
'is_allowed' => true,
|
|
]);
|
|
}
|
|
}
|
|
|
|
$this->command->info("Generated permissions for {$userLevel}: {$config['description']}");
|
|
}
|
|
|
|
$this->command->info('Document Manager Permissions seeded successfully!');
|
|
$this->command->info('Total permissions created: ' . DocumentManagerPermission::count());
|
|
$this->command->info('Total folders processed: ' . count($allFolders));
|
|
}
|
|
|
|
/**
|
|
* Get all folders dynamically from the documents directory using glob()
|
|
*/
|
|
private function getAllFolders(): array
|
|
{
|
|
$documentsPath = storage_path('documents');
|
|
$allFolders = [];
|
|
|
|
if (is_dir($documentsPath)) {
|
|
// Get main folders using glob() - much simpler and faster
|
|
$mainFolders = glob($documentsPath . '/*', GLOB_ONLYDIR);
|
|
|
|
foreach ($mainFolders as $folder) {
|
|
$folderName = basename($folder);
|
|
$allFolders[] = $folderName;
|
|
|
|
// Check for subfolders (one level deep) using glob()
|
|
$subFolders = glob($folder . '/*', GLOB_ONLYDIR);
|
|
foreach ($subFolders as $subFolder) {
|
|
$subFolderName = basename($subFolder);
|
|
$allFolders[] = $folderName . '/' . $subFolderName;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sort folders alphabetically
|
|
sort($allFolders);
|
|
|
|
return $allFolders;
|
|
}
|
|
|
|
/**
|
|
* Generate permissions based on user level pattern
|
|
*/
|
|
private function generatePermissions(string $userLevel, string $pattern, array $allFolders): array
|
|
{
|
|
$permissions = [];
|
|
|
|
switch ($pattern) {
|
|
case 'full_access':
|
|
// Admin gets full access to all folders
|
|
foreach ($allFolders as $folder) {
|
|
$permissions[$folder] = ['read', 'write', 'edit'];
|
|
}
|
|
break;
|
|
|
|
case 'pto_manager':
|
|
// PTO Manager gets full access to PTO folders, read-only to others
|
|
foreach ($allFolders as $folder) {
|
|
if (strpos($folder, '005_PTO') === 0) {
|
|
$permissions[$folder] = ['read', 'write', 'edit'];
|
|
} else {
|
|
$permissions[$folder] = ['read'];
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'qa_staff':
|
|
// Quality Staff gets full access to QA folders, read-only to others
|
|
foreach ($allFolders as $folder) {
|
|
if (strpos($folder, '004_QA') === 0) {
|
|
$permissions[$folder] = ['read', 'write', 'edit'];
|
|
} else {
|
|
$permissions[$folder] = ['read'];
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'qc_staff':
|
|
case 'welding_engineer':
|
|
// QC Staff and Welding Engineer get full access to Welding Database, read-only to others
|
|
foreach ($allFolders as $folder) {
|
|
if (strpos($folder, '003_Welding_Database') === 0) {
|
|
$permissions[$folder] = ['read', 'write', 'edit'];
|
|
} else {
|
|
$permissions[$folder] = ['read'];
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'ndt_staff':
|
|
// NDT Staff gets full access to NDT Release Check List, read-only to others
|
|
foreach ($allFolders as $folder) {
|
|
if (strpos($folder, '0010_Spool_NDT_Release_Check_List') === 0) {
|
|
$permissions[$folder] = ['read', 'write', 'edit'];
|
|
} else {
|
|
$permissions[$folder] = ['read'];
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'welding_staff':
|
|
// Welding Staff gets full access to Drawings, read-only to others
|
|
foreach ($allFolders as $folder) {
|
|
if (strpos($folder, '001_Drawings') === 0) {
|
|
$permissions[$folder] = ['read', 'write', 'edit'];
|
|
} else {
|
|
$permissions[$folder] = ['read'];
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'test_pack_engineer':
|
|
// Test Pack Engineer gets full access to Test Pack, read-only to others
|
|
foreach ($allFolders as $folder) {
|
|
if (strpos($folder, '008_Test_Pack') === 0) {
|
|
$permissions[$folder] = ['read', 'write', 'edit'];
|
|
} else {
|
|
$permissions[$folder] = ['read'];
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'field_staff':
|
|
// Field Staff gets limited read access to most folders
|
|
foreach ($allFolders as $folder) {
|
|
// Exclude system folders and sensitive folders
|
|
if (in_array($folder, ['.tmb', 'cache', 'framework', 'logs', 'test'])) {
|
|
continue; // No access to system folders
|
|
}
|
|
|
|
// Read-only access to most folders
|
|
$permissions[$folder] = ['read'];
|
|
}
|
|
break;
|
|
|
|
default:
|
|
// Default: read-only access to all folders
|
|
foreach ($allFolders as $folder) {
|
|
$permissions[$folder] = ['read'];
|
|
}
|
|
}
|
|
|
|
return $permissions;
|
|
}
|
|
} |