feat: implement admin management
This commit is contained in:
@@ -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', 'حذف دائمی مدیر با خطا مواجه شد.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
'مدیر عادی نمیتواند دسترسی مدیریت مدیران را داشته باشد.'
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+11
-2
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Reliese Model.
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class AdminAvatar
|
||||
*
|
||||
* @property int $id
|
||||
* @property int|null $file_id
|
||||
* @property int $admin_id
|
||||
* @property Carbon $created_at
|
||||
*
|
||||
* @property Admin $admin
|
||||
* @property File|null $file
|
||||
*
|
||||
* @package App\Models
|
||||
*/
|
||||
class AdminAvatar extends Model
|
||||
{
|
||||
protected $table = 'admin_avatars';
|
||||
|
||||
public static $snakeAttributes = false;
|
||||
|
||||
protected $casts = [
|
||||
'file_id' => '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);
|
||||
}
|
||||
}
|
||||
+6
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user