feat:implement permissions for admin side
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class AdminPermissionMiddleware
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param Closure(Request): (Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next, string $permission): Response
|
||||
{
|
||||
$admin = auth('admin')->user();
|
||||
|
||||
if (!$admin) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$hasPermission = $admin->permissions()
|
||||
->where('name', $permission)
|
||||
->exists();
|
||||
|
||||
if (!$hasPermission) {
|
||||
abort(403, 'شما اجازه دسترسی به این بخش را ندارید.');
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Reliese Model.
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
use App\Models\Admin;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class Permission
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $title
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
*
|
||||
* @property Collection|Admin[] $admins
|
||||
*
|
||||
* @package App\Models
|
||||
*/
|
||||
class Permission extends Model
|
||||
{
|
||||
protected $table = 'permissions';
|
||||
public static $snakeAttributes = false;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'title'
|
||||
];
|
||||
|
||||
public function admins()
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
Admin::class,
|
||||
'admin_permission',
|
||||
'permission_id',
|
||||
'admin_id'
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user