update patch 1
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Admin;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class AdminSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$admin = Admin::where('username', 'superadmin')->first();
|
||||
|
||||
if (is_null($admin)) {
|
||||
$admin = new Admin();
|
||||
$admin->name = "Super Admin";
|
||||
$admin->email = "superadmin@email.com";
|
||||
$admin->username = "superadmin";
|
||||
$admin->password = Hash::make('12345678');
|
||||
$admin->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class DatabaseSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Seed the application's database.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$this->call(UserSeeder::class);
|
||||
$this->call(AdminSeeder::class);
|
||||
$this->call(RolePermissionSeeder::class);
|
||||
$this->call(MenusTableSeeder::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class MenusTableSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
DB::table('menus')->insert([
|
||||
[
|
||||
'id' => 61,
|
||||
'title' => 'Role Manager',
|
||||
'url' => '/admin/roles',
|
||||
'icon' => 'mdi mdi-account-key-outline',
|
||||
'parent_id' => 62,
|
||||
'order' => 1,
|
||||
'created_at' => Carbon::now(),
|
||||
'updated_at' => Carbon::now(),
|
||||
],
|
||||
[
|
||||
'id' => 62,
|
||||
'title' => 'System Administrator',
|
||||
'url' => '#',
|
||||
'icon' => 'mdi mdi-cog-outline',
|
||||
'parent_id' => null,
|
||||
'order' => 99,
|
||||
'created_at' => Carbon::now(),
|
||||
'updated_at' => Carbon::now(),
|
||||
],
|
||||
[
|
||||
'id' => 63,
|
||||
'title' => 'Users List Accounts',
|
||||
'url' => '/admin/admins',
|
||||
'icon' => 'mdi mdi-account-group',
|
||||
'parent_id' => 62,
|
||||
'order' => 0,
|
||||
'created_at' => Carbon::now(),
|
||||
'updated_at' => Carbon::now(),
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Admin;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Spatie\Permission\Models\Role;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
|
||||
/**
|
||||
* Class RolePermissionSeeder.
|
||||
*
|
||||
* @see https://spatie.be/docs/laravel-permission/v5/basic-usage/multiple-guards
|
||||
*
|
||||
* @package App\Database\Seeds
|
||||
*/
|
||||
class RolePermissionSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
|
||||
/**
|
||||
* Enable these options if you need same role and other permission for User Model
|
||||
* Else, please follow the below steps for admin guard
|
||||
*/
|
||||
|
||||
// Create Roles and Permissions
|
||||
// $roleSuperAdmin = Role::create(['name' => 'superadmin']);
|
||||
// $roleAdmin = Role::create(['name' => 'admin']);
|
||||
// $roleEditor = Role::create(['name' => 'editor']);
|
||||
// $roleUser = Role::create(['name' => 'user']);
|
||||
|
||||
|
||||
// Permission List as array
|
||||
$permissions = [
|
||||
|
||||
[
|
||||
'group_name' => 'dashboard',
|
||||
'permissions' => [
|
||||
'dashboard.view',
|
||||
'dashboard.edit',
|
||||
]
|
||||
],
|
||||
[
|
||||
'group_name' => 'admin',
|
||||
'permissions' => [
|
||||
// admin Permissions
|
||||
'admin.create',
|
||||
'admin.view',
|
||||
'admin.edit',
|
||||
'admin.delete',
|
||||
'admin.approve',
|
||||
]
|
||||
],
|
||||
[
|
||||
'group_name' => 'role',
|
||||
'permissions' => [
|
||||
// role Permissions
|
||||
'role.create',
|
||||
'role.view',
|
||||
'role.edit',
|
||||
'role.delete',
|
||||
'role.approve',
|
||||
]
|
||||
],
|
||||
[
|
||||
'group_name' => 'profile',
|
||||
'permissions' => [
|
||||
// profile Permissions
|
||||
'profile.view',
|
||||
'profile.edit',
|
||||
'profile.delete',
|
||||
'profile.update',
|
||||
]
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
// Do same for the admin guard for tutorial purposes.
|
||||
$admin = Admin::where('username', 'superadmin')->first();
|
||||
$roleSuperAdmin = $this->maybeCreateSuperAdminRole($admin);
|
||||
|
||||
// Create and Assign Permissions
|
||||
for ($i = 0; $i < count($permissions); $i++) {
|
||||
$permissionGroup = $permissions[$i]['group_name'];
|
||||
for ($j = 0; $j < count($permissions[$i]['permissions']); $j++) {
|
||||
$permissionExist = Permission::where('name', $permissions[$i]['permissions'][$j])->first();
|
||||
if (is_null($permissionExist)) {
|
||||
$permission = Permission::create(
|
||||
[
|
||||
'name' => $permissions[$i]['permissions'][$j],
|
||||
'group_name' => $permissionGroup,
|
||||
'guard_name' => 'admin'
|
||||
]
|
||||
);
|
||||
$roleSuperAdmin->givePermissionTo($permission);
|
||||
$permission->assignRole($roleSuperAdmin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Assign super admin role permission to superadmin user
|
||||
if ($admin) {
|
||||
$admin->assignRole($roleSuperAdmin);
|
||||
}
|
||||
}
|
||||
|
||||
private function maybeCreateSuperAdminRole($admin): Role
|
||||
{
|
||||
if (is_null($admin)) {
|
||||
$roleSuperAdmin = Role::create(['name' => 'superadmin', 'guard_name' => 'admin']);
|
||||
} else {
|
||||
$roleSuperAdmin = Role::where('name', 'superadmin')->where('guard_name', 'admin')->first();
|
||||
}
|
||||
|
||||
if (is_null($roleSuperAdmin)) {
|
||||
$roleSuperAdmin = Role::create(['name' => 'superadmin', 'guard_name' => 'admin']);
|
||||
}
|
||||
|
||||
return $roleSuperAdmin;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
use App\User;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class UserSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$user = User::where('email', 'user1@gmail.com')->first();
|
||||
if (is_null($user)) {
|
||||
$user = new User();
|
||||
$user->name = "User 1";
|
||||
$user->username = "user1";
|
||||
$user->email = "user1@gmail.com";
|
||||
$user->password = Hash::make('12345678');
|
||||
$user->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user