glastree_on_gitea
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Middleware;
|
||||
|
||||
use BackedEnum;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Spatie\Permission\Exceptions\UnauthorizedException;
|
||||
use Spatie\Permission\Guard;
|
||||
use Spatie\Permission\Support\Config;
|
||||
|
||||
use function Illuminate\Support\enum_value;
|
||||
|
||||
class PermissionMiddleware
|
||||
{
|
||||
public function handle(Request $request, Closure $next, $permission, ?string $guard = null)
|
||||
{
|
||||
$authGuard = Auth::guard($guard);
|
||||
|
||||
$user = $authGuard->user();
|
||||
|
||||
// For machine-to-machine Passport clients
|
||||
if (! $user && $request->bearerToken() && Config::usePassportClientCredentials()) {
|
||||
$user = Guard::getPassportClient($guard);
|
||||
}
|
||||
|
||||
if (! $user) {
|
||||
throw UnauthorizedException::notLoggedIn();
|
||||
}
|
||||
|
||||
if (! method_exists($user, 'hasAnyPermission')) {
|
||||
throw UnauthorizedException::missingTraitHasRoles($user);
|
||||
}
|
||||
|
||||
$permissions = explode('|', self::parsePermissionsToString($permission));
|
||||
|
||||
if (! $user->canAny($permissions)) {
|
||||
throw UnauthorizedException::forPermissions($permissions);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the permission and guard for the middleware.
|
||||
*/
|
||||
public static function using(array|string|BackedEnum $permission, ?string $guard = null): string
|
||||
{
|
||||
$permissionString = self::parsePermissionsToString(enum_value($permission));
|
||||
|
||||
$args = is_null($guard) ? $permissionString : "$permissionString,$guard";
|
||||
|
||||
return static::class.':'.$args;
|
||||
}
|
||||
|
||||
protected static function parsePermissionsToString(array|string|BackedEnum $permission): string
|
||||
{
|
||||
$permission = enum_value($permission);
|
||||
|
||||
if (is_array($permission)) {
|
||||
return implode('|', array_map(fn ($r) => enum_value($r), $permission));
|
||||
}
|
||||
|
||||
return (string) $permission;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Middleware;
|
||||
|
||||
use BackedEnum;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Spatie\Permission\Exceptions\UnauthorizedException;
|
||||
use Spatie\Permission\Guard;
|
||||
use Spatie\Permission\Support\Config;
|
||||
|
||||
use function Illuminate\Support\enum_value;
|
||||
|
||||
class RoleMiddleware
|
||||
{
|
||||
public function handle(Request $request, Closure $next, $role, ?string $guard = null)
|
||||
{
|
||||
$authGuard = Auth::guard($guard);
|
||||
|
||||
$user = $authGuard->user();
|
||||
|
||||
// For machine-to-machine Passport clients
|
||||
if (! $user && $request->bearerToken() && Config::usePassportClientCredentials()) {
|
||||
$user = Guard::getPassportClient($guard);
|
||||
}
|
||||
|
||||
if (! $user) {
|
||||
throw UnauthorizedException::notLoggedIn();
|
||||
}
|
||||
|
||||
if (! method_exists($user, 'hasAnyRole')) {
|
||||
throw UnauthorizedException::missingTraitHasRoles($user);
|
||||
}
|
||||
|
||||
$roles = explode('|', self::parseRolesToString($role));
|
||||
|
||||
if (! $user->hasAnyRole($roles)) {
|
||||
throw UnauthorizedException::forRoles($roles);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the role and guard for the middleware.
|
||||
*/
|
||||
public static function using(array|string|BackedEnum $role, ?string $guard = null): string
|
||||
{
|
||||
$roleString = self::parseRolesToString($role);
|
||||
|
||||
$args = is_null($guard) ? $roleString : "$roleString,$guard";
|
||||
|
||||
return static::class.':'.$args;
|
||||
}
|
||||
|
||||
protected static function parseRolesToString(array|string|BackedEnum $role): string
|
||||
{
|
||||
$role = enum_value($role);
|
||||
|
||||
if (is_array($role)) {
|
||||
return implode('|', array_map(fn ($r) => enum_value($r), $role));
|
||||
}
|
||||
|
||||
return (string) $role;
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Middleware;
|
||||
|
||||
use BackedEnum;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Spatie\Permission\Exceptions\UnauthorizedException;
|
||||
use Spatie\Permission\Guard;
|
||||
use Spatie\Permission\Support\Config;
|
||||
|
||||
use function Illuminate\Support\enum_value;
|
||||
|
||||
class RoleOrPermissionMiddleware
|
||||
{
|
||||
public function handle(Request $request, Closure $next, $roleOrPermission, ?string $guard = null)
|
||||
{
|
||||
$authGuard = Auth::guard($guard);
|
||||
|
||||
$user = $authGuard->user();
|
||||
|
||||
// For machine-to-machine Passport clients
|
||||
if (! $user && $request->bearerToken() && Config::usePassportClientCredentials()) {
|
||||
$user = Guard::getPassportClient($guard);
|
||||
}
|
||||
|
||||
if (! $user) {
|
||||
throw UnauthorizedException::notLoggedIn();
|
||||
}
|
||||
|
||||
if (! method_exists($user, 'hasAnyRole') || ! method_exists($user, 'hasAnyPermission')) {
|
||||
throw UnauthorizedException::missingTraitHasRoles($user);
|
||||
}
|
||||
|
||||
$rolesOrPermissions = explode('|', self::parseRoleOrPermissionToString($roleOrPermission));
|
||||
|
||||
if (! $user->canAny($rolesOrPermissions) && ! $user->hasAnyRole($rolesOrPermissions)) {
|
||||
throw UnauthorizedException::forRolesOrPermissions($rolesOrPermissions);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the role or permission and guard for the middleware.
|
||||
*/
|
||||
public static function using(array|string|BackedEnum $roleOrPermission, ?string $guard = null): string
|
||||
{
|
||||
$roleOrPermissionString = self::parseRoleOrPermissionToString($roleOrPermission);
|
||||
$args = is_null($guard) ? $roleOrPermissionString : "$roleOrPermissionString,$guard";
|
||||
|
||||
return static::class.':'.$args;
|
||||
}
|
||||
|
||||
protected static function parseRoleOrPermissionToString(array|string|BackedEnum $roleOrPermission): string
|
||||
{
|
||||
$roleOrPermission = enum_value($roleOrPermission);
|
||||
|
||||
if (is_array($roleOrPermission)) {
|
||||
return implode('|', array_map(fn ($r) => enum_value($r), $roleOrPermission));
|
||||
}
|
||||
|
||||
return (string) $roleOrPermission;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user