Your IP : 216.73.216.79


Current Path : /var/www/html/redd/database/seeders/
Upload File :
Current File : /var/www/html/redd/database/seeders/AdminRolePermissionSeeder.php

<?php

namespace Database\Seeders;

use App\Models\Backend\BackendUser;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;

class AdminRolePermissionSeeder extends Seeder
{
    /**
     * Seed admin roles and permissions.
     */
    public function run(): void
    {
        $permissionRegistrar = app(PermissionRegistrar::class);
        $permissionRegistrar->forgetCachedPermissions();

        $permissions = [
            'backend-user.view',
            'backend-user.create',
            'backend-user.update',
            'backend-user.delete',
            'backend-menu.view',
            'backend-menu.create',
            'backend-menu.update',
            'backend-menu.delete',
            'backend-menu.roledelete',
            'backend-menu.assign',
            'backend-menu.level',
            'backend-menu.disabled',
            'activity-log.view',
            'auto-permission.view',
            'auto-permission.create',
            'ref.view',
            'ref.create',
            'ref.update',
            'ref.delete',
            'role.view',
            'role.create',
            'role.update',
            'role.delete',
            'permission.view',
            'permission.create',
            'permission.update',
            'permission.delete',
            'frontend-user.view',
            'frontend-user.create',
            'frontend-user.update',
            'frontend-user.delete',
            'frontend-menu.view',
            'frontend-menu.create',
            'frontend-menu.update',
            'frontend-menu.delete',
            'frontend-menu.roledelete',
            'frontend-menu.assign',
            'frontend-menu.level',
            'frontend-menu.disabled',
            'frontend-menu.article-item',
            'frontend-menu.slider-item',
            'menu-category.view',
            'menu-category.create',
            'menu-category.update',
            'menu-category.delete',
            'file.view',
            'route.view',
            'route.create',
            'route.update',
            'route.delete',
            'content-article.view',
            'content-article.create',
            'content-article.update',
            'content-article.delete',
            'content-application.view',
            'content-application.create',
            'content-application.update',
            'content-application.delete',
            'content-download.view',
            'content-download.create',
            'content-download.update',
            'content-download.delete',
            'content-photo-gallery.view',
            'content-photo-gallery.create',
            'content-photo-gallery.update',
            'content-photo-gallery.delete',
            'content-photo-list.view',
            'content-photo-list.create',
            'content-photo-list.update',
            'content-photo-list.delete',
            'content-slider.view',
            'content-slider.create',
            'content-slider.update',
            'content-slider.delete',
            'content-image.view',
            'content-image.create',
            'content-image.update',
            'content-image.delete',
            'content-video.view',
            'content-video.create',
            'content-video.update',
            'content-video.delete',
            'visitor.view',
            'visitor.update',
            'visitor.delete',
        ];

        foreach ($permissions as $permissionName) {
            Permission::findOrCreate($permissionName, 'admin');
        }

        $permissionRegistrar->forgetCachedPermissions();

        $superAdmin = Role::findOrCreate('super-admin', 'admin');
        $editor = Role::findOrCreate('editor', 'admin');
        $viewer = Role::findOrCreate('viewer', 'admin');

        $adminPermissions = Permission::query()
            ->where('guard_name', 'admin')
            ->whereIn('name', $permissions)
            ->get();

        $superAdmin->syncPermissions($adminPermissions);
        $editor->syncPermissions(
            $adminPermissions->whereIn('name', [
                'backend-user.view',
                'backend-user.create',
                'backend-user.update',
                'activity-log.view',
            ])->values()
        );
        $viewer->syncPermissions(
            $adminPermissions->whereIn('name', [
                'backend-user.view',
                'activity-log.view',
            ])->values()
        );

        $defaultAdmin = BackendUser::where('email', 'superadmin@example.com')->first();
        if ($defaultAdmin) {
            $defaultAdmin->syncRoles(['super-admin']);
        }
    }
}