7 Commits

Author SHA1 Message Date
Sholahuddin Al Ayubi
f3872e0665 feat(usermanagement): fix permission seeder structure and ensure module column populated correctly
- Updated PermissionGroupSeeder to seed consistent permission group definitions
- Updated PermissionsSeeder to generate proper permission records with correct module mapping
- Ensured permission creation uses dynamically generated module value based on group name
- Fixed undefined array key "module" issue by restructuring data() output
- Applied permission assignment to all roles for each generated permission
- Improved overall seeder stability and idempotency

To apply the updates, run the following commands:

php artisan module:seed Usermanagement --class="PermissionGroupSeeder"
php artisan module:seed Usermanagement --class="PermissionsSeeder"
2025-12-03 18:28:51 +07:00
Sholahuddin Al Ayubi
4270f152d2 feat(usermanagement): update UsersSeeder to generate users for all branches including branchdirector and soadmindokumen roles
Perubahan utama:
- Menambahkan proses pembuatan user untuk role `branchdirector` di semua branch (branchLuar, branchDalam, dan KPNO).
- Menambahkan proses pembuatan user untuk role `soadmindokumen` di semua branch (branchLuar, branchDalam, dan KPNO).
- Menjamin seluruh role dari RolesSeeder dibuat sebelum user di-generate.
- Menyelaraskan pola pembuatan email dan nama user per branch.
- Mempertahankan user default branch_id = 1 tanpa suffix branch.
- Membersihkan struktur dan alur seeder agar lebih maintainable.

Instruksi setelah perubahan:
1. php artisan config:clear
2. php artisan module:migrate-reset Usermanagement
3. php artisan module:migrate Usermanagement
4. php artisan module:seed Adk --class="UserSeeder"
5. php artisan module:seed Usermanagement --class="UsersSeeder"

Catatan tambahan:
- Urutan approval role kini mengikuti hierarki:
  1. soadmindokumen
  2. admindokumen
  3. legal
  4. branchdirector
2025-12-03 15:14:29 +07:00
Sholahuddin Al Ayubi
9539c2572f feat(usermanagement): add appointment_cabangs relationship to User model 2025-11-18 11:09:21 +07:00
Sholahuddin Al Ayubi
ff94434032 refactor(usermanagement): simplify UsersSeeder by removing hardcoded branch arrays and enhancing user creation logic 2025-11-11 13:29:37 +07:00
Sholahuddin Al Ayubi
7313f64a70 refactor(usermanagement): enhance UsersSeeder by removing RolesSeeder dependency and improving user creation logic 2025-11-10 17:44:41 +07:00
Sholahuddin Al Ayubi
a4aab54735 refactor(usermanagement): streamline UsersSeeder by utilizing RolesSeeder and improving user creation logic 2025-10-31 16:53:50 +07:00
c9bd6664f2 refactor(usermanagement): tidy up User model code structure and improve attribute casting 2025-10-03 09:07:42 +07:00
5 changed files with 344 additions and 162 deletions

View File

@@ -1,94 +1,110 @@
<?php
namespace Modules\Usermanagement\Models;
namespace Modules\Usermanagement\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Modules\Basicdata\Models\Branch;
use Spatie\Permission\Traits\HasRoles;
use Mattiverse\Userstamps\Traits\Userstamps;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Modules\Basicdata\Models\Branch;
use Modules\Adk\Models\Appointment;
use Spatie\Permission\Traits\HasRoles;
use Mattiverse\Userstamps\Traits\Userstamps;
/**
* Class User
*
* This class extends the Laravel's Authenticatable class and represents a User in the application.
* It includes traits for using factories, notifications, API tokens, and UUIDs.
*
* @property string $name The name of the user.
* @property string $email The email of the user.
* @property string $password The hashed password of the user.
* @property string $remember_token The token used for "remember me" functionality.
*
* @package App\Models
*/
class User extends Authenticatable
{
use HasFactory, Notifiable, Userstamps, HasRoles, softDeletes;
protected $guard_name = ['web'];
/**
* Class User
* The attributes that are mass assignable.
*
* This class extends the Laravel's Authenticatable class and represents a User in the application.
* It includes traits for using factories, notifications, API tokens, and UUIDs.
* These are the attributes that can be set in bulk during a create or update operation.
*
* @property string $name The name of the user.
* @property string $email The email of the user.
* @property string $password The hashed password of the user.
* @property string $remember_token The token used for "remember me" functionality.
*
* @package App\Models
* @var array<int, string>
*/
class User extends Authenticatable
protected $fillable = [
'name',
'email',
'password',
'nik',
'branch_id',
'profile_photo_path',
'last_login_at',
'last_login_ip',
'sign'
];
/**
* The attributes that should be hidden for serialization.
*
* These are the attributes that will be hidden when the model is converted to an array or JSON.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* This method defines how the attributes should be cast when accessed.
* In this case, 'email_verified_at' is cast to 'datetime', 'password' is cast to 'hashed', and 'id' is cast to 'string'.
*
* @return array<string, string>
*/
protected function casts(): array
{
use HasFactory, Notifiable, Userstamps, HasRoles, softDeletes;
protected $guard_name = ['web'];
/**
* The attributes that are mass assignable.
*
* These are the attributes that can be set in bulk during a create or update operation.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'nik',
'branch_id',
'profile_photo_path',
'last_login_at',
'last_login_ip',
'sign'
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'id' => 'string',
];
/**
* The attributes that should be hidden for serialization.
*
* These are the attributes that will be hidden when the model is converted to an array or JSON.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* This method defines how the attributes should be cast when accessed.
* In this case, 'email_verified_at' is cast to 'datetime', 'password' is cast to 'hashed', and 'id' is cast to 'string'.
*
* @return array<string, string>
*/
protected function casts()
: array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'id' => 'string',
];
}
public function branch(){
return $this->belongsTo(Branch::class);
}
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Modules\Usermanagement\Database\Factories\UserFactory::new();
}
}
public function branch()
{
return $this->belongsTo(Branch::class);
}
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Modules\Usermanagement\Database\Factories\UserFactory::new();
}
/**
* Get all of the appointments for the User
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function appointments()
{
return $this->hasMany(Appointment::class, 'admin_id');
}
public function appointment_cabangs()
{
return $this->hasMany(Appointment::class, 'admin_id');
}
}

View File

@@ -4,8 +4,7 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
return new class extends Migration {
/**
* Run the migrations.
*/
@@ -46,8 +45,12 @@ return new class extends Migration
*/
public function down(): void
{
Schema::dropIfExists('users');
Schema::dropIfExists('password_reset_tokens');
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('sessions');
Schema::dropIfExists('password_reset_tokens');
Schema::dropIfExists('users');
Schema::enableForeignKeyConstraints();
}
};

View File

@@ -28,6 +28,10 @@
public function data()
{
return [
['name' => 'adk'],
['name' => 'basicdata'],
['name' => 'location'],
['name' => 'logs'],
['name' => 'usermanagement']
];
}

View File

@@ -1,65 +1,63 @@
<?php
namespace Modules\Usermanagement\Database\Seeders;
namespace Modules\Usermanagement\Database\Seeders;
use Illuminate\Database\Seeder;
use Modules\Usermanagement\Models\PermissionGroup;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Illuminate\Database\Seeder;
use Modules\Usermanagement\Models\PermissionGroup;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
class PermissionsSeeder extends Seeder
class PermissionsSeeder extends Seeder
{
public function run()
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$data = $this->data();
$data = $this->data();
foreach ($data as $value) {
$permission = Permission::updateOrCreate([
'name' => $value['name'],
'guard_name' => 'web' // or 'api
], [
'permission_group_id' => $value['group']
]);
foreach ($data as $value) {
$roles = Role::all();
foreach ($roles as $role) {
$role->givePermissionTo($permission);
}
$permission = Permission::updateOrCreate([
'name' => $value['name'],
'guard_name' => 'web',
], [
'permission_group_id' => $value['group_id'],
'module' => $value['module'],
]);
foreach (Role::all() as $role) {
$role->givePermissionTo($permission);
}
}
public function data()
{
$data = [];
// list of model permission
$groups = PermissionGroup::all();
foreach ($groups as $group) {
foreach ($this->crudActions($group->name) as $action) {
$data[] = ['name' => $action, 'group' => $group->id];
}
}
return $data;
}
public function crudActions($name)
{
$actions = [];
// list of permission actions
$crud = ['create', 'read', 'update', 'delete','export', 'authorize', 'report','restore'];
foreach ($crud as $value) {
$actions[] = $name . '.' . $value;
}
return $actions;
}
}
public function data()
{
$data = [];
$groups = PermissionGroup::all();
foreach ($groups as $group) {
foreach ($this->crudActions($group->name) as $action) {
$data[] = [
'name' => $action,
'group_id' => $group->id,
'module' => $group->name,
];
}
}
return $data;
}
public function crudActions($name)
{
$actions = ['create', 'read', 'update', 'delete', 'export', 'authorize', 'report', 'restore'];
$result = [];
foreach ($actions as $value) {
$result[] = $name . '.' . $value;
}
return $result;
}
}

View File

@@ -1,35 +1,196 @@
<?php
namespace Modules\Usermanagement\Database\Seeders;
namespace Modules\Usermanagement\Database\Seeders;
use Faker\Generator;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Modules\Usermanagement\Models\User;
use Spatie\Permission\Models\Role;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Modules\Usermanagement\Models\User;
use Modules\Usermanagement\Database\Seeders\RolesSeeder;
use Spatie\Permission\Models\Role;
class UsersSeeder extends Seeder
class UsersSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$roleSeeder = new RolesSeeder();
$rolesData = $roleSeeder->data();
/**
* Run the database seeds.
*
* @return void
* ==================================================
* STEP 0: Pastikan semua roles dari RolesSeeder sudah dibuat di tabel roles
* ==================================================
*/
public function run(Generator $faker)
{
$roles = Role::all();
foreach ($rolesData as $roleData) {
Role::firstOrCreate(
['name' => $roleData['name']],
['guard_name' => 'web']
);
}
foreach ($roles as $role) {
$user = User::create([
'name' => $role->name,
'email' => $role->name . '@ag.co.id',
'password' => Hash::make('bagbag'),
'branch_id' => 1,
'nik' => '000000',
/**
* ==================================================
* Helper function untuk membuat user
* ==================================================
*/
$createUser = function ($roleName, $branchId = null, $includeBranchInEmail = true, $includeBranchInName = true) {
$email = $roleName . ($includeBranchInEmail && $branchId ? $branchId : '') . '@ag.co.id';
$name = ucfirst($roleName);
if ($includeBranchInName && $branchId) {
$name .= ' ' . $branchId;
}
$user = User::firstOrCreate(
['email' => $email],
[
'name' => $name,
'password' => Hash::make('bagbag'),
'branch_id' => $branchId,
'nik' => rand(100000, 999999),
'email_verified_at' => now(),
]);
]
);
$role = Role::where('name', $roleName)->first();
if ($role) {
$user->assignRole($role);
}
};
/**
* ==================================================
* STEP 1: Buat user per role (branch_id = 1)
* ==================================================
* - Tanpa angka "1" di email
* - Tanpa angka "1" di nama
*/
foreach ($rolesData as $roleData) {
$roleName = $roleData['name'];
$createUser($roleName, 1, false, false); // tanpa 1 di email & nama
}
/**
* ==================================================
* STEP 2: Jalankan logic lama (user per cabang)
* ==================================================
*/
$branchLuar = [
24,
25,
29,
35,
37,
41,
42,
45,
46,
50,
71,
74,
77,
82,
84,
85,
88,
90,
91,
93,
97,
107,
108,
111,
112,
113,
114,
115
];
$branchDalam = [
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
14,
15,
17,
18,
22,
23,
53,
55,
58,
60,
61,
66,
70,
95,
96,
98,
100,
105
];
$kpno = 6;
// LEGAL
foreach ($branchLuar as $branchId) {
$createUser('legal', $branchId);
}
$createUser('legal', $kpno);
// SPV LEGAL
$createUser('spvlegal', $kpno);
// USER CABANG
foreach (array_merge($branchLuar, $branchDalam) as $branchId) {
$createUser('cabang', $branchId);
}
// ADMIN DOKUMEN
foreach ($branchLuar as $branchId) {
$createUser('admindokumen', $branchId);
}
$createUser('admindokumen', $kpno);
// ADMIN KREDIT
$createUser('adminkredit', $kpno);
// AUDITOR
foreach ($branchLuar as $branchId) {
$createUser('auditor', $branchId);
}
$createUser('auditor', $kpno);
/**
* ==================================================
* NEW STEP: BRANCHDIRECTOR UNTUK SEMUA BRANCH
* ==================================================
*/
foreach (array_merge($branchLuar, $branchDalam) as $branchId) {
$createUser('branchdirector', $branchId);
}
$createUser('branchdirector', $kpno);
/**
* ==================================================
* NEW STEP: SO ADMIN DOKUMEN UNTUK SEMUA BRANCH
* ==================================================
*/
foreach (array_merge($branchLuar, $branchDalam) as $branchId) {
$createUser('soadmindokumen', $branchId);
}
// tetap buat juga untuk KPNO
$createUser('soadmindokumen', $kpno);
}
}