feat: Introduce user roles with RoleEnum, update user table schema for username and soft deletes, and refactor user seeding.
This commit is contained in:
parent
9daecee574
commit
c5fa78840a
22
app/Enums/RoleEnum.php
Normal file
22
app/Enums/RoleEnum.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enums;
|
||||||
|
|
||||||
|
use App\Traits\Enum\WithComment;
|
||||||
|
use App\Traits\Enum\WithValue;
|
||||||
|
|
||||||
|
enum RoleEnum: string
|
||||||
|
{
|
||||||
|
use WithComment, WithValue;
|
||||||
|
|
||||||
|
case DEVELOPER = 'Developer';
|
||||||
|
case OWNER = 'Pemilik';
|
||||||
|
case ADMINISTRATOR = 'Administrator';
|
||||||
|
|
||||||
|
public static function options(): array
|
||||||
|
{
|
||||||
|
return collect(self::cases())
|
||||||
|
->mapWithKeys(fn ($case) => [$case->value => $case->value])
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,46 +2,24 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
|
|
||||||
class User extends Authenticatable
|
class User extends Authenticatable
|
||||||
{
|
{
|
||||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
use HasFactory, Notifiable, SoftDeletes;
|
||||||
use HasFactory, Notifiable;
|
|
||||||
|
|
||||||
/**
|
protected $guarded = ['id'];
|
||||||
* The attributes that are mass assignable.
|
|
||||||
*
|
|
||||||
* @var list<string>
|
|
||||||
*/
|
|
||||||
protected $fillable = [
|
|
||||||
'name',
|
|
||||||
'email',
|
|
||||||
'password',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that should be hidden for serialization.
|
|
||||||
*
|
|
||||||
* @var list<string>
|
|
||||||
*/
|
|
||||||
protected $hidden = [
|
protected $hidden = [
|
||||||
'password',
|
'password',
|
||||||
'remember_token',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the attributes that should be cast.
|
|
||||||
*
|
|
||||||
* @return array<string, string>
|
|
||||||
*/
|
|
||||||
protected function casts(): array
|
protected function casts(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'email_verified_at' => 'datetime',
|
|
||||||
'password' => 'hashed',
|
'password' => 'hashed',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
11
app/Traits/Enum/WithComment.php
Normal file
11
app/Traits/Enum/WithComment.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Traits\Enum;
|
||||||
|
|
||||||
|
trait WithComment
|
||||||
|
{
|
||||||
|
public static function comment(): string
|
||||||
|
{
|
||||||
|
return implode(', ', array_map(fn ($case) => "{$case->value}: {$case->getLabel()}", self::cases()));
|
||||||
|
}
|
||||||
|
}
|
||||||
11
app/Traits/Enum/WithValue.php
Normal file
11
app/Traits/Enum/WithValue.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Traits\Enum;
|
||||||
|
|
||||||
|
trait WithValue
|
||||||
|
{
|
||||||
|
public static function values(): array
|
||||||
|
{
|
||||||
|
return array_map(fn ($case) => $case->value, self::cases());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -112,4 +112,5 @@
|
|||||||
|
|
||||||
'password_timeout' => env('AUTH_PASSWORD_TIMEOUT', 10800),
|
'password_timeout' => env('AUTH_PASSWORD_TIMEOUT', 10800),
|
||||||
|
|
||||||
|
'password_default' => env('PASSWORD_DEFAULT', 'users'),
|
||||||
];
|
];
|
||||||
|
|||||||
@ -13,12 +13,13 @@ public function up(): void
|
|||||||
{
|
{
|
||||||
Schema::create('users', function (Blueprint $table) {
|
Schema::create('users', function (Blueprint $table) {
|
||||||
$table->id();
|
$table->id();
|
||||||
$table->string('name');
|
$table->string('name', 100);
|
||||||
$table->string('email')->unique();
|
$table->string('email', 254)->unique();
|
||||||
$table->timestamp('email_verified_at')->nullable();
|
$table->string('username', 10)->unique();
|
||||||
$table->string('password');
|
$table->string('password', 60);
|
||||||
$table->rememberToken();
|
$table->timestamp('created_at')->useCurrent();
|
||||||
$table->timestamps();
|
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();
|
||||||
|
$table->softDeletes();
|
||||||
});
|
});
|
||||||
|
|
||||||
Schema::create('password_reset_tokens', function (Blueprint $table) {
|
Schema::create('password_reset_tokens', function (Blueprint $table) {
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace Database\Seeders;
|
namespace Database\Seeders;
|
||||||
|
|
||||||
use App\Models\User;
|
|
||||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
|
|
||||||
@ -15,11 +14,8 @@ class DatabaseSeeder extends Seeder
|
|||||||
*/
|
*/
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
// User::factory(10)->create();
|
$this->call([
|
||||||
|
UserSeeder::class,
|
||||||
User::factory()->create([
|
|
||||||
'name' => 'Test User',
|
|
||||||
'email' => 'test@example.com',
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
40
database/seeders/UserSeeder.php
Normal file
40
database/seeders/UserSeeder.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
|
||||||
|
class UserSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*/
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
// developer
|
||||||
|
$developer = User::create([
|
||||||
|
'name' => 'Yoga Pangestu',
|
||||||
|
'email' => 'info.pangestuyoga@gmail.com',
|
||||||
|
'username' => 'pangestu',
|
||||||
|
'password' => Hash::make(config('auth.password_default')),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// owner
|
||||||
|
$owner = User::create([
|
||||||
|
'name' => 'Owner',
|
||||||
|
'email' => 'owner@gmail.com',
|
||||||
|
'username' => 'owner',
|
||||||
|
'password' => Hash::make(config('auth.password_default')),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// admin
|
||||||
|
$administrator = User::create([
|
||||||
|
'name' => 'Administrator',
|
||||||
|
'email' => 'admin@gmail.com',
|
||||||
|
'username' => 'admin',
|
||||||
|
'password' => Hash::make(config('auth.password_default')),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user