feat(admin): complete admin management and add edit functionality
This commit is contained in:
@@ -2,8 +2,11 @@
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Enums\Admin\AdminRole;
|
||||
use App\Enums\Admin\AdminStatus;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\AdminStoreRequest;
|
||||
use App\Http\Requests\Admin\AdminUpdateRequest;
|
||||
use App\Models\Admin;
|
||||
use App\Models\AdminAvatar;
|
||||
use App\Models\AdminPermission;
|
||||
@@ -80,7 +83,152 @@ class AdminController extends Controller
|
||||
|
||||
public function edit(Admin $admin)
|
||||
{
|
||||
return view('admin.admins.edit', compact('admin'));
|
||||
$title = 'جزئیات مدیر';
|
||||
$loggedInAdmin = auth('admin')->user();
|
||||
|
||||
$canEditSensitiveFields = $loggedInAdmin->isSuperAdmin() && $loggedInAdmin->id !== $admin->id;
|
||||
|
||||
$showReadonlySensitiveFields = $loggedInAdmin->id === $admin->id;
|
||||
|
||||
$adminRoles = AdminRole::cases();
|
||||
|
||||
$adminStatuses = AdminStatus::cases();
|
||||
|
||||
$admin->load([
|
||||
'avatar.file',
|
||||
'permissions',
|
||||
]);
|
||||
return view('admin.admins.edit', compact(
|
||||
'title',
|
||||
'admin',
|
||||
'adminRoles',
|
||||
'adminStatuses',
|
||||
'canEditSensitiveFields',
|
||||
'showReadonlySensitiveFields'
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
public function update(AdminUpdateRequest $request,Admin $admin)
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
$newAvatarPath = null;
|
||||
$oldFile = null;
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$updateData = [
|
||||
'first_name' => $data['first_name'],
|
||||
'last_name' => $data['last_name'],
|
||||
'username' => $data['username'],
|
||||
'email' => $data['email'] ?? null,
|
||||
'mobile' => $data['mobile'] ?? null,
|
||||
];
|
||||
|
||||
if (!empty($data['password'])) {
|
||||
$updateData['password'] = Hash::make($data['password']);
|
||||
}
|
||||
|
||||
$loggedInAdmin = auth('admin')->user();
|
||||
|
||||
|
||||
$canEditSensitiveFields =
|
||||
$loggedInAdmin->isSuperAdmin()
|
||||
&& $loggedInAdmin->id !== $admin->id;
|
||||
|
||||
if ($canEditSensitiveFields) {
|
||||
if (array_key_exists('role', $data)) {
|
||||
$updateData['role'] = $data['role'];
|
||||
}
|
||||
|
||||
if (array_key_exists('status', $data)) {
|
||||
$updateData['status'] = $data['status'];
|
||||
}
|
||||
}
|
||||
|
||||
$admin->update($updateData);
|
||||
|
||||
if ($canEditSensitiveFields && array_key_exists('permissions', $data)) {
|
||||
$admin->permissions()->delete();
|
||||
|
||||
if (!$admin->isSuperAdmin()) {
|
||||
foreach ($data['permissions'] ?? [] as $permission) {
|
||||
$admin->permissions()->create([
|
||||
'permission' => $permission,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ($request->hasFile('image')) {
|
||||
$oldAvatar = $admin->avatar()
|
||||
->with('file')
|
||||
->first();
|
||||
|
||||
$oldFile = $oldAvatar?->file;
|
||||
|
||||
$image = $data['image'];
|
||||
|
||||
$imageName = $admin->id
|
||||
. '_' . time() . rand(11111, 99999) . '.' . $image->extension();
|
||||
|
||||
$newAvatarPath = $image->storeAs(
|
||||
'admin_avatars',
|
||||
$imageName,
|
||||
'public'
|
||||
);
|
||||
|
||||
$file = File::create([
|
||||
'name' => $imageName,
|
||||
'size' => $image->getSize(),
|
||||
'original_name' => $image->getClientOriginalName(),
|
||||
'path' => $newAvatarPath,
|
||||
'extension' => $image->extension(),
|
||||
]);
|
||||
|
||||
if ($oldAvatar) {
|
||||
$oldAvatar->update([
|
||||
'file_id' => $file->id,
|
||||
]);
|
||||
} else {
|
||||
AdminAvatar::create([
|
||||
'admin_id' => $admin->id,
|
||||
'file_id' => $file->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
if ($oldFile) {
|
||||
deleteFile($oldFile);
|
||||
}
|
||||
|
||||
$redirectRoute = $loggedInAdmin->isSuperAdmin()
|
||||
? 'admin.admins.index'
|
||||
: 'admin.profile.index';
|
||||
|
||||
return redirect()
|
||||
->route($redirectRoute)
|
||||
->with('success', 'اطلاعات مدیر با موفقیت ویرایش شد.');
|
||||
|
||||
} catch (\Exception $exception) {
|
||||
DB::rollBack();
|
||||
|
||||
|
||||
if ($newAvatarPath && Storage::disk('public')->exists($newAvatarPath)) {
|
||||
Storage::disk('public')->delete($newAvatarPath);
|
||||
}
|
||||
|
||||
Log::error($exception);
|
||||
|
||||
return back()
|
||||
->withInput()
|
||||
->with('error', 'ویرایش مدیر با خطا مواجه شد.');
|
||||
}
|
||||
}
|
||||
|
||||
public function create()
|
||||
@@ -113,13 +261,7 @@ class AdminController extends Controller
|
||||
if ($request->hasFile('image')) {
|
||||
$image = $request->file('image');
|
||||
|
||||
$imageName = $admin->id
|
||||
. '_'
|
||||
. time()
|
||||
. '_'
|
||||
. rand(11111, 99999)
|
||||
. '.'
|
||||
. $image->extension();
|
||||
$imageName = $admin->id . '_' . time() . '_' . rand(11111, 99999) . '.' . $image->extension();
|
||||
|
||||
$avatarPath = $image->storeAs(
|
||||
'admin_avatars',
|
||||
@@ -172,7 +314,6 @@ class AdminController extends Controller
|
||||
->withInput(
|
||||
$request->except([
|
||||
'password',
|
||||
'password_confirmation',
|
||||
'image',
|
||||
])
|
||||
)
|
||||
|
||||
@@ -17,7 +17,7 @@ class CheckAdminPermission
|
||||
{
|
||||
$admin = auth('admin')->user();
|
||||
|
||||
if (! auth('admin')->user()?->hasPermission($permission)) {
|
||||
if (! auth('admin')->user()?->canAccess($permission)) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use App\Enums\Admin\AdminRole;
|
||||
use App\Enums\Admin\AdminStatus;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class AdminUpdateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$admin = $this->route('admin');
|
||||
return [
|
||||
'first_name' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:64',
|
||||
'persianAlpha',
|
||||
],
|
||||
|
||||
'last_name' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:64',
|
||||
'persianAlpha',
|
||||
],
|
||||
|
||||
'username' => [
|
||||
'required',
|
||||
'string',
|
||||
'min:3',
|
||||
'max:100',
|
||||
'regex:/^[a-zA-Z][a-zA-Z0-9_]*$/',
|
||||
Rule::unique('admins', 'username')->ignore($admin),
|
||||
],
|
||||
|
||||
'email' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'email',
|
||||
'max:150',
|
||||
Rule::unique('admins', 'email')->ignore($admin),
|
||||
],
|
||||
|
||||
'mobile' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'regex:/^09\d{9}$/',
|
||||
'ir_mobile:zero',
|
||||
Rule::unique('admins', 'mobile')->ignore($admin),
|
||||
],
|
||||
'password' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'min:8',
|
||||
'max:255',
|
||||
],
|
||||
|
||||
'role' => [
|
||||
'nullable',
|
||||
Rule::enum(AdminRole::class),
|
||||
],
|
||||
|
||||
'status' => [
|
||||
'nullable',
|
||||
Rule::enum(AdminStatus::class),
|
||||
],
|
||||
|
||||
'permissions' => [
|
||||
'nullable',
|
||||
'array',
|
||||
],
|
||||
|
||||
'permissions.*' => [
|
||||
'required',
|
||||
'string',
|
||||
],
|
||||
|
||||
'image' => [
|
||||
'nullable',
|
||||
'image',
|
||||
'mimes:jpg,jpeg,png,webp',
|
||||
'max:800',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -84,7 +84,7 @@ if (!function_exists('getUserFullName')) {
|
||||
}
|
||||
}
|
||||
if (! function_exists('deleteFile')) {
|
||||
function deleteFile(?File $file): void
|
||||
function deleteFile(?\App\Models\File $file): void
|
||||
{
|
||||
if (! $file) {
|
||||
return;
|
||||
@@ -107,7 +107,7 @@ if (!function_exists('getFileUrl')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$file = File::find($fileId);
|
||||
$file = \App\Models\File::find($fileId);
|
||||
|
||||
if (!$file){
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user