Merge pull request #17 from 4msar/master

refactor: default user role by slug
pull/19/head
Hasin Hayder 2 years ago committed by GitHub
commit 82aa3bba81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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" 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 DELETE_PREVIOUS_ACCESS_TOKENS_ON_LOGIN=false

@ -44,10 +44,11 @@ class UserController extends Controller {
'name' => $creds['name'] 'name' => $creds['name']
]); ]);
UserRole::create([ $user->roles()->attach(Role::firstOrCreate([
'user_id' => $user->id, 'slug' => config('hydra.default_user_role_slug', 'user')
'role_id' => config('hydra.default_user_role_id', 2) ],[
]); 'name' => Str::title(config('hydra.default_user_role_slug', 'user'))
]));
return $user; return $user;

@ -22,11 +22,11 @@ return [
| This value is the default user role id that will be assigned to new users | This value is the default user role id that will be assigned to new users
| when they register. | 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', 'user'),
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

@ -21,9 +21,8 @@ class DatabaseSeeder extends Seeder
// 'email' => 'test@example.com', // 'email' => 'test@example.com',
// ]); // ]);
$this->call([ $this->call([
UsersSeeder::class,
RoleSeeder::class, RoleSeeder::class,
UserRoleSeeder::class UsersSeeder::class,
]); ]);
} }

@ -1,33 +0,0 @@
<?php
namespace Database\Seeders;
use App\Models\UserRole;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class UserRoleSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Schema::disableForeignKeyConstraints();
DB::table('user_roles')->truncate();
Schema::enableForeignKeyConstraints();
UserRole::create([
'user_id'=>1,
'role_id'=>1
]); //admin
UserRole::create([
'user_id'=>1,
'role_id'=>5
]);//all
}
}

@ -2,12 +2,14 @@
namespace Database\Seeders; namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents; use App\Models\Role;
use Illuminate\Database\Seeder;
use App\Models\User; use App\Models\User;
use Illuminate\Support\Str;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
class UsersSeeder extends Seeder class UsersSeeder extends Seeder
{ {
@ -22,10 +24,11 @@ class UsersSeeder extends Seeder
DB::table('users')->truncate(); DB::table('users')->truncate();
Schema::enableForeignKeyConstraints(); Schema::enableForeignKeyConstraints();
User::create([ $user = User::create([
'email'=>'admin@hydra.project', 'email'=>'admin@hydra.project',
'password'=>Hash::make('hydra'), 'password'=>Hash::make('hydra'),
'name'=>'Hydra Admin' 'name'=>'Hydra Admin'
]); ]);
$user->roles()->attach(Role::where('slug', 'admin')->first());
} }
} }

Loading…
Cancel
Save