Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f3872e0665 | ||
|
|
4270f152d2 | ||
|
|
9539c2572f | ||
|
|
ff94434032 | ||
|
|
7313f64a70 | ||
|
|
a4aab54735 | ||
| c9bd6664f2 |
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -28,6 +28,10 @@
|
||||
public function data()
|
||||
{
|
||||
return [
|
||||
['name' => 'adk'],
|
||||
['name' => 'basicdata'],
|
||||
['name' => 'location'],
|
||||
['name' => 'logs'],
|
||||
['name' => 'usermanagement']
|
||||
];
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user