From c6795b0123de4d5547b3d0b09c95809fe765fb30 Mon Sep 17 00:00:00 2001 From: Saiful Alam Date: Fri, 24 Jun 2022 00:58:04 +0600 Subject: [PATCH] refactor: update user default role --- .env.example | 2 +- app/Http/Controllers/UserController.php | 9 ++++--- config/hydra.php | 4 +-- database/seeders/DatabaseSeeder.php | 3 +-- database/seeders/UserRoleSeeder.php | 33 ------------------------- database/seeders/UsersSeeder.php | 13 +++++++--- 6 files changed, 19 insertions(+), 45 deletions(-) delete mode 100644 database/seeders/UserRoleSeeder.php diff --git a/.env.example b/.env.example index 299078a..86c7167 100644 --- a/.env.example +++ b/.env.example @@ -54,5 +54,5 @@ MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" SANCTUM_STATEFUL_DOMAINS="localhost,localhost:3000,localhost:8000,127.0.0.1,127.0.0.1:8000,::1" -DEFAULT_ROLE_ID=2 +DEFAULT_ROLE_SLUG=user DELETE_PREVIOUS_ACCESS_TOKENS_ON_LOGIN=false diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 7c110e5..9629de8 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -45,10 +45,11 @@ class UserController extends Controller { 'name' => $creds['name'] ]); - UserRole::create([ - 'user_id' => $user->id, - 'role_id' => config('hydra.default_user_role_id', 2) - ]); + $user->roles()->attach(Role::firstOrCreate([ + 'slug' => config('hydra.default_user_role_slug', 'user') + ],[ + 'name' => Str::title(config('hydra.default_user_role_slug', 'user')) + ])); return $user; diff --git a/config/hydra.php b/config/hydra.php index 88a7252..7782450 100644 --- a/config/hydra.php +++ b/config/hydra.php @@ -22,11 +22,11 @@ return [ | This value is the default user role id that will be assigned to new users | when they register. | - | 1 = Admin role, 2 = User role, 3 = Customer Role - Check RoleSeeder for more + | admin = Admin role, user = User role, customer = Customer Role - Check RoleSeeder for more | */ - 'default_user_role_id' => env('DEFAULT_ROLE_ID', 2), + 'default_user_role_slug' => env('DEFAULT_ROLE_SLUG', 'admin'), /* |-------------------------------------------------------------------------- diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index ff961c4..de71c2b 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -21,9 +21,8 @@ class DatabaseSeeder extends Seeder // 'email' => 'test@example.com', // ]); $this->call([ - UsersSeeder::class, RoleSeeder::class, - UserRoleSeeder::class + UsersSeeder::class, ]); } diff --git a/database/seeders/UserRoleSeeder.php b/database/seeders/UserRoleSeeder.php deleted file mode 100644 index 7a90e9c..0000000 --- a/database/seeders/UserRoleSeeder.php +++ /dev/null @@ -1,33 +0,0 @@ -truncate(); - Schema::enableForeignKeyConstraints(); - - UserRole::create([ - 'user_id'=>1, - 'role_id'=>1 - ]); //admin - UserRole::create([ - 'user_id'=>1, - 'role_id'=>5 - ]);//all - } -} diff --git a/database/seeders/UsersSeeder.php b/database/seeders/UsersSeeder.php index a385c33..4c874ab 100644 --- a/database/seeders/UsersSeeder.php +++ b/database/seeders/UsersSeeder.php @@ -2,12 +2,14 @@ namespace Database\Seeders; -use Illuminate\Database\Console\Seeds\WithoutModelEvents; -use Illuminate\Database\Seeder; +use App\Models\Role use App\Models\User; +use Illuminate\Support\Str; +use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; +use Illuminate\Database\Console\Seeds\WithoutModelEvents; class UsersSeeder extends Seeder { @@ -22,10 +24,15 @@ class UsersSeeder extends Seeder DB::table('users')->truncate(); Schema::enableForeignKeyConstraints(); - User::create([ + $user = User::create([ 'email'=>'admin@hydra.project', 'password'=>Hash::make('hydra'), 'name'=>'Hydra Admin' ]); + $user->roles()->attach(Role::firstOrCreate([ + 'slug' => config('hydra.default_user_role_slug', 'user') + ],[ + 'name' => Str::title(config('hydra.default_user_role_slug', 'user')) + ])); } }