feat: Introduce user roles with RoleEnum, update user table schema for username and soft deletes, and refactor user seeding.

This commit is contained in:
Yoga Pangestu 2026-02-07 15:35:09 +07:00
parent 9daecee574
commit c5fa78840a
8 changed files with 97 additions and 37 deletions

22
app/Enums/RoleEnum.php Normal file
View 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();
}
}

View File

@ -2,46 +2,24 @@
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
use HasFactory, Notifiable, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'email',
'password',
];
protected $guarded = ['id'];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}

View 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()));
}
}

View 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());
}
}

View File

@ -112,4 +112,5 @@
'password_timeout' => env('AUTH_PASSWORD_TIMEOUT', 10800),
'password_default' => env('PASSWORD_DEFAULT', 'users'),
];

View File

@ -13,12 +13,13 @@ public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->string('name', 100);
$table->string('email', 254)->unique();
$table->string('username', 10)->unique();
$table->string('password', 60);
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();
$table->softDeletes();
});
Schema::create('password_reset_tokens', function (Blueprint $table) {

View File

@ -2,7 +2,6 @@
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
@ -15,11 +14,8 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
// User::factory(10)->create();
User::factory()->create([
'name' => 'Test User',
'email' => 'test@example.com',
$this->call([
UserSeeder::class,
]);
}
}

View 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')),
]);
}
}