diff --git a/app/Http/Controllers/Admin/AdminController.php b/app/Http/Controllers/Admin/AdminController.php index f80a85a..5524e47 100644 --- a/app/Http/Controllers/Admin/AdminController.php +++ b/app/Http/Controllers/Admin/AdminController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Http\Requests\Admin\AdminStoreRequest; use App\Models\Admin; +use App\Models\AdminAvatar; use App\Models\AdminPermission; use App\Models\File; use Illuminate\Database\Eloquent\Builder; @@ -72,6 +73,8 @@ class AdminController extends Controller ->paginate() ->withQueryString(); + + return view('admin.admins.index', compact('title', 'admins')); } @@ -132,19 +135,20 @@ class AdminController extends Controller 'extension' => $image->extension(), ]); -// AdminAvatar::create([ -// 'admin_id' => $admin->id, -// 'file_id' => $file->id, -// ]); + AdminAvatar::create([ + 'admin_id' => $admin->id, + 'file_id' => $file->id, + ]); } - foreach ($data['permissions'] as $permission) { + foreach ($data['permissions'] ?? [] as $permission) { AdminPermission::create([ 'admin_id' => $admin->id, 'permission' => $permission, ]); } + DB::commit(); return redirect() @@ -177,23 +181,80 @@ class AdminController extends Controller } - public function destroy() + public function destroy(Admin $admin) { + if ($admin->id === auth('admin')->id()) { + return back()->with('error', 'امکان حذف حساب کاربری خودتان وجود ندارد.'); + } + + try { + $admin->delete(); + + return redirect() + ->route('admin.admins.index') + ->with('success', 'مدیر با موفقیت به سطل زباله منتقل شد.'); + + } catch (\Exception $exception) { + Log::error('خطا در حذف مدیر', [ + 'admin_id' => $admin->id, + 'message' => $exception->getMessage(), + ]); + + return back()->with('error', 'خطایی هنگام حذف مدیر رخ داد.'); + } } public function trash() { - + $title = 'مدیران حذف شده'; + $admins = Admin::onlyTrashed() + ->paginate(); + return view('admin.admins.trash' , compact('title' , 'admins')); } - public function restore() + public function restore($id) { + $admin = Admin::onlyTrashed()->findOrFail($id); + $admin->restore(); + + return redirect() + ->route('admin.admins.trash.index') + ->with('success', 'مدیر با موفقیت بازیابی شد.'); } - public function forceDelete() + public function forceDelete($id) { + $admin = Admin::onlyTrashed()->findOrFail($id); + $avatar = $admin->avatar()->with('file')->first(); + $file = $avatar?->file; + + DB::beginTransaction(); + + try { + $avatar?->delete(); + + deleteFile($file); + + $admin->forceDelete(); + + DB::commit(); + + return redirect() + ->route('admin.admins.trash.index') + ->with('success', 'مدیر و آواتار او برای همیشه حذف شدند.'); + + } catch (\Exception $exception) { + DB::rollBack(); + + Log::error('خطا در حذف دائمی مدیر', [ + 'admin_id' => $admin->id, + 'message' => $exception->getMessage(), + ]); + + return back()->with('error', 'حذف دائمی مدیر با خطا مواجه شد.'); + } } } diff --git a/app/Http/Requests/Admin/AdminStoreRequest.php b/app/Http/Requests/Admin/AdminStoreRequest.php index 393bd27..d33e1c0 100644 --- a/app/Http/Requests/Admin/AdminStoreRequest.php +++ b/app/Http/Requests/Admin/AdminStoreRequest.php @@ -88,5 +88,37 @@ class AdminStoreRequest extends FormRequest Rule::enum(AdminPermission::class), ], ]; + + } + public function withValidator($validator): void + { + $validator->after(function ($validator) { + + $role = $this->input('role'); + + $permissions = $this->input('permissions', []); + + + $isSuperAdmin = + $role === \App\Enums\Admin\AdminRole::SUPERADMIN->value; + + + $manageAdminsPermission = + \App\Enums\Admin\AdminPermission::MANAGE_ADMINS->value; + + + if ( + !$isSuperAdmin && + in_array($manageAdminsPermission, $permissions) + ) { + + $validator->errors()->add( + 'permissions', + 'مدیر عادی نمیتواند دسترسی مدیریت مدیران را داشته باشد.' + ); + + } + + }); } } diff --git a/app/Models/Admin.php b/app/Models/Admin.php index d417f31..eb2aa94 100644 --- a/app/Models/Admin.php +++ b/app/Models/Admin.php @@ -79,9 +79,18 @@ class Admin extends User { return $this->hasMany(AdminPermission::class); } - public function hasPermission(string $permission): bool + public function avatar() { - if ($this->role === AdminRole::SUPERADMIN) { + return $this->hasOne(AdminAvatar::class); + } + public function isSuperAdmin(): bool + { + return $this->role === \App\Enums\Admin\AdminRole::SUPERADMIN; + } + + public function canAccess(string $permission): bool + { + if ($this->isSuperAdmin()) { return true; } diff --git a/app/Models/AdminAvatar.php b/app/Models/AdminAvatar.php new file mode 100644 index 0000000..7196961 --- /dev/null +++ b/app/Models/AdminAvatar.php @@ -0,0 +1,50 @@ + 'int', + 'admin_id' => 'int' + ]; + + protected $fillable = [ + 'file_id', + 'admin_id' + ]; + + public function admin() + { + return $this->belongsTo(Admin::class); + } + + public function file() + { + return $this->belongsTo(File::class); + } +} diff --git a/app/Models/File.php b/app/Models/File.php index 5f051e0..146e833 100644 --- a/app/Models/File.php +++ b/app/Models/File.php @@ -12,7 +12,7 @@ use Illuminate\Database\Eloquent\Model; /** * Class File - * + * * @property int $id * @property string $name * @property string $extension @@ -21,7 +21,7 @@ use Illuminate\Database\Eloquent\Model; * @property string $path * @property Carbon $created_at * @property Carbon $updated_at - * + * * @property Collection|Flight[] $flights * @property Collection|User[] $users * @@ -53,4 +53,8 @@ class File extends Model { return $this->hasMany(User::class); } + public function adminAvatar() + { + return $this->hasOne(AdminAvatar::class); + } } diff --git a/app/helpers/helper.php b/app/helpers/helper.php index 2616d58..24e7625 100644 --- a/app/helpers/helper.php +++ b/app/helpers/helper.php @@ -83,3 +83,36 @@ if (!function_exists('getUserFullName')) { return $user->first_name . ' ' . $user->last_name; } } +if (! function_exists('deleteFile')) { + function deleteFile(?File $file): void + { + if (! $file) { + return; + } + + + if (Storage::exists($file->path)) { + Storage::delete($file->path); + } + + + $file->delete(); + } +} + +if (!function_exists('getFileUrl')) { + function getFileUrl(?int $fileId): ?string + { + if (!$fileId){ + return null; + } + + $file = File::find($fileId); + + if (!$file){ + return null; + } + return \Illuminate\Support\Facades\Storage::url($file->path); + + } +} diff --git a/resources/views/admin/admins/create.blade.php b/resources/views/admin/admins/create.blade.php index c008428..e496a54 100644 --- a/resources/views/admin/admins/create.blade.php +++ b/resources/views/admin/admins/create.blade.php @@ -206,27 +206,22 @@ @error('role') - {{ $message }} - + {{ $message }} + @enderror @@ -271,7 +266,7 @@ {{-- Permissions --}} -
| تصویر | +نام و نام خانوادگی | +نام کاربری | +ایمیل | +نوع حساب | +تاریخ ثبت | +تاریخ حذف | +عملیات | +
|---|---|---|---|---|---|---|---|
| + {{ session('success') }} + | +|||||||
|
+ @foreach($errors->all() as $error)
+ {{ $error }}
+ @endforeach
+ |
+ |||||||
|
+
+ |
+
+ + {{ $admin->first_name }} {{ $admin->last_name }} + | + ++ {{ $admin->username }} + | + ++ {{ $admin->email ?: '-' }} + | + ++ @if($admin->is_super) + مدیر کل + @else + مدیر عادی + @endif + | + ++ {{ $admin->created_at->toJalali()->format('H:i Y/m/d') }} + | + ++ {{ $admin->deleted_at?->toJalali()->format('H:i Y/m/d') }} + | + +
+
+
+
+
+
+
+
+ |
+
| + مدیری در سطل زباله وجود ندارد. + | +|||||||
| تصویر | +نام و نام خانوادگی | +نام کاربری | +ایمیل | +نوع حساب | +تاریخ ثبت | +تاریخ حذف | +عملیات | +
|---|---|---|---|---|---|---|---|
| + + | +|||||||
| + all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?> + + popLoop(); $loop = $__env->getLastLoop(); ?> + | +|||||||
|
+
+ |
+
+ + first_name); ?> last_name); ?> + + | + ++ username); ?> + + | + ++ email ?: '-'); ?> + + | + ++ is_super): ?> + مدیر کل + + مدیر عادی + + | + ++ created_at->toJalali()->format('H:i Y/m/d')); ?> + + | + ++ deleted_at?->toJalali()->format('H:i Y/m/d')); ?> + + | + +
+
+
+
+
+
+
+
+ |
+
| + مدیری در سطل زباله وجود ندارد. + | +|||||||