feat: add user relationship to core models, update seeders, and improve login validation error handling
This commit is contained in:
parent
bf12a38ccb
commit
fdb662d61c
|
|
@ -32,15 +32,27 @@ public function authenticate(): void
|
|||
$password = $this->input('password');
|
||||
|
||||
$errorField = $this->filled('nisn') ? 'nisn' : 'nip';
|
||||
$labelField = $this->filled('nisn') ? 'NISN' : 'NIP/NIK';
|
||||
|
||||
if (!Auth::attempt(['nomor_induk' => $loginIdentifier, 'password' => $password], $this->boolean('remember'))) {
|
||||
// 1. Cek keberadaan user berdasarkan nomor_induk
|
||||
$user = \App\Models\User::where('nomor_induk', $loginIdentifier)->first();
|
||||
|
||||
if (!$user) {
|
||||
RateLimiter::hit($this->throttleKey());
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
$errorField => trans('auth.failed'),
|
||||
$errorField => "Nomor Induk ({$labelField}) tidak ditemukan.",
|
||||
]);
|
||||
}
|
||||
|
||||
// 2. Cek password (Auth::attempt)
|
||||
if (!Auth::attempt(['nomor_induk' => $loginIdentifier, 'password' => $password], $this->boolean('remember'))) {
|
||||
RateLimiter::hit($this->throttleKey());
|
||||
throw ValidationException::withMessages([
|
||||
'password' => 'Kata sandi yang Anda masukkan salah.',
|
||||
]);
|
||||
}
|
||||
|
||||
// Ambil user yang sudah terautentikasi
|
||||
$user = Auth::user();
|
||||
|
||||
// Cek jika role sesuai
|
||||
|
|
|
|||
|
|
@ -6,5 +6,10 @@
|
|||
|
||||
class Announcement extends Model
|
||||
{
|
||||
protected $fillable = ['type', 'icon', 'title', 'content'];
|
||||
protected $fillable = ['user_id', 'type', 'icon', 'title', 'content'];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,4 +10,9 @@ class MasterInduk extends Model
|
|||
use HasFactory;
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,5 +6,10 @@
|
|||
|
||||
class Recommendation extends Model
|
||||
{
|
||||
protected $fillable = ['judul', 'kategori', 'youtube_link', 'deskripsi'];
|
||||
protected $fillable = ['user_id', 'judul', 'kategori', 'youtube_link', 'deskripsi'];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,4 +63,19 @@ public function loans()
|
|||
{
|
||||
return $this->hasMany(Loan::class);
|
||||
}
|
||||
|
||||
public function announcements()
|
||||
{
|
||||
return $this->hasMany(Announcement::class);
|
||||
}
|
||||
|
||||
public function recommendations()
|
||||
{
|
||||
return $this->hasMany(Recommendation::class);
|
||||
}
|
||||
|
||||
public function masterInduks()
|
||||
{
|
||||
return $this->hasMany(MasterInduk::class);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ public function up(): void
|
|||
{
|
||||
Schema::create('announcements', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->onDelete('cascade');
|
||||
$table->string('type'); // warning, info, success, etc.
|
||||
$table->string('icon');
|
||||
$table->string('title');
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ public function up(): void
|
|||
{
|
||||
Schema::create('recommendations', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->onDelete('cascade');
|
||||
$table->string('judul');
|
||||
$table->string('kategori');
|
||||
$table->string('youtube_link');
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ public function up()
|
|||
{
|
||||
Schema::create('master_induks', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->onDelete('cascade');
|
||||
$table->string('nomor_induk')->unique();
|
||||
$table->enum('role', ['siswa', 'guru']);
|
||||
$table->string('nama_pemilik')->nullable();
|
||||
|
|
|
|||
|
|
@ -16,9 +16,12 @@ public function run(): void
|
|||
$announcements = DummyDataService::getPengumuman();
|
||||
|
||||
foreach ($announcements as $data) {
|
||||
$adminId = \App\Models\User::where('role', 'penjaga perpus')->first()?->id ?? 2;
|
||||
|
||||
Announcement::updateOrCreate(
|
||||
['id' => $data['id']],
|
||||
[
|
||||
'user_id' => $adminId,
|
||||
'type' => $data['type'],
|
||||
'icon' => $data['icon'],
|
||||
'title' => $data['title'],
|
||||
|
|
|
|||
|
|
@ -18,6 +18,16 @@ public function run()
|
|||
MasterInduk::truncate();
|
||||
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
|
||||
|
||||
// ID 2: Budi (Admin/Penjaga)
|
||||
$admin = User::create([
|
||||
'id' => 2,
|
||||
'name' => 'Budi Santoso',
|
||||
'email' => 'budi.santoso@smkn1perpus.sch.id',
|
||||
'password' => Hash::make('password'),
|
||||
'role' => 'penjaga perpus',
|
||||
'nomor_induk' => '197812312005011',
|
||||
]);
|
||||
|
||||
$whitelist = [
|
||||
['nomor_induk' => '1234567890', 'role' => 'siswa', 'nama_pemilik' => 'Silvi Rahmawati'],
|
||||
['nomor_induk' => '9988776655', 'role' => 'siswa', 'nama_pemilik' => 'Siti Nurhaliza'],
|
||||
|
|
@ -26,7 +36,7 @@ public function run()
|
|||
];
|
||||
|
||||
foreach ($whitelist as $w) {
|
||||
MasterInduk::create($w);
|
||||
MasterInduk::create(array_merge($w, ['user_id' => $admin->id]));
|
||||
}
|
||||
|
||||
// ISI USER ASLI
|
||||
|
|
@ -44,16 +54,6 @@ public function run()
|
|||
'golongan' => 'A',
|
||||
]);
|
||||
|
||||
// ID 2: Budi (Admin/Penjaga)
|
||||
User::create([
|
||||
'id' => 2,
|
||||
'name' => 'Budi Santoso',
|
||||
'email' => 'budi.santoso@smkn1perpus.sch.id',
|
||||
'password' => Hash::make('password'),
|
||||
'role' => 'penjaga perpus',
|
||||
'nomor_induk' => '197812312005011',
|
||||
]);
|
||||
|
||||
// ID 3: Siti (Siswa)
|
||||
User::create([
|
||||
'id' => 3,
|
||||
|
|
|
|||
|
|
@ -16,9 +16,12 @@ public function run(): void
|
|||
$recommendations = DummyDataService::getRekomendasiPembelajaran();
|
||||
|
||||
foreach ($recommendations as $data) {
|
||||
$adminId = \App\Models\User::where('role', 'penjaga perpus')->first()?->id ?? 2;
|
||||
|
||||
Recommendation::updateOrCreate(
|
||||
['id' => $data['id']],
|
||||
[
|
||||
'user_id' => $adminId,
|
||||
'judul' => $data['judul'],
|
||||
'kategori' => $data['kategori'],
|
||||
'youtube_link' => $data['youtube_link'],
|
||||
|
|
|
|||
|
|
@ -56,7 +56,10 @@ public function run(): void
|
|||
$nomorInduk = fake()->unique()->numerify('##########');
|
||||
$name = fake()->name();
|
||||
|
||||
$adminId = User::where('role', 'penjaga perpus')->first()?->id ?? 2;
|
||||
|
||||
\App\Models\MasterInduk::create([
|
||||
'user_id' => $adminId,
|
||||
'nomor_induk' => $nomorInduk,
|
||||
'role' => 'siswa',
|
||||
'nama_pemilik' => $name,
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@
|
|||
<i class="bi bi-eye-slash-fill"></i>
|
||||
</button>
|
||||
</div>
|
||||
<x-input-error :messages="$errors->get('password')" class="mt-2" />
|
||||
</div>
|
||||
|
||||
<div class="d-grid mt-4">
|
||||
|
|
|
|||
Loading…
Reference in New Issue