Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b51a4f039 | ||
|
|
0b28760f41 | ||
|
|
f3872e0665 | ||
|
|
4270f152d2 | ||
|
|
9539c2572f | ||
|
|
ff94434032 | ||
|
|
7313f64a70 |
@@ -1,30 +1,31 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Modules\Usermanagement\Http\Controllers;
|
namespace Modules\Usermanagement\Http\Controllers;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Validator;
|
||||||
use Maatwebsite\Excel\Facades\Excel;
|
use Maatwebsite\Excel\Facades\Excel;
|
||||||
use Modules\Basicdata\Models\Branch;
|
use Modules\Basicdata\Models\Branch;
|
||||||
use Modules\Usermanagement\Exports\UsersExport;
|
use Modules\Usermanagement\Exports\UsersExport;
|
||||||
use Modules\Usermanagement\Http\Requests\User as UserRequest;
|
use Modules\Usermanagement\Http\Requests\User as UserRequest;
|
||||||
use Modules\Usermanagement\Models\Role;
|
use Modules\Usermanagement\Models\Role;
|
||||||
use Modules\Usermanagement\Models\User;
|
use Modules\Usermanagement\Models\User;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class UsersController
|
* Class UsersController
|
||||||
*
|
*
|
||||||
* This controller is responsible for managing user within the application.
|
* This controller is responsible for managing user within the application.
|
||||||
*
|
*
|
||||||
* @package Modules\Usermanagement\Http\Controllers
|
* @package Modules\Usermanagement\Http\Controllers
|
||||||
*/
|
*/
|
||||||
class UsersController extends Controller
|
class UsersController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var \Illuminate\Contracts\Auth\Authenticatable|null
|
* @var \Illuminate\Contracts\Auth\Authenticatable|null
|
||||||
*/
|
*/
|
||||||
@@ -73,64 +74,62 @@
|
|||||||
public function dataForDatatables(Request $request)
|
public function dataForDatatables(Request $request)
|
||||||
{
|
{
|
||||||
if (is_null($this->user) || !$this->user->can('usermanagement.read')) {
|
if (is_null($this->user) || !$this->user->can('usermanagement.read')) {
|
||||||
return response()->json(['message' => 'Sorry! You are not allowed to view users.','success' => false]);
|
return response()->json([
|
||||||
|
'message' => 'Sorry! You are not allowed to view users.',
|
||||||
|
'success' => false
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve data from the database
|
$query = User::query()->with(['branches', 'roles']);
|
||||||
$query = User::query();
|
|
||||||
|
|
||||||
if(!$this->user->hasRole('administrator')){
|
if (!$this->user->hasRole('administrator')) {
|
||||||
$query->whereHas('roles', function($q){
|
$query->whereHas('roles', function ($q) {
|
||||||
$q->where('name', '!=', 'administrator');
|
$q->where('name', '!=', 'administrator');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply search filter if provided
|
|
||||||
if ($request->has('search') && !empty($request->get('search'))) {
|
if ($request->has('search') && !empty($request->get('search'))) {
|
||||||
$search = $request->get('search');
|
$search = $request->get('search');
|
||||||
$query->whereAny(['name', 'email'], 'like', '%'.$search.'%');
|
$query->where(function ($q) use ($search) {
|
||||||
|
$q->where('name', 'like', '%' . $search . '%')
|
||||||
|
->orWhere('email', 'like', '%' . $search . '%')
|
||||||
|
->orWhereHas('branches', function ($qb) use ($search) {
|
||||||
|
$qb->where('name', 'like', '%' . $search . '%');
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply sorting if provided
|
|
||||||
if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
|
if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
|
||||||
$order = $request->get('sortOrder');
|
$order = $request->get('sortOrder');
|
||||||
$column = $request->get('sortField');
|
$column = $request->get('sortField');
|
||||||
$query->orderBy($column, $order);
|
$query->orderBy($column, $order);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the total count of records
|
|
||||||
$totalRecords = $query->count();
|
$totalRecords = $query->count();
|
||||||
|
|
||||||
// Apply pagination if provided
|
$page = $request->get('page', 1);
|
||||||
if ($request->has('page') && $request->has('size')) {
|
$size = $request->get('size', 10);
|
||||||
$page = $request->get('page');
|
$offset = ($page - 1) * $size;
|
||||||
$size = $request->get('size');
|
|
||||||
$offset = ($page - 1) * $size; // Calculate the offset
|
|
||||||
|
|
||||||
$query->skip($offset)->take($size);
|
$query->skip($offset)->take($size);
|
||||||
}
|
|
||||||
|
|
||||||
// Get the filtered count of records
|
|
||||||
$filteredRecords = $query->count();
|
$filteredRecords = $query->count();
|
||||||
|
|
||||||
// Get the data for the current page
|
$users = $query->get()->map(function ($user) {
|
||||||
$data = $query->with(['branch', 'roles'])->get();
|
$user->branch_names = $user->branches->pluck('name')->join(', ');
|
||||||
|
return $user;
|
||||||
|
});
|
||||||
|
|
||||||
// Calculate the page count
|
$pageCount = ceil($totalRecords / $size);
|
||||||
$pageCount = ceil($totalRecords / $request->get('size'));
|
|
||||||
|
|
||||||
// Calculate the current page number
|
|
||||||
$currentPage = 0 + 1;
|
|
||||||
|
|
||||||
// Return the response data as a JSON object
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'draw' => $request->get('draw'),
|
'draw' => $request->get('draw'),
|
||||||
'recordsTotal' => $totalRecords,
|
'recordsTotal' => $totalRecords,
|
||||||
'recordsFiltered' => $filteredRecords,
|
'recordsFiltered' => $filteredRecords,
|
||||||
'pageCount' => $pageCount,
|
'pageCount' => $pageCount,
|
||||||
'page' => $currentPage,
|
'page' => $page,
|
||||||
'totalCount' => $totalRecords,
|
'totalCount' => $totalRecords,
|
||||||
'data' => $data,
|
'data' => $users,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,7 +149,7 @@
|
|||||||
|
|
||||||
$user = User::find($id);
|
$user = User::find($id);
|
||||||
$roles = Role::all();
|
$roles = Role::all();
|
||||||
if(!$this->user->hasRole('administrator')){
|
if (!$this->user->hasRole('administrator')) {
|
||||||
$roles = $roles->where('name', '!=', 'administrator');
|
$roles = $roles->where('name', '!=', 'administrator');
|
||||||
}
|
}
|
||||||
$branches = Branch::all();
|
$branches = Branch::all();
|
||||||
@@ -168,7 +167,7 @@
|
|||||||
public function destroy($id)
|
public function destroy($id)
|
||||||
{
|
{
|
||||||
if (is_null($this->user) || !$this->user->can('usermanagement.delete')) {
|
if (is_null($this->user) || !$this->user->can('usermanagement.delete')) {
|
||||||
return response()->json(['message' => 'Sorry! You are not allowed to delete users.','success' => false]);
|
return response()->json(['message' => 'Sorry! You are not allowed to delete users.', 'success' => false]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$user = User::find($id);
|
$user = User::find($id);
|
||||||
@@ -218,11 +217,23 @@
|
|||||||
|
|
||||||
if ($validated) {
|
if ($validated) {
|
||||||
$user = User::create($validated);
|
$user = User::create($validated);
|
||||||
|
|
||||||
if ($user) {
|
if ($user) {
|
||||||
if ($request->roles) {
|
if ($request->roles) {
|
||||||
$user->assignRole($request->roles);
|
$user->assignRole($request->roles);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$branches = $request->input('branches', []);
|
||||||
|
|
||||||
|
$user->branches()->sync($branches);
|
||||||
|
|
||||||
|
if (!empty($branches)) {
|
||||||
|
$firstBranchId = $branches[0];
|
||||||
|
|
||||||
|
$user->branch_id = $firstBranchId;
|
||||||
|
$user->save();
|
||||||
|
}
|
||||||
|
|
||||||
return redirect()->route('users.index')->with('success', 'User created successfully.');
|
return redirect()->route('users.index')->with('success', 'User created successfully.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -243,7 +254,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
$roles = Role::all();
|
$roles = Role::all();
|
||||||
if(!$this->user->hasRole('administrator')){
|
if (!$this->user->hasRole('administrator')) {
|
||||||
$roles = $roles->where('name', '!=', 'administrator');
|
$roles = $roles->where('name', '!=', 'administrator');
|
||||||
}
|
}
|
||||||
$branches = Branch::all();
|
$branches = Branch::all();
|
||||||
@@ -348,25 +359,38 @@
|
|||||||
|
|
||||||
if ($validated) {
|
if ($validated) {
|
||||||
try {
|
try {
|
||||||
$user = User::find($id);
|
$user = User::findOrFail($id);
|
||||||
|
|
||||||
|
// Handle file upload e-sign
|
||||||
if ($request->hasFile('sign')) {
|
if ($request->hasFile('sign')) {
|
||||||
$sign = $request->file('sign');
|
$sign = $request->file('sign');
|
||||||
|
|
||||||
$signName = time() . '.' . $sign->getClientOriginalExtension();
|
$signName = time() . '.' . $sign->getClientOriginalExtension();
|
||||||
|
|
||||||
|
// Simpan file ke storage
|
||||||
$sign->storeAs(
|
$sign->storeAs(
|
||||||
'public/signatures/' . $user->id . '/',
|
'public/signatures/' . $user->id . '/',
|
||||||
$signName,
|
$signName
|
||||||
);
|
);
|
||||||
|
|
||||||
$validated['sign'] = $signName;
|
$validated['sign'] = $signName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update data user
|
||||||
$user->update($validated);
|
$user->update($validated);
|
||||||
|
|
||||||
|
// Update roles
|
||||||
if ($request->roles) {
|
if ($request->roles) {
|
||||||
$user->roles()->detach();
|
$user->roles()->detach();
|
||||||
$user->assignRole($request->roles);
|
$user->assignRole($request->roles);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$user->branches()->sync($request->input('branches', []));
|
||||||
|
|
||||||
|
$branchIds = $user->branches()->pluck('branches.id')->toArray();
|
||||||
|
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
Log::error('Failed to update user: ' . $e->getMessage());
|
||||||
return redirect()->back()->withErrors(['error' => 'Failed to update user. Please try again.']);
|
return redirect()->back()->withErrors(['error' => 'Failed to update user. Please try again.']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -374,4 +398,4 @@
|
|||||||
return redirect()->route('users.index')->with('success', 'User updated successfully.');
|
return redirect()->route('users.index')->with('success', 'User updated successfully.');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,4 +102,15 @@ class User extends Authenticatable
|
|||||||
{
|
{
|
||||||
return $this->hasMany(Appointment::class, 'admin_id');
|
return $this->hasMany(Appointment::class, 'admin_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function appointment_cabangs()
|
||||||
|
{
|
||||||
|
return $this->hasMany(Appointment::class, 'admin_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function branches()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Branch::class, 'user_branches', 'user_id', 'branch_id')
|
||||||
|
->withTimestamps();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
35
app/Models/UserBranch.php
Normal file
35
app/Models/UserBranch.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Usermanagement\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Modules\Usermanagement\Models\User;
|
||||||
|
use Modules\Basicdata\Models\Branch;
|
||||||
|
|
||||||
|
// use Modules\Usermanagement\Database\Factories\UserBranchFactory;
|
||||||
|
|
||||||
|
class UserBranch extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*/
|
||||||
|
protected $table = 'user_branches';
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'branch_id',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class)
|
||||||
|
->withTimestamps();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function branch()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Branch::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,8 +4,7 @@ use Illuminate\Database\Migrations\Migration;
|
|||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
return new class extends Migration
|
return new class extends Migration {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* Run the migrations.
|
* Run the migrations.
|
||||||
*/
|
*/
|
||||||
@@ -46,8 +45,12 @@ return new class extends Migration
|
|||||||
*/
|
*/
|
||||||
public function down(): void
|
public function down(): void
|
||||||
{
|
{
|
||||||
Schema::dropIfExists('users');
|
Schema::disableForeignKeyConstraints();
|
||||||
Schema::dropIfExists('password_reset_tokens');
|
|
||||||
Schema::dropIfExists('sessions');
|
Schema::dropIfExists('sessions');
|
||||||
|
Schema::dropIfExists('password_reset_tokens');
|
||||||
|
Schema::dropIfExists('users');
|
||||||
|
|
||||||
|
Schema::enableForeignKeyConstraints();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('user_branches', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->unsignedBigInteger('user_id');
|
||||||
|
$table->unsignedBigInteger('branch_id');
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
|
||||||
|
$table->foreign('branch_id')->references('id')->on('branches')->onDelete('cascade');
|
||||||
|
|
||||||
|
$table->unique(['user_id', 'branch_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('user_branches');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -28,6 +28,10 @@
|
|||||||
public function data()
|
public function data()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
['name' => 'adk'],
|
||||||
|
['name' => 'basicdata'],
|
||||||
|
['name' => 'location'],
|
||||||
|
['name' => 'logs'],
|
||||||
['name' => 'usermanagement']
|
['name' => 'usermanagement']
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,48 +1,48 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Modules\Usermanagement\Database\Seeders;
|
namespace Modules\Usermanagement\Database\Seeders;
|
||||||
|
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
use Modules\Usermanagement\Models\PermissionGroup;
|
use Modules\Usermanagement\Models\PermissionGroup;
|
||||||
use Spatie\Permission\Models\Permission;
|
use Spatie\Permission\Models\Permission;
|
||||||
use Spatie\Permission\Models\Role;
|
use Spatie\Permission\Models\Role;
|
||||||
|
|
||||||
class PermissionsSeeder extends Seeder
|
class PermissionsSeeder extends Seeder
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Run the database seeds.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
$data = $this->data();
|
$data = $this->data();
|
||||||
|
|
||||||
foreach ($data as $value) {
|
foreach ($data as $value) {
|
||||||
|
|
||||||
$permission = Permission::updateOrCreate([
|
$permission = Permission::updateOrCreate([
|
||||||
'name' => $value['name'],
|
'name' => $value['name'],
|
||||||
'guard_name' => 'web' // or 'api
|
'guard_name' => 'web',
|
||||||
], [
|
], [
|
||||||
'permission_group_id' => $value['group']
|
'permission_group_id' => $value['group_id'],
|
||||||
|
'module' => $value['module'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$roles = Role::all();
|
foreach (Role::all() as $role) {
|
||||||
foreach ($roles as $role) {
|
|
||||||
$role->givePermissionTo($permission);
|
$role->givePermissionTo($permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function data()
|
public function data()
|
||||||
{
|
{
|
||||||
$data = [];
|
$data = [];
|
||||||
// list of model permission
|
|
||||||
$groups = PermissionGroup::all();
|
$groups = PermissionGroup::all();
|
||||||
|
|
||||||
foreach ($groups as $group) {
|
foreach ($groups as $group) {
|
||||||
|
|
||||||
foreach ($this->crudActions($group->name) as $action) {
|
foreach ($this->crudActions($group->name) as $action) {
|
||||||
$data[] = ['name' => $action, 'group' => $group->id];
|
|
||||||
|
$data[] = [
|
||||||
|
'name' => $action,
|
||||||
|
'group_id' => $group->id,
|
||||||
|
'module' => $group->name,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,15 +51,13 @@
|
|||||||
|
|
||||||
public function crudActions($name)
|
public function crudActions($name)
|
||||||
{
|
{
|
||||||
$actions = [];
|
$actions = ['create', 'read', 'update', 'delete', 'export', 'authorize', 'report', 'restore'];
|
||||||
// list of permission actions
|
$result = [];
|
||||||
$crud = ['create', 'read', 'update', 'delete','export', 'authorize', 'report','restore'];
|
|
||||||
|
|
||||||
|
foreach ($actions as $value) {
|
||||||
foreach ($crud as $value) {
|
$result[] = $name . '.' . $value;
|
||||||
$actions[] = $name . '.' . $value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $actions;
|
return $result;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
38
database/seeders/UserBranchesSeeder.php
Normal file
38
database/seeders/UserBranchesSeeder.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Usermanagement\Database\Seeders;
|
||||||
|
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Modules\Usermanagement\Models\User;
|
||||||
|
|
||||||
|
class UserBranchesSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*/
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
$users = User::all();
|
||||||
|
|
||||||
|
foreach ($users as $user) {
|
||||||
|
if ($user->branch_id) {
|
||||||
|
$exists = DB::table('user_branches')
|
||||||
|
->where('user_id', $user->id)
|
||||||
|
->where('branch_id', $user->branch_id)
|
||||||
|
->exists();
|
||||||
|
|
||||||
|
if (!$exists) {
|
||||||
|
DB::table('user_branches')->insert([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'branch_id' => $user->branch_id,
|
||||||
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->command->info('User branches seeded successfully.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ use Illuminate\Database\Seeder;
|
|||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
use Modules\Usermanagement\Models\User;
|
use Modules\Usermanagement\Models\User;
|
||||||
use Modules\Usermanagement\Database\Seeders\RolesSeeder;
|
use Modules\Usermanagement\Database\Seeders\RolesSeeder;
|
||||||
|
use Spatie\Permission\Models\Role;
|
||||||
|
|
||||||
class UsersSeeder extends Seeder
|
class UsersSeeder extends Seeder
|
||||||
{
|
{
|
||||||
@@ -17,26 +18,179 @@ class UsersSeeder extends Seeder
|
|||||||
$roleSeeder = new RolesSeeder();
|
$roleSeeder = new RolesSeeder();
|
||||||
$rolesData = $roleSeeder->data();
|
$rolesData = $roleSeeder->data();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ==================================================
|
||||||
|
* STEP 0: Pastikan semua roles dari RolesSeeder sudah dibuat di tabel roles
|
||||||
|
* ==================================================
|
||||||
|
*/
|
||||||
foreach ($rolesData as $roleData) {
|
foreach ($rolesData as $roleData) {
|
||||||
if ($roleData['name'] === 'administrator') {
|
Role::firstOrCreate(
|
||||||
|
['name' => $roleData['name']],
|
||||||
|
['guard_name' => 'web']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ==================================================
|
||||||
|
* 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(
|
$user = User::firstOrCreate(
|
||||||
['email' => $roleData['name'] . '@ag.co.id'],
|
['email' => $email],
|
||||||
[
|
[
|
||||||
'name' => $roleData['name'],
|
'name' => $name,
|
||||||
'password' => Hash::make('bagbag'),
|
'password' => Hash::make('bagbag'),
|
||||||
'branch_id' => 1,
|
'branch_id' => $branchId,
|
||||||
'nik' => '000000',
|
'nik' => rand(100000, 999999),
|
||||||
'email_verified_at' => now(),
|
'email_verified_at' => now(),
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
$role = \Spatie\Permission\Models\Role::firstOrCreate(
|
$role = Role::where('name', $roleName)->first();
|
||||||
['name' => $roleData['name']],
|
if ($role) {
|
||||||
['guard_name' => 'web']
|
|
||||||
);
|
|
||||||
|
|
||||||
$user->assignRole($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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
@section('content')
|
@section('content')
|
||||||
<div class="w-full grid gap-5 lg:gap-7.5 mx-auto">
|
<div class="w-full grid gap-5 lg:gap-7.5 mx-auto">
|
||||||
@if(isset($user->id))
|
@if (isset($user->id))
|
||||||
<form action="{{ route('users.update', $user->id) }}" method="POST" enctype="multipart/form-data">
|
<form action="{{ route('users.update', $user->id) }}" method="POST" enctype="multipart/form-data">
|
||||||
<input type="hidden" name="id" value="{{ $user->id }}">
|
<input type="hidden" name="id" value="{{ $user->id }}">
|
||||||
@method('PUT')
|
@method('PUT')
|
||||||
@@ -35,7 +35,8 @@
|
|||||||
Name
|
Name
|
||||||
</label>
|
</label>
|
||||||
<div class="flex flex-wrap items-baseline w-full">
|
<div class="flex flex-wrap items-baseline w-full">
|
||||||
<input class="input @error('name') border-danger @enderror" type="text" name="name" value="{{ $user->name ?? '' }}">
|
<input class="input @error('name') border-danger @enderror" type="text" name="name"
|
||||||
|
value="{{ $user->name ?? '' }}">
|
||||||
@error('name')
|
@error('name')
|
||||||
<em class="alert text-danger text-sm">{{ $message }}</em>
|
<em class="alert text-danger text-sm">{{ $message }}</em>
|
||||||
@enderror
|
@enderror
|
||||||
@@ -46,7 +47,8 @@
|
|||||||
Email
|
Email
|
||||||
</label>
|
</label>
|
||||||
<div class="flex flex-wrap items-baseline w-full">
|
<div class="flex flex-wrap items-baseline w-full">
|
||||||
<input class="w-full input @error('email') border-danger @enderror" type="email" name="email" value="{{ $user->email ?? '' }}">
|
<input class="w-full input @error('email') border-danger @enderror" type="email" name="email"
|
||||||
|
value="{{ $user->email ?? '' }}">
|
||||||
@error('email')
|
@error('email')
|
||||||
<em class="alert text-danger text-sm">{{ $message }}</em>
|
<em class="alert text-danger text-sm">{{ $message }}</em>
|
||||||
@enderror
|
@enderror
|
||||||
@@ -57,7 +59,8 @@
|
|||||||
NIK
|
NIK
|
||||||
</label>
|
</label>
|
||||||
<div class="flex flex-wrap items-baseline w-full">
|
<div class="flex flex-wrap items-baseline w-full">
|
||||||
<input class="w-full input @error('nik') border-danger @enderror" type="number" name="nik" value="{{ $user->nik ?? '' }}">
|
<input class="w-full input @error('nik') border-danger @enderror" type="number" name="nik"
|
||||||
|
value="{{ $user->nik ?? '' }}">
|
||||||
@error('nik')
|
@error('nik')
|
||||||
<em class="alert text-danger text-sm">{{ $message }}</em>
|
<em class="alert text-danger text-sm">{{ $message }}</em>
|
||||||
@enderror
|
@enderror
|
||||||
@@ -68,28 +71,22 @@
|
|||||||
Branch
|
Branch
|
||||||
</label>
|
</label>
|
||||||
<div class="flex flex-wrap items-baseline w-full">
|
<div class="flex flex-wrap items-baseline w-full">
|
||||||
<select class="input tomselect w-full @error('branch_id') border-danger @enderror" name="branch_id" id="branch_id">
|
<select class="input tomselect w-full @error('branches') border-danger @enderror" name="branches[]"
|
||||||
<option value="">Pilih Branch</option>
|
id="branches" multiple>
|
||||||
@if(isset($branches))
|
<option value="">-- Select Branch --</option>
|
||||||
@foreach($branches as $row)
|
@foreach ($branches as $branch)
|
||||||
@if(isset($user))
|
<option value="{{ $branch->id }}"
|
||||||
<option value="{{ $row->id }}" {{ isset($user->branch_id) && $user->branch_id == $row->id?'selected' : '' }}>
|
{{ isset($user) && $user->branches->pluck('id')->contains($branch->id) ? 'selected' : '' }}>
|
||||||
{{ $row->name }}
|
{{ $branch->name }}
|
||||||
</option>
|
</option>
|
||||||
@else
|
|
||||||
<option value="{{ $row->id }}">
|
|
||||||
{{ $row->name }}
|
|
||||||
</option>
|
|
||||||
@endif
|
|
||||||
@endforeach
|
@endforeach
|
||||||
@endif
|
|
||||||
</select>
|
</select>
|
||||||
@error('branch_id')
|
@error('branches')
|
||||||
<em class="alert text-danger text-sm">{{ $message }}</em>
|
<em class="alert text-danger text-sm">{{ $message }}</em>
|
||||||
@enderror
|
@enderror
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@if(isset($user->id))
|
@if (isset($user->id))
|
||||||
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||||
<label class="form-label max-w-56">
|
<label class="form-label max-w-56">
|
||||||
E-Sign
|
E-Sign
|
||||||
@@ -105,8 +102,9 @@
|
|||||||
</label>
|
</label>
|
||||||
|
|
||||||
<div class="flex flex-wrap items-baseline w-full">
|
<div class="flex flex-wrap items-baseline w-full">
|
||||||
<div class="input @error('password') border-danger @enderror" data-toggle-password="true" data-toggle-password-permanent="true">
|
<div class="input @error('password') border-danger @enderror" data-toggle-password="true"
|
||||||
<input placeholder="Password" type="password" name="password"/>
|
data-toggle-password-permanent="true">
|
||||||
|
<input placeholder="Password" type="password" name="password" />
|
||||||
<div class="btn btn-icon" data-toggle-password-trigger="true">
|
<div class="btn btn-icon" data-toggle-password-trigger="true">
|
||||||
<i class="ki-outline ki-eye toggle-password-active:hidden"></i>
|
<i class="ki-outline ki-eye toggle-password-active:hidden"></i>
|
||||||
<i class="ki-outline ki-eye-slash hidden toggle-password-active:block"></i>
|
<i class="ki-outline ki-eye-slash hidden toggle-password-active:block"></i>
|
||||||
@@ -122,8 +120,9 @@
|
|||||||
Password Confirmation
|
Password Confirmation
|
||||||
</label>
|
</label>
|
||||||
<div class="flex flex-wrap items-baseline w-full">
|
<div class="flex flex-wrap items-baseline w-full">
|
||||||
<div class="input @error('password_confirmation') border-danger @enderror" data-toggle-password="true" data-toggle-password-permanent="true">
|
<div class="input @error('password_confirmation') border-danger @enderror"
|
||||||
<input placeholder="Password Confirmation" type="password" name="password_confirmation"/>
|
data-toggle-password="true" data-toggle-password-permanent="true">
|
||||||
|
<input placeholder="Password Confirmation" type="password" name="password_confirmation" />
|
||||||
<div class="btn btn-icon" data-toggle-password-trigger="true">
|
<div class="btn btn-icon" data-toggle-password-trigger="true">
|
||||||
<i class="ki-outline ki-eye toggle-password-active:hidden"></i>
|
<i class="ki-outline ki-eye toggle-password-active:hidden"></i>
|
||||||
<i class="ki-outline ki-eye-slash hidden toggle-password-active:block"></i>
|
<i class="ki-outline ki-eye-slash hidden toggle-password-active:block"></i>
|
||||||
@@ -140,27 +139,35 @@
|
|||||||
</label>
|
</label>
|
||||||
<div class="flex flex-wrap items-baseline w-full">
|
<div class="flex flex-wrap items-baseline w-full">
|
||||||
<div class="grid grid-cols-1 lg:grid-cols-2 gap-5 py-5 lg:py-7.5 w-full">
|
<div class="grid grid-cols-1 lg:grid-cols-2 gap-5 py-5 lg:py-7.5 w-full">
|
||||||
@foreach($roles as $role)
|
@foreach ($roles as $role)
|
||||||
<div class="rounded-xl border p-4 flex items-center justify-between gap-2.5">
|
<div class="rounded-xl border p-4 flex items-center justify-between gap-2.5">
|
||||||
<div class="flex items-center gap-3.5">
|
<div class="flex items-center gap-3.5">
|
||||||
<div class="relative size-[45px] shrink-0">
|
<div class="relative size-[45px] shrink-0">
|
||||||
<svg class="w-full h-full stroke-gray-300 fill-gray-100" fill="none" height="48" viewBox="0 0 44 48" width="44" xmlns="http://www.w3.org/2000/svg">
|
<svg class="w-full h-full stroke-gray-300 fill-gray-100" fill="none"
|
||||||
<path d="M16 2.4641C19.7128 0.320509 24.2872 0.320508 28 2.4641L37.6506 8.0359C41.3634 10.1795 43.6506 14.141 43.6506
|
height="48" viewBox="0 0 44 48" width="44"
|
||||||
|
xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path
|
||||||
|
d="M16 2.4641C19.7128 0.320509 24.2872 0.320508 28 2.4641L37.6506 8.0359C41.3634 10.1795 43.6506 14.141 43.6506
|
||||||
18.4282V29.5718C43.6506 33.859 41.3634 37.8205 37.6506 39.9641L28 45.5359C24.2872 47.6795 19.7128 47.6795 16 45.5359L6.34937
|
18.4282V29.5718C43.6506 33.859 41.3634 37.8205 37.6506 39.9641L28 45.5359C24.2872 47.6795 19.7128 47.6795 16 45.5359L6.34937
|
||||||
39.9641C2.63655 37.8205 0.349365 33.859 0.349365 29.5718V18.4282C0.349365 14.141 2.63655 10.1795 6.34937 8.0359L16 2.4641Z" fill="">
|
39.9641C2.63655 37.8205 0.349365 33.859 0.349365 29.5718V18.4282C0.349365 14.141 2.63655 10.1795 6.34937 8.0359L16 2.4641Z"
|
||||||
|
fill="">
|
||||||
</path>
|
</path>
|
||||||
<path d="M16.25 2.89711C19.8081 0.842838 24.1919 0.842837 27.75 2.89711L37.4006 8.46891C40.9587 10.5232 43.1506 14.3196 43.1506
|
<path
|
||||||
|
d="M16.25 2.89711C19.8081 0.842838 24.1919 0.842837 27.75 2.89711L37.4006 8.46891C40.9587 10.5232 43.1506 14.3196 43.1506
|
||||||
18.4282V29.5718C43.1506 33.6804 40.9587 37.4768 37.4006 39.5311L27.75 45.1029C24.1919 47.1572 19.8081 47.1572 16.25 45.1029L6.59937
|
18.4282V29.5718C43.1506 33.6804 40.9587 37.4768 37.4006 39.5311L27.75 45.1029C24.1919 47.1572 19.8081 47.1572 16.25 45.1029L6.59937
|
||||||
39.5311C3.04125 37.4768 0.849365 33.6803 0.849365 29.5718V18.4282C0.849365 14.3196 3.04125 10.5232 6.59937 8.46891L16.25 2.89711Z" stroke="">
|
39.5311C3.04125 37.4768 0.849365 33.6803 0.849365 29.5718V18.4282C0.849365 14.3196 3.04125 10.5232 6.59937 8.46891L16.25 2.89711Z"
|
||||||
|
stroke="">
|
||||||
</path>
|
</path>
|
||||||
</svg>
|
</svg>
|
||||||
<div class="absolute leading-none left-2/4 top-2/4 -translate-y-2/4 -translate-x-2/4">
|
<div
|
||||||
|
class="absolute leading-none left-2/4 top-2/4 -translate-y-2/4 -translate-x-2/4">
|
||||||
<i class="ki-filled ki-category text-lg text-gray-500">
|
<i class="ki-filled ki-category text-lg text-gray-500">
|
||||||
</i>
|
</i>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="flex flex-col gap-1">
|
<div class="flex flex-col gap-1">
|
||||||
<span class="flex items-center gap-1.5 leading-none font-medium text-sm text-gray-900">
|
<span
|
||||||
|
class="flex items-center gap-1.5 leading-none font-medium text-sm text-gray-900">
|
||||||
{{ $role->name }}
|
{{ $role->name }}
|
||||||
</span>
|
</span>
|
||||||
<span class="text-2sm text-gray-700">
|
<span class="text-2sm text-gray-700">
|
||||||
@@ -169,8 +176,10 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="switch switch-sm">
|
<div class="switch switch-sm">
|
||||||
@if(isset($user))
|
@if (isset($user))
|
||||||
<input {{ in_array($role->name,$user->roles->pluck("name")->toArray()) ? 'checked' : '' }} name="roles" type="radio" value="{{ $role->name }}">
|
<input
|
||||||
|
{{ in_array($role->name, $user->roles->pluck('name')->toArray()) ? 'checked' : '' }}
|
||||||
|
name="roles" type="radio" value="{{ $role->name }}">
|
||||||
@else
|
@else
|
||||||
<input name="roles" type="radio" value="{{ $role->name }}">
|
<input name="roles" type="radio" value="{{ $role->name }}">
|
||||||
@endif
|
@endif
|
||||||
|
|||||||
@@ -160,7 +160,10 @@
|
|||||||
branch: {
|
branch: {
|
||||||
title: 'Branch',
|
title: 'Branch',
|
||||||
render: (item, data) => {
|
render: (item, data) => {
|
||||||
return data.branch?.name || '-';
|
if (data.branches && data.branches.length > 0) {
|
||||||
|
return data.branches.map(b => b.name).join(', ');
|
||||||
|
}
|
||||||
|
return '-';
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
role: {
|
role: {
|
||||||
|
|||||||
Reference in New Issue
Block a user