diff --git a/app/Http/Controllers/Admin/AdminController.php b/app/Http/Controllers/Admin/AdminController.php index 5524e47..e9b95e6 100644 --- a/app/Http/Controllers/Admin/AdminController.php +++ b/app/Http/Controllers/Admin/AdminController.php @@ -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', ]) ) diff --git a/app/Http/Middleware/Admin/CheckAdminPermission.php b/app/Http/Middleware/Admin/CheckAdminPermission.php index 0d6ab1f..711b5cf 100644 --- a/app/Http/Middleware/Admin/CheckAdminPermission.php +++ b/app/Http/Middleware/Admin/CheckAdminPermission.php @@ -17,7 +17,7 @@ class CheckAdminPermission { $admin = auth('admin')->user(); - if (! auth('admin')->user()?->hasPermission($permission)) { + if (! auth('admin')->user()?->canAccess($permission)) { abort(403); } diff --git a/app/Http/Requests/Admin/AdminUpdateRequest.php b/app/Http/Requests/Admin/AdminUpdateRequest.php new file mode 100644 index 0000000..71fec64 --- /dev/null +++ b/app/Http/Requests/Admin/AdminUpdateRequest.php @@ -0,0 +1,103 @@ +|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', + ], + ]; + } +} diff --git a/app/helpers/helper.php b/app/helpers/helper.php index 24e7625..0044a40 100644 --- a/app/helpers/helper.php +++ b/app/helpers/helper.php @@ -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; diff --git a/public/assets/js/images.jpg b/public/assets/js/images.jpg new file mode 100644 index 0000000..3ff2975 Binary files /dev/null and b/public/assets/js/images.jpg differ diff --git a/resources/views/admin/admins/index.blade.php b/resources/views/admin/admins/index.blade.php index 2a6893d..b0f56da 100644 --- a/resources/views/admin/admins/index.blade.php +++ b/resources/views/admin/admins/index.blade.php @@ -83,7 +83,6 @@
| تصویر | نام | نام خانوادگی | @@ -99,13 +98,10 @@|
|---|---|---|---|
| - - |
- @if($admin->file)
- |