update inbox list
This commit is contained in:
22
vendor/spatie/laravel-permission/src/Commands/CacheReset.php
vendored
Normal file
22
vendor/spatie/laravel-permission/src/Commands/CacheReset.php
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Spatie\Permission\PermissionRegistrar;
|
||||
|
||||
class CacheReset extends Command
|
||||
{
|
||||
protected $signature = 'permission:cache-reset';
|
||||
|
||||
protected $description = 'Reset the permission cache';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
if (app(PermissionRegistrar::class)->forgetCachedPermissions()) {
|
||||
$this->info('Permission cache flushed.');
|
||||
} else {
|
||||
$this->error('Unable to flush cache.');
|
||||
}
|
||||
}
|
||||
}
|
||||
24
vendor/spatie/laravel-permission/src/Commands/CreatePermission.php
vendored
Normal file
24
vendor/spatie/laravel-permission/src/Commands/CreatePermission.php
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Spatie\Permission\Contracts\Permission as PermissionContract;
|
||||
|
||||
class CreatePermission extends Command
|
||||
{
|
||||
protected $signature = 'permission:create-permission
|
||||
{name : The name of the permission}
|
||||
{guard? : The name of the guard}';
|
||||
|
||||
protected $description = 'Create a permission';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$permissionClass = app(PermissionContract::class);
|
||||
|
||||
$permission = $permissionClass::findOrCreate($this->argument('name'), $this->argument('guard'));
|
||||
|
||||
$this->info("Permission `{$permission->name}` created");
|
||||
}
|
||||
}
|
||||
50
vendor/spatie/laravel-permission/src/Commands/CreateRole.php
vendored
Normal file
50
vendor/spatie/laravel-permission/src/Commands/CreateRole.php
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Spatie\Permission\Contracts\Role as RoleContract;
|
||||
use Spatie\Permission\Contracts\Permission as PermissionContract;
|
||||
|
||||
class CreateRole extends Command
|
||||
{
|
||||
protected $signature = 'permission:create-role
|
||||
{name : The name of the role}
|
||||
{guard? : The name of the guard}
|
||||
{permissions? : A list of permissions to assign to the role, separated by | }';
|
||||
|
||||
protected $description = 'Create a role';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$roleClass = app(RoleContract::class);
|
||||
|
||||
$role = $roleClass::findOrCreate($this->argument('name'), $this->argument('guard'));
|
||||
|
||||
$role->givePermissionTo($this->makePermissions($this->argument('permissions')));
|
||||
|
||||
$this->info("Role `{$role->name}` created");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|null|string $string
|
||||
*/
|
||||
protected function makePermissions($string = null)
|
||||
{
|
||||
if (empty($string)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$permissionClass = app(PermissionContract::class);
|
||||
|
||||
$permissions = explode('|', $string);
|
||||
|
||||
$models = [];
|
||||
|
||||
foreach ($permissions as $permission) {
|
||||
$models[] = $permissionClass::findOrCreate(trim($permission), $this->argument('guard'));
|
||||
}
|
||||
|
||||
return collect($models);
|
||||
}
|
||||
}
|
||||
54
vendor/spatie/laravel-permission/src/Commands/Show.php
vendored
Normal file
54
vendor/spatie/laravel-permission/src/Commands/Show.php
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\Permission\Contracts\Role as RoleContract;
|
||||
use Spatie\Permission\Contracts\Permission as PermissionContract;
|
||||
|
||||
class Show extends Command
|
||||
{
|
||||
protected $signature = 'permission:show
|
||||
{guard? : The name of the guard}
|
||||
{style? : The display style (default|borderless|compact|box)}';
|
||||
|
||||
protected $description = 'Show a table of roles and permissions per guard';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$permissionClass = app(PermissionContract::class);
|
||||
$roleClass = app(RoleContract::class);
|
||||
|
||||
$style = $this->argument('style') ?? 'default';
|
||||
$guard = $this->argument('guard');
|
||||
|
||||
if ($guard) {
|
||||
$guards = Collection::make([$guard]);
|
||||
} else {
|
||||
$guards = $permissionClass::pluck('guard_name')->merge($roleClass::pluck('guard_name'))->unique();
|
||||
}
|
||||
|
||||
foreach ($guards as $guard) {
|
||||
$this->info("Guard: $guard");
|
||||
|
||||
$roles = $roleClass::whereGuardName($guard)->orderBy('name')->get()->mapWithKeys(function ($role) {
|
||||
return [$role->name => $role->permissions->pluck('name')];
|
||||
});
|
||||
|
||||
$permissions = $permissionClass::whereGuardName($guard)->orderBy('name')->pluck('name');
|
||||
|
||||
$body = $permissions->map(function ($permission) use ($roles) {
|
||||
return $roles->map(function (Collection $role_permissions) use ($permission) {
|
||||
return $role_permissions->contains($permission) ? ' ✔' : ' ·';
|
||||
})->prepend($permission);
|
||||
});
|
||||
|
||||
$this->table(
|
||||
$roles->keys()->prepend('')->toArray(),
|
||||
$body->toArray(),
|
||||
$style
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
49
vendor/spatie/laravel-permission/src/Contracts/Permission.php
vendored
Normal file
49
vendor/spatie/laravel-permission/src/Contracts/Permission.php
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Contracts;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
interface Permission
|
||||
{
|
||||
/**
|
||||
* A permission can be applied to roles.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
public function roles(): BelongsToMany;
|
||||
|
||||
/**
|
||||
* Find a permission by its name.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist
|
||||
*
|
||||
* @return Permission
|
||||
*/
|
||||
public static function findByName(string $name, $guardName): self;
|
||||
|
||||
/**
|
||||
* Find a permission by its id.
|
||||
*
|
||||
* @param int $id
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist
|
||||
*
|
||||
* @return Permission
|
||||
*/
|
||||
public static function findById(int $id, $guardName): self;
|
||||
|
||||
/**
|
||||
* Find or Create a permission by its name and guard name.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @return Permission
|
||||
*/
|
||||
public static function findOrCreate(string $name, $guardName): self;
|
||||
}
|
||||
58
vendor/spatie/laravel-permission/src/Contracts/Role.php
vendored
Normal file
58
vendor/spatie/laravel-permission/src/Contracts/Role.php
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Contracts;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
interface Role
|
||||
{
|
||||
/**
|
||||
* A role may be given various permissions.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
public function permissions(): BelongsToMany;
|
||||
|
||||
/**
|
||||
* Find a role by its name and guard name.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @return \Spatie\Permission\Contracts\Role
|
||||
*
|
||||
* @throws \Spatie\Permission\Exceptions\RoleDoesNotExist
|
||||
*/
|
||||
public static function findByName(string $name, $guardName): self;
|
||||
|
||||
/**
|
||||
* Find a role by its id and guard name.
|
||||
*
|
||||
* @param int $id
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @return \Spatie\Permission\Contracts\Role
|
||||
*
|
||||
* @throws \Spatie\Permission\Exceptions\RoleDoesNotExist
|
||||
*/
|
||||
public static function findById(int $id, $guardName): self;
|
||||
|
||||
/**
|
||||
* Find or create a role by its name and guard name.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @return \Spatie\Permission\Contracts\Role
|
||||
*/
|
||||
public static function findOrCreate(string $name, $guardName): self;
|
||||
|
||||
/**
|
||||
* Determine if the user may perform the given permission.
|
||||
*
|
||||
* @param string|\Spatie\Permission\Contracts\Permission $permission
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPermissionTo($permission): bool;
|
||||
}
|
||||
14
vendor/spatie/laravel-permission/src/Exceptions/GuardDoesNotMatch.php
vendored
Normal file
14
vendor/spatie/laravel-permission/src/Exceptions/GuardDoesNotMatch.php
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Exceptions;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class GuardDoesNotMatch extends InvalidArgumentException
|
||||
{
|
||||
public static function create(string $givenGuard, Collection $expectedGuards)
|
||||
{
|
||||
return new static("The given role or permission should use guard `{$expectedGuards->implode(', ')}` instead of `{$givenGuard}`.");
|
||||
}
|
||||
}
|
||||
13
vendor/spatie/laravel-permission/src/Exceptions/PermissionAlreadyExists.php
vendored
Normal file
13
vendor/spatie/laravel-permission/src/Exceptions/PermissionAlreadyExists.php
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Exceptions;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class PermissionAlreadyExists extends InvalidArgumentException
|
||||
{
|
||||
public static function create(string $permissionName, string $guardName)
|
||||
{
|
||||
return new static("A `{$permissionName}` permission already exists for guard `{$guardName}`.");
|
||||
}
|
||||
}
|
||||
18
vendor/spatie/laravel-permission/src/Exceptions/PermissionDoesNotExist.php
vendored
Normal file
18
vendor/spatie/laravel-permission/src/Exceptions/PermissionDoesNotExist.php
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Exceptions;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class PermissionDoesNotExist extends InvalidArgumentException
|
||||
{
|
||||
public static function create(string $permissionName, string $guardName = '')
|
||||
{
|
||||
return new static("There is no permission named `{$permissionName}` for guard `{$guardName}`.");
|
||||
}
|
||||
|
||||
public static function withId(int $permissionId, string $guardName = '')
|
||||
{
|
||||
return new static("There is no [permission] with id `{$permissionId}` for guard `{$guardName}`.");
|
||||
}
|
||||
}
|
||||
13
vendor/spatie/laravel-permission/src/Exceptions/RoleAlreadyExists.php
vendored
Normal file
13
vendor/spatie/laravel-permission/src/Exceptions/RoleAlreadyExists.php
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Exceptions;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class RoleAlreadyExists extends InvalidArgumentException
|
||||
{
|
||||
public static function create(string $roleName, string $guardName)
|
||||
{
|
||||
return new static("A role `{$roleName}` already exists for guard `{$guardName}`.");
|
||||
}
|
||||
}
|
||||
18
vendor/spatie/laravel-permission/src/Exceptions/RoleDoesNotExist.php
vendored
Normal file
18
vendor/spatie/laravel-permission/src/Exceptions/RoleDoesNotExist.php
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Exceptions;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class RoleDoesNotExist extends InvalidArgumentException
|
||||
{
|
||||
public static function named(string $roleName)
|
||||
{
|
||||
return new static("There is no role named `{$roleName}`.");
|
||||
}
|
||||
|
||||
public static function withId(int $roleId)
|
||||
{
|
||||
return new static("There is no role with id `{$roleId}`.");
|
||||
}
|
||||
}
|
||||
72
vendor/spatie/laravel-permission/src/Exceptions/UnauthorizedException.php
vendored
Normal file
72
vendor/spatie/laravel-permission/src/Exceptions/UnauthorizedException.php
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Exceptions;
|
||||
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class UnauthorizedException extends HttpException
|
||||
{
|
||||
private $requiredRoles = [];
|
||||
|
||||
private $requiredPermissions = [];
|
||||
|
||||
public static function forRoles(array $roles): self
|
||||
{
|
||||
$message = 'User does not have the right roles.';
|
||||
|
||||
if (config('permission.display_permission_in_exception')) {
|
||||
$permStr = implode(', ', $roles);
|
||||
$message = 'User does not have the right roles. Necessary roles are '.$permStr;
|
||||
}
|
||||
|
||||
$exception = new static(403, $message, null, []);
|
||||
$exception->requiredRoles = $roles;
|
||||
|
||||
return $exception;
|
||||
}
|
||||
|
||||
public static function forPermissions(array $permissions): self
|
||||
{
|
||||
$message = 'User does not have the right permissions.';
|
||||
|
||||
if (config('permission.display_permission_in_exception')) {
|
||||
$permStr = implode(', ', $permissions);
|
||||
$message = 'User does not have the right permissions. Necessary permissions are '.$permStr;
|
||||
}
|
||||
|
||||
$exception = new static(403, $message, null, []);
|
||||
$exception->requiredPermissions = $permissions;
|
||||
|
||||
return $exception;
|
||||
}
|
||||
|
||||
public static function forRolesOrPermissions(array $rolesOrPermissions): self
|
||||
{
|
||||
$message = 'User does not have any of the necessary access rights.';
|
||||
|
||||
if (config('permission.display_permission_in_exception') && config('permission.display_role_in_exception')) {
|
||||
$permStr = implode(', ', $rolesOrPermissions);
|
||||
$message = 'User does not have the right permissions. Necessary permissions are '.$permStr;
|
||||
}
|
||||
|
||||
$exception = new static(403, $message, null, []);
|
||||
$exception->requiredPermissions = $rolesOrPermissions;
|
||||
|
||||
return $exception;
|
||||
}
|
||||
|
||||
public static function notLoggedIn(): self
|
||||
{
|
||||
return new static(403, 'User is not logged in.', null, []);
|
||||
}
|
||||
|
||||
public function getRequiredRoles(): array
|
||||
{
|
||||
return $this->requiredRoles;
|
||||
}
|
||||
|
||||
public function getRequiredPermissions(): array
|
||||
{
|
||||
return $this->requiredPermissions;
|
||||
}
|
||||
}
|
||||
13
vendor/spatie/laravel-permission/src/Exceptions/WildcardPermissionInvalidArgument.php
vendored
Normal file
13
vendor/spatie/laravel-permission/src/Exceptions/WildcardPermissionInvalidArgument.php
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Exceptions;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class WildcardPermissionInvalidArgument extends InvalidArgumentException
|
||||
{
|
||||
public static function create()
|
||||
{
|
||||
return new static('Wildcard permission must be string, permission id or permission instance');
|
||||
}
|
||||
}
|
||||
13
vendor/spatie/laravel-permission/src/Exceptions/WildcardPermissionNotProperlyFormatted.php
vendored
Normal file
13
vendor/spatie/laravel-permission/src/Exceptions/WildcardPermissionNotProperlyFormatted.php
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Exceptions;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class WildcardPermissionNotProperlyFormatted extends InvalidArgumentException
|
||||
{
|
||||
public static function create(string $permission)
|
||||
{
|
||||
return new static("Wildcard permission `{$permission}` is not properly formatted.");
|
||||
}
|
||||
}
|
||||
55
vendor/spatie/laravel-permission/src/Guard.php
vendored
Normal file
55
vendor/spatie/laravel-permission/src/Guard.php
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class Guard
|
||||
{
|
||||
/**
|
||||
* return collection of (guard_name) property if exist on class or object
|
||||
* otherwise will return collection of guards names that exists in config/auth.php.
|
||||
* @param $model
|
||||
* @return Collection
|
||||
*/
|
||||
public static function getNames($model): Collection
|
||||
{
|
||||
if (is_object($model)) {
|
||||
if (\method_exists($model, 'guardName')) {
|
||||
$guardName = $model->guardName();
|
||||
} else {
|
||||
$guardName = $model->guard_name ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
if (! isset($guardName)) {
|
||||
$class = is_object($model) ? get_class($model) : $model;
|
||||
|
||||
$guardName = (new \ReflectionClass($class))->getDefaultProperties()['guard_name'] ?? null;
|
||||
}
|
||||
|
||||
if ($guardName) {
|
||||
return collect($guardName);
|
||||
}
|
||||
|
||||
return collect(config('auth.guards'))
|
||||
->map(function ($guard) {
|
||||
if (! isset($guard['provider'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
return config("auth.providers.{$guard['provider']}.model");
|
||||
})
|
||||
->filter(function ($model) use ($class) {
|
||||
return $class === $model;
|
||||
})
|
||||
->keys();
|
||||
}
|
||||
|
||||
public static function getDefaultName($class): string
|
||||
{
|
||||
$default = config('auth.defaults.guard');
|
||||
|
||||
return static::getNames($class)->first() ?: $default;
|
||||
}
|
||||
}
|
||||
28
vendor/spatie/laravel-permission/src/Middlewares/PermissionMiddleware.php
vendored
Normal file
28
vendor/spatie/laravel-permission/src/Middlewares/PermissionMiddleware.php
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Middlewares;
|
||||
|
||||
use Closure;
|
||||
use Spatie\Permission\Exceptions\UnauthorizedException;
|
||||
|
||||
class PermissionMiddleware
|
||||
{
|
||||
public function handle($request, Closure $next, $permission)
|
||||
{
|
||||
if (app('auth')->guest()) {
|
||||
throw UnauthorizedException::notLoggedIn();
|
||||
}
|
||||
|
||||
$permissions = is_array($permission)
|
||||
? $permission
|
||||
: explode('|', $permission);
|
||||
|
||||
foreach ($permissions as $permission) {
|
||||
if (app('auth')->user()->can($permission)) {
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
||||
throw UnauthorizedException::forPermissions($permissions);
|
||||
}
|
||||
}
|
||||
27
vendor/spatie/laravel-permission/src/Middlewares/RoleMiddleware.php
vendored
Normal file
27
vendor/spatie/laravel-permission/src/Middlewares/RoleMiddleware.php
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Middlewares;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Spatie\Permission\Exceptions\UnauthorizedException;
|
||||
|
||||
class RoleMiddleware
|
||||
{
|
||||
public function handle($request, Closure $next, $role)
|
||||
{
|
||||
if (Auth::guest()) {
|
||||
throw UnauthorizedException::notLoggedIn();
|
||||
}
|
||||
|
||||
$roles = is_array($role)
|
||||
? $role
|
||||
: explode('|', $role);
|
||||
|
||||
if (! Auth::user()->hasAnyRole($roles)) {
|
||||
throw UnauthorizedException::forRoles($roles);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
27
vendor/spatie/laravel-permission/src/Middlewares/RoleOrPermissionMiddleware.php
vendored
Normal file
27
vendor/spatie/laravel-permission/src/Middlewares/RoleOrPermissionMiddleware.php
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Middlewares;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Spatie\Permission\Exceptions\UnauthorizedException;
|
||||
|
||||
class RoleOrPermissionMiddleware
|
||||
{
|
||||
public function handle($request, Closure $next, $roleOrPermission)
|
||||
{
|
||||
if (Auth::guest()) {
|
||||
throw UnauthorizedException::notLoggedIn();
|
||||
}
|
||||
|
||||
$rolesOrPermissions = is_array($roleOrPermission)
|
||||
? $roleOrPermission
|
||||
: explode('|', $roleOrPermission);
|
||||
|
||||
if (! Auth::user()->hasAnyRole($rolesOrPermissions) && ! Auth::user()->hasAnyPermission($rolesOrPermissions)) {
|
||||
throw UnauthorizedException::forRolesOrPermissions($rolesOrPermissions);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
145
vendor/spatie/laravel-permission/src/Models/Permission.php
vendored
Normal file
145
vendor/spatie/laravel-permission/src/Models/Permission.php
vendored
Normal file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Models;
|
||||
|
||||
use Spatie\Permission\Guard;
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\Permission\Traits\HasRoles;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Spatie\Permission\PermissionRegistrar;
|
||||
use Spatie\Permission\Traits\RefreshesPermissionCache;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Spatie\Permission\Exceptions\PermissionAlreadyExists;
|
||||
use Spatie\Permission\Contracts\Permission as PermissionContract;
|
||||
|
||||
class Permission extends Model implements PermissionContract
|
||||
{
|
||||
use HasRoles;
|
||||
use RefreshesPermissionCache;
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
public function __construct(array $attributes = [])
|
||||
{
|
||||
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
|
||||
|
||||
parent::__construct($attributes);
|
||||
|
||||
$this->setTable(config('permission.table_names.permissions'));
|
||||
}
|
||||
|
||||
public static function create(array $attributes = [])
|
||||
{
|
||||
$attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
|
||||
|
||||
$permission = static::getPermissions(['name' => $attributes['name'], 'guard_name' => $attributes['guard_name']])->first();
|
||||
|
||||
if ($permission) {
|
||||
throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']);
|
||||
}
|
||||
|
||||
return static::query()->create($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* A permission can be applied to roles.
|
||||
*/
|
||||
public function roles(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
config('permission.models.role'),
|
||||
config('permission.table_names.role_has_permissions'),
|
||||
'permission_id',
|
||||
'role_id'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A permission belongs to some users of the model associated with its guard.
|
||||
*/
|
||||
public function users(): MorphToMany
|
||||
{
|
||||
return $this->morphedByMany(
|
||||
getModelForGuard($this->attributes['guard_name']),
|
||||
'model',
|
||||
config('permission.table_names.model_has_permissions'),
|
||||
'permission_id',
|
||||
config('permission.column_names.model_morph_key')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a permission by its name (and optionally guardName).
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist
|
||||
*
|
||||
* @return \Spatie\Permission\Contracts\Permission
|
||||
*/
|
||||
public static function findByName(string $name, $guardName = null): PermissionContract
|
||||
{
|
||||
$guardName = $guardName ?? Guard::getDefaultName(static::class);
|
||||
$permission = static::getPermissions(['name' => $name, 'guard_name' => $guardName])->first();
|
||||
if (! $permission) {
|
||||
throw PermissionDoesNotExist::create($name, $guardName);
|
||||
}
|
||||
|
||||
return $permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a permission by its id (and optionally guardName).
|
||||
*
|
||||
* @param int $id
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist
|
||||
*
|
||||
* @return \Spatie\Permission\Contracts\Permission
|
||||
*/
|
||||
public static function findById(int $id, $guardName = null): PermissionContract
|
||||
{
|
||||
$guardName = $guardName ?? Guard::getDefaultName(static::class);
|
||||
$permission = static::getPermissions(['id' => $id, 'guard_name' => $guardName])->first();
|
||||
|
||||
if (! $permission) {
|
||||
throw PermissionDoesNotExist::withId($id, $guardName);
|
||||
}
|
||||
|
||||
return $permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find or create permission by its name (and optionally guardName).
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @return \Spatie\Permission\Contracts\Permission
|
||||
*/
|
||||
public static function findOrCreate(string $name, $guardName = null): PermissionContract
|
||||
{
|
||||
$guardName = $guardName ?? Guard::getDefaultName(static::class);
|
||||
$permission = static::getPermissions(['name' => $name, 'guard_name' => $guardName])->first();
|
||||
|
||||
if (! $permission) {
|
||||
return static::query()->create(['name' => $name, 'guard_name' => $guardName]);
|
||||
}
|
||||
|
||||
return $permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current cached permissions.
|
||||
*/
|
||||
protected static function getPermissions(array $params = []): Collection
|
||||
{
|
||||
return app(PermissionRegistrar::class)
|
||||
->setPermissionClass(static::class)
|
||||
->getPermissions($params);
|
||||
}
|
||||
}
|
||||
158
vendor/spatie/laravel-permission/src/Models/Role.php
vendored
Normal file
158
vendor/spatie/laravel-permission/src/Models/Role.php
vendored
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Models;
|
||||
|
||||
use Spatie\Permission\Guard;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Spatie\Permission\Traits\HasPermissions;
|
||||
use Spatie\Permission\Exceptions\RoleDoesNotExist;
|
||||
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
|
||||
use Spatie\Permission\Exceptions\RoleAlreadyExists;
|
||||
use Spatie\Permission\Contracts\Role as RoleContract;
|
||||
use Spatie\Permission\Traits\RefreshesPermissionCache;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Role extends Model implements RoleContract
|
||||
{
|
||||
use HasPermissions;
|
||||
use RefreshesPermissionCache;
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
public function __construct(array $attributes = [])
|
||||
{
|
||||
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
|
||||
|
||||
parent::__construct($attributes);
|
||||
|
||||
$this->setTable(config('permission.table_names.roles'));
|
||||
}
|
||||
|
||||
public static function create(array $attributes = [])
|
||||
{
|
||||
$attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
|
||||
|
||||
if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
|
||||
throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']);
|
||||
}
|
||||
|
||||
return static::query()->create($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* A role may be given various permissions.
|
||||
*/
|
||||
public function permissions(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
config('permission.models.permission'),
|
||||
config('permission.table_names.role_has_permissions'),
|
||||
'role_id',
|
||||
'permission_id'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A role belongs to some users of the model associated with its guard.
|
||||
*/
|
||||
public function users(): MorphToMany
|
||||
{
|
||||
return $this->morphedByMany(
|
||||
getModelForGuard($this->attributes['guard_name']),
|
||||
'model',
|
||||
config('permission.table_names.model_has_roles'),
|
||||
'role_id',
|
||||
config('permission.column_names.model_morph_key')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a role by its name and guard name.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @return \Spatie\Permission\Contracts\Role|\Spatie\Permission\Models\Role
|
||||
*
|
||||
* @throws \Spatie\Permission\Exceptions\RoleDoesNotExist
|
||||
*/
|
||||
public static function findByName(string $name, $guardName = null): RoleContract
|
||||
{
|
||||
$guardName = $guardName ?? Guard::getDefaultName(static::class);
|
||||
|
||||
$role = static::where('name', $name)->where('guard_name', $guardName)->first();
|
||||
|
||||
if (! $role) {
|
||||
throw RoleDoesNotExist::named($name);
|
||||
}
|
||||
|
||||
return $role;
|
||||
}
|
||||
|
||||
public static function findById(int $id, $guardName = null): RoleContract
|
||||
{
|
||||
$guardName = $guardName ?? Guard::getDefaultName(static::class);
|
||||
|
||||
$role = static::where('id', $id)->where('guard_name', $guardName)->first();
|
||||
|
||||
if (! $role) {
|
||||
throw RoleDoesNotExist::withId($id);
|
||||
}
|
||||
|
||||
return $role;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find or create role by its name (and optionally guardName).
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @return \Spatie\Permission\Contracts\Role
|
||||
*/
|
||||
public static function findOrCreate(string $name, $guardName = null): RoleContract
|
||||
{
|
||||
$guardName = $guardName ?? Guard::getDefaultName(static::class);
|
||||
|
||||
$role = static::where('name', $name)->where('guard_name', $guardName)->first();
|
||||
|
||||
if (! $role) {
|
||||
return static::query()->create(['name' => $name, 'guard_name' => $guardName]);
|
||||
}
|
||||
|
||||
return $role;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user may perform the given permission.
|
||||
*
|
||||
* @param string|Permission $permission
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch
|
||||
*/
|
||||
public function hasPermissionTo($permission): bool
|
||||
{
|
||||
if (config('permission.enable_wildcard_permission', false)) {
|
||||
return $this->hasWildcardPermission($permission, $this->getDefaultGuardName());
|
||||
}
|
||||
|
||||
$permissionClass = $this->getPermissionClass();
|
||||
|
||||
if (is_string($permission)) {
|
||||
$permission = $permissionClass->findByName($permission, $this->getDefaultGuardName());
|
||||
}
|
||||
|
||||
if (is_int($permission)) {
|
||||
$permission = $permissionClass->findById($permission, $this->getDefaultGuardName());
|
||||
}
|
||||
|
||||
if (! $this->getGuardNames()->contains($permission->guard_name)) {
|
||||
throw GuardDoesNotMatch::create($permission->guard_name, $this->getGuardNames());
|
||||
}
|
||||
|
||||
return $this->permissions->contains('id', $permission->id);
|
||||
}
|
||||
}
|
||||
179
vendor/spatie/laravel-permission/src/PermissionRegistrar.php
vendored
Normal file
179
vendor/spatie/laravel-permission/src/PermissionRegistrar.php
vendored
Normal file
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission;
|
||||
|
||||
use Illuminate\Cache\CacheManager;
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\Permission\Contracts\Role;
|
||||
use Illuminate\Contracts\Auth\Access\Gate;
|
||||
use Spatie\Permission\Contracts\Permission;
|
||||
use Illuminate\Contracts\Auth\Access\Authorizable;
|
||||
|
||||
class PermissionRegistrar
|
||||
{
|
||||
/** @var \Illuminate\Contracts\Cache\Repository */
|
||||
protected $cache;
|
||||
|
||||
/** @var \Illuminate\Cache\CacheManager */
|
||||
protected $cacheManager;
|
||||
|
||||
/** @var string */
|
||||
protected $permissionClass;
|
||||
|
||||
/** @var string */
|
||||
protected $roleClass;
|
||||
|
||||
/** @var \Illuminate\Support\Collection */
|
||||
protected $permissions;
|
||||
|
||||
/** @var \DateInterval|int */
|
||||
public static $cacheExpirationTime;
|
||||
|
||||
/** @var string */
|
||||
public static $cacheKey;
|
||||
|
||||
/** @var string */
|
||||
public static $cacheModelKey;
|
||||
|
||||
/**
|
||||
* PermissionRegistrar constructor.
|
||||
*
|
||||
* @param \Illuminate\Cache\CacheManager $cacheManager
|
||||
*/
|
||||
public function __construct(CacheManager $cacheManager)
|
||||
{
|
||||
$this->permissionClass = config('permission.models.permission');
|
||||
$this->roleClass = config('permission.models.role');
|
||||
|
||||
$this->cacheManager = $cacheManager;
|
||||
$this->initializeCache();
|
||||
}
|
||||
|
||||
protected function initializeCache()
|
||||
{
|
||||
self::$cacheExpirationTime = config('permission.cache.expiration_time', config('permission.cache_expiration_time'));
|
||||
|
||||
self::$cacheKey = config('permission.cache.key');
|
||||
self::$cacheModelKey = config('permission.cache.model_key');
|
||||
|
||||
$this->cache = $this->getCacheStoreFromConfig();
|
||||
}
|
||||
|
||||
protected function getCacheStoreFromConfig(): \Illuminate\Contracts\Cache\Repository
|
||||
{
|
||||
// the 'default' fallback here is from the permission.php config file, where 'default' means to use config(cache.default)
|
||||
$cacheDriver = config('permission.cache.store', 'default');
|
||||
|
||||
// when 'default' is specified, no action is required since we already have the default instance
|
||||
if ($cacheDriver === 'default') {
|
||||
return $this->cacheManager->store();
|
||||
}
|
||||
|
||||
// if an undefined cache store is specified, fallback to 'array' which is Laravel's closest equiv to 'none'
|
||||
if (! \array_key_exists($cacheDriver, config('cache.stores'))) {
|
||||
$cacheDriver = 'array';
|
||||
}
|
||||
|
||||
return $this->cacheManager->store($cacheDriver);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the permission check method on the gate.
|
||||
* We resolve the Gate fresh here, for benefit of long-running instances.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function registerPermissions(): bool
|
||||
{
|
||||
app(Gate::class)->before(function (Authorizable $user, string $ability) {
|
||||
if (method_exists($user, 'checkPermissionTo')) {
|
||||
return $user->checkPermissionTo($ability) ?: null;
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush the cache.
|
||||
*/
|
||||
public function forgetCachedPermissions()
|
||||
{
|
||||
$this->permissions = null;
|
||||
|
||||
return $this->cache->forget(self::$cacheKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear class permissions.
|
||||
* This is only intended to be called by the PermissionServiceProvider on boot,
|
||||
* so that long-running instances like Swoole don't keep old data in memory.
|
||||
*/
|
||||
public function clearClassPermissions()
|
||||
{
|
||||
$this->permissions = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the permissions based on the passed params.
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function getPermissions(array $params = []): Collection
|
||||
{
|
||||
if ($this->permissions === null) {
|
||||
$this->permissions = $this->cache->remember(self::$cacheKey, self::$cacheExpirationTime, function () {
|
||||
return $this->getPermissionClass()
|
||||
->with('roles')
|
||||
->get();
|
||||
});
|
||||
}
|
||||
|
||||
$permissions = clone $this->permissions;
|
||||
|
||||
foreach ($params as $attr => $value) {
|
||||
$permissions = $permissions->where($attr, $value);
|
||||
}
|
||||
|
||||
return $permissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an instance of the permission class.
|
||||
*
|
||||
* @return \Spatie\Permission\Contracts\Permission
|
||||
*/
|
||||
public function getPermissionClass(): Permission
|
||||
{
|
||||
return app($this->permissionClass);
|
||||
}
|
||||
|
||||
public function setPermissionClass($permissionClass)
|
||||
{
|
||||
$this->permissionClass = $permissionClass;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an instance of the role class.
|
||||
*
|
||||
* @return \Spatie\Permission\Contracts\Role
|
||||
*/
|
||||
public function getRoleClass(): Role
|
||||
{
|
||||
return app($this->roleClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance of the Cache Store.
|
||||
*
|
||||
* @return \Illuminate\Contracts\Cache\Store
|
||||
*/
|
||||
public function getCacheStore(): \Illuminate\Contracts\Cache\Store
|
||||
{
|
||||
return $this->cache->getStore();
|
||||
}
|
||||
}
|
||||
170
vendor/spatie/laravel-permission/src/PermissionServiceProvider.php
vendored
Normal file
170
vendor/spatie/laravel-permission/src/PermissionServiceProvider.php
vendored
Normal file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission;
|
||||
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\View\Compilers\BladeCompiler;
|
||||
use Spatie\Permission\Contracts\Role as RoleContract;
|
||||
use Spatie\Permission\Contracts\Permission as PermissionContract;
|
||||
|
||||
class PermissionServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(PermissionRegistrar $permissionLoader, Filesystem $filesystem)
|
||||
{
|
||||
if (function_exists('config_path')) { // function not available and 'publish' not relevant in Lumen
|
||||
$this->publishes([
|
||||
__DIR__.'/../config/permission.php' => config_path('permission.php'),
|
||||
], 'config');
|
||||
|
||||
$this->publishes([
|
||||
__DIR__.'/../database/migrations/create_permission_tables.php.stub' => $this->getMigrationFileName($filesystem),
|
||||
], 'migrations');
|
||||
}
|
||||
|
||||
$this->registerMacroHelpers();
|
||||
|
||||
$this->commands([
|
||||
Commands\CacheReset::class,
|
||||
Commands\CreateRole::class,
|
||||
Commands\CreatePermission::class,
|
||||
Commands\Show::class,
|
||||
]);
|
||||
|
||||
$this->registerModelBindings();
|
||||
|
||||
$permissionLoader->clearClassPermissions();
|
||||
$permissionLoader->registerPermissions();
|
||||
|
||||
$this->app->singleton(PermissionRegistrar::class, function ($app) use ($permissionLoader) {
|
||||
return $permissionLoader;
|
||||
});
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
$this->mergeConfigFrom(
|
||||
__DIR__.'/../config/permission.php',
|
||||
'permission'
|
||||
);
|
||||
|
||||
$this->registerBladeExtensions();
|
||||
}
|
||||
|
||||
protected function registerModelBindings()
|
||||
{
|
||||
$config = $this->app->config['permission.models'];
|
||||
|
||||
if (! $config) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->app->bind(PermissionContract::class, $config['permission']);
|
||||
$this->app->bind(RoleContract::class, $config['role']);
|
||||
}
|
||||
|
||||
protected function registerBladeExtensions()
|
||||
{
|
||||
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
|
||||
$bladeCompiler->directive('role', function ($arguments) {
|
||||
list($role, $guard) = explode(',', $arguments.',');
|
||||
|
||||
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
|
||||
});
|
||||
$bladeCompiler->directive('elserole', function ($arguments) {
|
||||
list($role, $guard) = explode(',', $arguments.',');
|
||||
|
||||
return "<?php elseif(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
|
||||
});
|
||||
$bladeCompiler->directive('endrole', function () {
|
||||
return '<?php endif; ?>';
|
||||
});
|
||||
|
||||
$bladeCompiler->directive('hasrole', function ($arguments) {
|
||||
list($role, $guard) = explode(',', $arguments.',');
|
||||
|
||||
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
|
||||
});
|
||||
$bladeCompiler->directive('endhasrole', function () {
|
||||
return '<?php endif; ?>';
|
||||
});
|
||||
|
||||
$bladeCompiler->directive('hasanyrole', function ($arguments) {
|
||||
list($roles, $guard) = explode(',', $arguments.',');
|
||||
|
||||
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
|
||||
});
|
||||
$bladeCompiler->directive('endhasanyrole', function () {
|
||||
return '<?php endif; ?>';
|
||||
});
|
||||
|
||||
$bladeCompiler->directive('hasallroles', function ($arguments) {
|
||||
list($roles, $guard) = explode(',', $arguments.',');
|
||||
|
||||
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
|
||||
});
|
||||
$bladeCompiler->directive('endhasallroles', function () {
|
||||
return '<?php endif; ?>';
|
||||
});
|
||||
|
||||
$bladeCompiler->directive('unlessrole', function ($arguments) {
|
||||
list($role, $guard) = explode(',', $arguments.',');
|
||||
|
||||
return "<?php if(!auth({$guard})->check() || ! auth({$guard})->user()->hasRole({$role})): ?>";
|
||||
});
|
||||
$bladeCompiler->directive('endunlessrole', function () {
|
||||
return '<?php endif; ?>';
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected function registerMacroHelpers()
|
||||
{
|
||||
if (! method_exists(Route::class, 'macro')) { // Lumen
|
||||
return;
|
||||
}
|
||||
|
||||
Route::macro('role', function ($roles = []) {
|
||||
if (! is_array($roles)) {
|
||||
$roles = [$roles];
|
||||
}
|
||||
|
||||
$roles = implode('|', $roles);
|
||||
|
||||
$this->middleware("role:$roles");
|
||||
|
||||
return $this;
|
||||
});
|
||||
|
||||
Route::macro('permission', function ($permissions = []) {
|
||||
if (! is_array($permissions)) {
|
||||
$permissions = [$permissions];
|
||||
}
|
||||
|
||||
$permissions = implode('|', $permissions);
|
||||
|
||||
$this->middleware("permission:$permissions");
|
||||
|
||||
return $this;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns existing migration file if found, else uses the current timestamp.
|
||||
*
|
||||
* @param Filesystem $filesystem
|
||||
* @return string
|
||||
*/
|
||||
protected function getMigrationFileName(Filesystem $filesystem): string
|
||||
{
|
||||
$timestamp = date('Y_m_d_His');
|
||||
|
||||
return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR)
|
||||
->flatMap(function ($path) use ($filesystem) {
|
||||
return $filesystem->glob($path.'*_create_permission_tables.php');
|
||||
})->push($this->app->databasePath()."/migrations/{$timestamp}_create_permission_tables.php")
|
||||
->first();
|
||||
}
|
||||
}
|
||||
496
vendor/spatie/laravel-permission/src/Traits/HasPermissions.php
vendored
Normal file
496
vendor/spatie/laravel-permission/src/Traits/HasPermissions.php
vendored
Normal file
@@ -0,0 +1,496 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Traits;
|
||||
|
||||
use Spatie\Permission\Guard;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Spatie\Permission\WildcardPermission;
|
||||
use Spatie\Permission\PermissionRegistrar;
|
||||
use Spatie\Permission\Contracts\Permission;
|
||||
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
|
||||
use Spatie\Permission\Exceptions\WildcardPermissionInvalidArgument;
|
||||
|
||||
trait HasPermissions
|
||||
{
|
||||
private $permissionClass;
|
||||
|
||||
public static function bootHasPermissions()
|
||||
{
|
||||
static::deleting(function ($model) {
|
||||
if (method_exists($model, 'isForceDeleting') && ! $model->isForceDeleting()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$model->permissions()->detach();
|
||||
});
|
||||
}
|
||||
|
||||
public function getPermissionClass()
|
||||
{
|
||||
if (! isset($this->permissionClass)) {
|
||||
$this->permissionClass = app(PermissionRegistrar::class)->getPermissionClass();
|
||||
}
|
||||
|
||||
return $this->permissionClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* A model may have multiple direct permissions.
|
||||
*/
|
||||
public function permissions(): MorphToMany
|
||||
{
|
||||
return $this->morphToMany(
|
||||
config('permission.models.permission'),
|
||||
'model',
|
||||
config('permission.table_names.model_has_permissions'),
|
||||
config('permission.column_names.model_morph_key'),
|
||||
'permission_id'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope the model query to certain permissions only.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopePermission(Builder $query, $permissions): Builder
|
||||
{
|
||||
$permissions = $this->convertToPermissionModels($permissions);
|
||||
|
||||
$rolesWithPermissions = array_unique(array_reduce($permissions, function ($result, $permission) {
|
||||
return array_merge($result, $permission->roles->all());
|
||||
}, []));
|
||||
|
||||
return $query->where(function (Builder $query) use ($permissions, $rolesWithPermissions) {
|
||||
$query->whereHas('permissions', function (Builder $subQuery) use ($permissions) {
|
||||
$subQuery->whereIn(config('permission.table_names.permissions').'.id', \array_column($permissions, 'id'));
|
||||
});
|
||||
if (count($rolesWithPermissions) > 0) {
|
||||
$query->orWhereHas('roles', function (Builder $subQuery) use ($rolesWithPermissions) {
|
||||
$subQuery->whereIn(config('permission.table_names.roles').'.id', \array_column($rolesWithPermissions, 'id'));
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function convertToPermissionModels($permissions): array
|
||||
{
|
||||
if ($permissions instanceof Collection) {
|
||||
$permissions = $permissions->all();
|
||||
}
|
||||
|
||||
$permissions = is_array($permissions) ? $permissions : [$permissions];
|
||||
|
||||
return array_map(function ($permission) {
|
||||
if ($permission instanceof Permission) {
|
||||
return $permission;
|
||||
}
|
||||
|
||||
return $this->getPermissionClass()->findByName($permission, $this->getDefaultGuardName());
|
||||
}, $permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the model may perform the given permission.
|
||||
*
|
||||
* @param string|int|\Spatie\Permission\Contracts\Permission $permission
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @return bool
|
||||
* @throws PermissionDoesNotExist
|
||||
*/
|
||||
public function hasPermissionTo($permission, $guardName = null): bool
|
||||
{
|
||||
if (config('permission.enable_wildcard_permission', false)) {
|
||||
return $this->hasWildcardPermission($permission, $guardName);
|
||||
}
|
||||
|
||||
$permissionClass = $this->getPermissionClass();
|
||||
|
||||
if (is_string($permission)) {
|
||||
$permission = $permissionClass->findByName(
|
||||
$permission,
|
||||
$guardName ?? $this->getDefaultGuardName()
|
||||
);
|
||||
}
|
||||
|
||||
if (is_int($permission)) {
|
||||
$permission = $permissionClass->findById(
|
||||
$permission,
|
||||
$guardName ?? $this->getDefaultGuardName()
|
||||
);
|
||||
}
|
||||
|
||||
if (! $permission instanceof Permission) {
|
||||
throw new PermissionDoesNotExist;
|
||||
}
|
||||
|
||||
return $this->hasDirectPermission($permission) || $this->hasPermissionViaRole($permission);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a wildcard permission against all permissions of a user.
|
||||
*
|
||||
* @param string|int|\Spatie\Permission\Contracts\Permission $permission
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function hasWildcardPermission($permission, $guardName = null): bool
|
||||
{
|
||||
$guardName = $guardName ?? $this->getDefaultGuardName();
|
||||
|
||||
if (is_int($permission)) {
|
||||
$permission = $this->getPermissionClass()->findById($permission, $guardName);
|
||||
}
|
||||
|
||||
if ($permission instanceof Permission) {
|
||||
$permission = $permission->name;
|
||||
}
|
||||
|
||||
if (! is_string($permission)) {
|
||||
throw WildcardPermissionInvalidArgument::create();
|
||||
}
|
||||
|
||||
foreach ($this->getAllPermissions() as $userPermission) {
|
||||
if ($guardName !== $userPermission->guard_name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$userPermission = new WildcardPermission($userPermission->name);
|
||||
|
||||
if ($userPermission->implies($permission)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since 2.35.0
|
||||
* @alias of hasPermissionTo()
|
||||
*/
|
||||
public function hasUncachedPermissionTo($permission, $guardName = null): bool
|
||||
{
|
||||
return $this->hasPermissionTo($permission, $guardName);
|
||||
}
|
||||
|
||||
/**
|
||||
* An alias to hasPermissionTo(), but avoids throwing an exception.
|
||||
*
|
||||
* @param string|int|\Spatie\Permission\Contracts\Permission $permission
|
||||
* @param string|null $guardName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function checkPermissionTo($permission, $guardName = null): bool
|
||||
{
|
||||
try {
|
||||
return $this->hasPermissionTo($permission, $guardName);
|
||||
} catch (PermissionDoesNotExist $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the model has any of the given permissions.
|
||||
*
|
||||
* @param array ...$permissions
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function hasAnyPermission(...$permissions): bool
|
||||
{
|
||||
$permissions = collect($permissions)->flatten();
|
||||
|
||||
foreach ($permissions as $permission) {
|
||||
if ($this->checkPermissionTo($permission)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the model has all of the given permissions.
|
||||
*
|
||||
* @param array ...$permissions
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function hasAllPermissions(...$permissions): bool
|
||||
{
|
||||
$permissions = collect($permissions)->flatten();
|
||||
|
||||
foreach ($permissions as $permission) {
|
||||
if (! $this->hasPermissionTo($permission)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the model has, via roles, the given permission.
|
||||
*
|
||||
* @param \Spatie\Permission\Contracts\Permission $permission
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function hasPermissionViaRole(Permission $permission): bool
|
||||
{
|
||||
return $this->hasRole($permission->roles);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the model has the given permission.
|
||||
*
|
||||
* @param string|int|\Spatie\Permission\Contracts\Permission $permission
|
||||
*
|
||||
* @return bool
|
||||
* @throws PermissionDoesNotExist
|
||||
*/
|
||||
public function hasDirectPermission($permission): bool
|
||||
{
|
||||
$permissionClass = $this->getPermissionClass();
|
||||
|
||||
if (is_string($permission)) {
|
||||
$permission = $permissionClass->findByName($permission, $this->getDefaultGuardName());
|
||||
}
|
||||
|
||||
if (is_int($permission)) {
|
||||
$permission = $permissionClass->findById($permission, $this->getDefaultGuardName());
|
||||
}
|
||||
|
||||
if (! $permission instanceof Permission) {
|
||||
throw new PermissionDoesNotExist;
|
||||
}
|
||||
|
||||
return $this->permissions->contains('id', $permission->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all the permissions the model has via roles.
|
||||
*/
|
||||
public function getPermissionsViaRoles(): Collection
|
||||
{
|
||||
return $this->loadMissing('roles', 'roles.permissions')
|
||||
->roles->flatMap(function ($role) {
|
||||
return $role->permissions;
|
||||
})->sort()->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all the permissions the model has, both directly and via roles.
|
||||
*/
|
||||
public function getAllPermissions(): Collection
|
||||
{
|
||||
/** @var Collection $permissions */
|
||||
$permissions = $this->permissions;
|
||||
|
||||
if ($this->roles) {
|
||||
$permissions = $permissions->merge($this->getPermissionsViaRoles());
|
||||
}
|
||||
|
||||
return $permissions->sort()->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Grant the given permission(s) to a role.
|
||||
*
|
||||
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function givePermissionTo(...$permissions)
|
||||
{
|
||||
$permissions = collect($permissions)
|
||||
->flatten()
|
||||
->map(function ($permission) {
|
||||
if (empty($permission)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->getStoredPermission($permission);
|
||||
})
|
||||
->filter(function ($permission) {
|
||||
return $permission instanceof Permission;
|
||||
})
|
||||
->each(function ($permission) {
|
||||
$this->ensureModelSharesGuard($permission);
|
||||
})
|
||||
->map->id
|
||||
->all();
|
||||
|
||||
$model = $this->getModel();
|
||||
|
||||
if ($model->exists) {
|
||||
$this->permissions()->sync($permissions, false);
|
||||
$model->load('permissions');
|
||||
} else {
|
||||
$class = \get_class($model);
|
||||
|
||||
$class::saved(
|
||||
function ($object) use ($permissions, $model) {
|
||||
static $modelLastFiredOn;
|
||||
if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) {
|
||||
return;
|
||||
}
|
||||
$object->permissions()->sync($permissions, false);
|
||||
$object->load('permissions');
|
||||
$modelLastFiredOn = $object;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
$this->forgetCachedPermissions();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all current permissions and set the given ones.
|
||||
*
|
||||
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function syncPermissions(...$permissions)
|
||||
{
|
||||
$this->permissions()->detach();
|
||||
|
||||
return $this->givePermissionTo($permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke the given permission.
|
||||
*
|
||||
* @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function revokePermissionTo($permission)
|
||||
{
|
||||
$this->permissions()->detach($this->getStoredPermission($permission));
|
||||
|
||||
$this->forgetCachedPermissions();
|
||||
|
||||
$this->load('permissions');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPermissionNames(): Collection
|
||||
{
|
||||
return $this->permissions->pluck('name');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
|
||||
*
|
||||
* @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection
|
||||
*/
|
||||
protected function getStoredPermission($permissions)
|
||||
{
|
||||
$permissionClass = $this->getPermissionClass();
|
||||
|
||||
if (is_numeric($permissions)) {
|
||||
return $permissionClass->findById($permissions, $this->getDefaultGuardName());
|
||||
}
|
||||
|
||||
if (is_string($permissions)) {
|
||||
return $permissionClass->findByName($permissions, $this->getDefaultGuardName());
|
||||
}
|
||||
|
||||
if (is_array($permissions)) {
|
||||
return $permissionClass
|
||||
->whereIn('name', $permissions)
|
||||
->whereIn('guard_name', $this->getGuardNames())
|
||||
->get();
|
||||
}
|
||||
|
||||
return $permissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission
|
||||
*
|
||||
* @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch
|
||||
*/
|
||||
protected function ensureModelSharesGuard($roleOrPermission)
|
||||
{
|
||||
if (! $this->getGuardNames()->contains($roleOrPermission->guard_name)) {
|
||||
throw GuardDoesNotMatch::create($roleOrPermission->guard_name, $this->getGuardNames());
|
||||
}
|
||||
}
|
||||
|
||||
protected function getGuardNames(): Collection
|
||||
{
|
||||
return Guard::getNames($this);
|
||||
}
|
||||
|
||||
protected function getDefaultGuardName(): string
|
||||
{
|
||||
return Guard::getDefaultName($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Forget the cached permissions.
|
||||
*/
|
||||
public function forgetCachedPermissions()
|
||||
{
|
||||
app(PermissionRegistrar::class)->forgetCachedPermissions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the model has All of the requested Direct permissions.
|
||||
* @param array ...$permissions
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAllDirectPermissions(...$permissions): bool
|
||||
{
|
||||
$permissions = collect($permissions)->flatten();
|
||||
|
||||
foreach ($permissions as $permission) {
|
||||
if (! $this->hasDirectPermission($permission)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the model has Any of the requested Direct permissions.
|
||||
* @param array ...$permissions
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAnyDirectPermission(...$permissions): bool
|
||||
{
|
||||
$permissions = collect($permissions)->flatten();
|
||||
|
||||
foreach ($permissions as $permission) {
|
||||
if ($this->hasDirectPermission($permission)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
306
vendor/spatie/laravel-permission/src/Traits/HasRoles.php
vendored
Normal file
306
vendor/spatie/laravel-permission/src/Traits/HasRoles.php
vendored
Normal file
@@ -0,0 +1,306 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Traits;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\Permission\Contracts\Role;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Spatie\Permission\PermissionRegistrar;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
|
||||
trait HasRoles
|
||||
{
|
||||
use HasPermissions;
|
||||
|
||||
private $roleClass;
|
||||
|
||||
public static function bootHasRoles()
|
||||
{
|
||||
static::deleting(function ($model) {
|
||||
if (method_exists($model, 'isForceDeleting') && ! $model->isForceDeleting()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$model->roles()->detach();
|
||||
});
|
||||
}
|
||||
|
||||
public function getRoleClass()
|
||||
{
|
||||
if (! isset($this->roleClass)) {
|
||||
$this->roleClass = app(PermissionRegistrar::class)->getRoleClass();
|
||||
}
|
||||
|
||||
return $this->roleClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* A model may have multiple roles.
|
||||
*/
|
||||
public function roles(): MorphToMany
|
||||
{
|
||||
return $this->morphToMany(
|
||||
config('permission.models.role'),
|
||||
'model',
|
||||
config('permission.table_names.model_has_roles'),
|
||||
config('permission.column_names.model_morph_key'),
|
||||
'role_id'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope the model query to certain roles only.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles
|
||||
* @param string $guard
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeRole(Builder $query, $roles, $guard = null): Builder
|
||||
{
|
||||
if ($roles instanceof Collection) {
|
||||
$roles = $roles->all();
|
||||
}
|
||||
|
||||
if (! is_array($roles)) {
|
||||
$roles = [$roles];
|
||||
}
|
||||
|
||||
$roles = array_map(function ($role) use ($guard) {
|
||||
if ($role instanceof Role) {
|
||||
return $role;
|
||||
}
|
||||
|
||||
$method = is_numeric($role) ? 'findById' : 'findByName';
|
||||
$guard = $guard ?: $this->getDefaultGuardName();
|
||||
|
||||
return $this->getRoleClass()->{$method}($role, $guard);
|
||||
}, $roles);
|
||||
|
||||
return $query->whereHas('roles', function (Builder $subQuery) use ($roles) {
|
||||
$subQuery->whereIn(config('permission.table_names.roles').'.id', \array_column($roles, 'id'));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign the given role to the model.
|
||||
*
|
||||
* @param array|string|\Spatie\Permission\Contracts\Role ...$roles
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function assignRole(...$roles)
|
||||
{
|
||||
$roles = collect($roles)
|
||||
->flatten()
|
||||
->map(function ($role) {
|
||||
if (empty($role)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->getStoredRole($role);
|
||||
})
|
||||
->filter(function ($role) {
|
||||
return $role instanceof Role;
|
||||
})
|
||||
->each(function ($role) {
|
||||
$this->ensureModelSharesGuard($role);
|
||||
})
|
||||
->map->id
|
||||
->all();
|
||||
|
||||
$model = $this->getModel();
|
||||
|
||||
if ($model->exists) {
|
||||
$this->roles()->sync($roles, false);
|
||||
$model->load('roles');
|
||||
} else {
|
||||
$class = \get_class($model);
|
||||
|
||||
$class::saved(
|
||||
function ($object) use ($roles, $model) {
|
||||
static $modelLastFiredOn;
|
||||
if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) {
|
||||
return;
|
||||
}
|
||||
$object->roles()->sync($roles, false);
|
||||
$object->load('roles');
|
||||
$modelLastFiredOn = $object;
|
||||
});
|
||||
}
|
||||
|
||||
$this->forgetCachedPermissions();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke the given role from the model.
|
||||
*
|
||||
* @param string|\Spatie\Permission\Contracts\Role $role
|
||||
*/
|
||||
public function removeRole($role)
|
||||
{
|
||||
$this->roles()->detach($this->getStoredRole($role));
|
||||
|
||||
$this->load('roles');
|
||||
|
||||
$this->forgetCachedPermissions();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all current roles and set the given ones.
|
||||
*
|
||||
* @param array|\Spatie\Permission\Contracts\Role|string ...$roles
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function syncRoles(...$roles)
|
||||
{
|
||||
$this->roles()->detach();
|
||||
|
||||
return $this->assignRole($roles);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the model has (one of) the given role(s).
|
||||
*
|
||||
* @param string|int|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles
|
||||
* @param string|null $guard
|
||||
* @return bool
|
||||
*/
|
||||
public function hasRole($roles, string $guard = null): bool
|
||||
{
|
||||
if (is_string($roles) && false !== strpos($roles, '|')) {
|
||||
$roles = $this->convertPipeToArray($roles);
|
||||
}
|
||||
|
||||
if (is_string($roles)) {
|
||||
return $guard
|
||||
? $this->roles->where('guard_name', $guard)->contains('name', $roles)
|
||||
: $this->roles->contains('name', $roles);
|
||||
}
|
||||
|
||||
if (is_int($roles)) {
|
||||
return $guard
|
||||
? $this->roles->where('guard_name', $guard)->contains('id', $roles)
|
||||
: $this->roles->contains('id', $roles);
|
||||
}
|
||||
|
||||
if ($roles instanceof Role) {
|
||||
return $this->roles->contains('id', $roles->id);
|
||||
}
|
||||
|
||||
if (is_array($roles)) {
|
||||
foreach ($roles as $role) {
|
||||
if ($this->hasRole($role, $guard)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return $roles->intersect($guard ? $this->roles->where('guard_name', $guard) : $this->roles)->isNotEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the model has any of the given role(s).
|
||||
*
|
||||
* Alias to hasRole() but without Guard controls
|
||||
*
|
||||
* @param string|int|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAnyRole(...$roles): bool
|
||||
{
|
||||
return $this->hasRole($roles);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the model has all of the given role(s).
|
||||
*
|
||||
* @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles
|
||||
* @param string|null $guard
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAllRoles($roles, string $guard = null): bool
|
||||
{
|
||||
if (is_string($roles) && false !== strpos($roles, '|')) {
|
||||
$roles = $this->convertPipeToArray($roles);
|
||||
}
|
||||
|
||||
if (is_string($roles)) {
|
||||
return $guard
|
||||
? $this->roles->where('guard_name', $guard)->contains('name', $roles)
|
||||
: $this->roles->contains('name', $roles);
|
||||
}
|
||||
|
||||
if ($roles instanceof Role) {
|
||||
return $this->roles->contains('id', $roles->id);
|
||||
}
|
||||
|
||||
$roles = collect()->make($roles)->map(function ($role) {
|
||||
return $role instanceof Role ? $role->name : $role;
|
||||
});
|
||||
|
||||
return $roles->intersect(
|
||||
$guard
|
||||
? $this->roles->where('guard_name', $guard)->pluck('name')
|
||||
: $this->getRoleNames()) == $roles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all permissions directly coupled to the model.
|
||||
*/
|
||||
public function getDirectPermissions(): Collection
|
||||
{
|
||||
return $this->permissions;
|
||||
}
|
||||
|
||||
public function getRoleNames(): Collection
|
||||
{
|
||||
return $this->roles->pluck('name');
|
||||
}
|
||||
|
||||
protected function getStoredRole($role): Role
|
||||
{
|
||||
$roleClass = $this->getRoleClass();
|
||||
|
||||
if (is_numeric($role)) {
|
||||
return $roleClass->findById($role, $this->getDefaultGuardName());
|
||||
}
|
||||
|
||||
if (is_string($role)) {
|
||||
return $roleClass->findByName($role, $this->getDefaultGuardName());
|
||||
}
|
||||
|
||||
return $role;
|
||||
}
|
||||
|
||||
protected function convertPipeToArray(string $pipeString)
|
||||
{
|
||||
$pipeString = trim($pipeString);
|
||||
|
||||
if (strlen($pipeString) <= 2) {
|
||||
return $pipeString;
|
||||
}
|
||||
|
||||
$quoteCharacter = substr($pipeString, 0, 1);
|
||||
$endCharacter = substr($quoteCharacter, -1, 1);
|
||||
|
||||
if ($quoteCharacter !== $endCharacter) {
|
||||
return explode('|', $pipeString);
|
||||
}
|
||||
|
||||
if (! in_array($quoteCharacter, ["'", '"'])) {
|
||||
return explode('|', $pipeString);
|
||||
}
|
||||
|
||||
return explode('|', trim($pipeString, $quoteCharacter));
|
||||
}
|
||||
}
|
||||
19
vendor/spatie/laravel-permission/src/Traits/RefreshesPermissionCache.php
vendored
Normal file
19
vendor/spatie/laravel-permission/src/Traits/RefreshesPermissionCache.php
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission\Traits;
|
||||
|
||||
use Spatie\Permission\PermissionRegistrar;
|
||||
|
||||
trait RefreshesPermissionCache
|
||||
{
|
||||
public static function bootRefreshesPermissionCache()
|
||||
{
|
||||
static::saved(function () {
|
||||
app(PermissionRegistrar::class)->forgetCachedPermissions();
|
||||
});
|
||||
|
||||
static::deleted(function () {
|
||||
app(PermissionRegistrar::class)->forgetCachedPermissions();
|
||||
});
|
||||
}
|
||||
}
|
||||
124
vendor/spatie/laravel-permission/src/WildcardPermission.php
vendored
Normal file
124
vendor/spatie/laravel-permission/src/WildcardPermission.php
vendored
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Permission;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\Permission\Exceptions\WildcardPermissionNotProperlyFormatted;
|
||||
|
||||
class WildcardPermission
|
||||
{
|
||||
/** @var string */
|
||||
const WILDCARD_TOKEN = '*';
|
||||
|
||||
/** @var string */
|
||||
const PART_DELIMITER = '.';
|
||||
|
||||
/** @var string */
|
||||
const SUBPART_DELIMITER = ',';
|
||||
|
||||
/** @var string */
|
||||
protected $permission;
|
||||
|
||||
/** @var Collection */
|
||||
protected $parts;
|
||||
|
||||
/**
|
||||
* @param string $permission
|
||||
*/
|
||||
public function __construct(string $permission)
|
||||
{
|
||||
$this->permission = $permission;
|
||||
$this->parts = collect();
|
||||
|
||||
$this->setParts();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|WildcardPermission $permission
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function implies($permission): bool
|
||||
{
|
||||
if (is_string($permission)) {
|
||||
$permission = new self($permission);
|
||||
}
|
||||
|
||||
$otherParts = $permission->getParts();
|
||||
|
||||
$i = 0;
|
||||
foreach ($otherParts as $otherPart) {
|
||||
if ($this->getParts()->count() - 1 < $i) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! $this->parts->get($i)->contains(self::WILDCARD_TOKEN)
|
||||
&& ! $this->containsAll($this->parts->get($i), $otherPart)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
for ($i; $i < $this->parts->count(); $i++) {
|
||||
if (! $this->parts->get($i)->contains(self::WILDCARD_TOKEN)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $part
|
||||
* @param Collection $otherPart
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function containsAll(Collection $part, Collection $otherPart): bool
|
||||
{
|
||||
foreach ($otherPart->toArray() as $item) {
|
||||
if (! $part->contains($item)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getParts(): Collection
|
||||
{
|
||||
return $this->parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the different parts and subparts from permission string.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setParts(): void
|
||||
{
|
||||
if (empty($this->permission) || $this->permission == null) {
|
||||
throw WildcardPermissionNotProperlyFormatted::create($this->permission);
|
||||
}
|
||||
|
||||
$parts = collect(explode(self::PART_DELIMITER, $this->permission));
|
||||
|
||||
$parts->each(function ($item, $key) {
|
||||
$subParts = collect(explode(self::SUBPART_DELIMITER, $item));
|
||||
|
||||
if ($subParts->isEmpty() || $subParts->contains('')) {
|
||||
throw WildcardPermissionNotProperlyFormatted::create($this->permission);
|
||||
}
|
||||
|
||||
$this->parts->add($subParts);
|
||||
});
|
||||
|
||||
if ($this->parts->isEmpty()) {
|
||||
throw WildcardPermissionNotProperlyFormatted::create($this->permission);
|
||||
}
|
||||
}
|
||||
}
|
||||
20
vendor/spatie/laravel-permission/src/helpers.php
vendored
Normal file
20
vendor/spatie/laravel-permission/src/helpers.php
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
if (! function_exists('getModelForGuard')) {
|
||||
/**
|
||||
* @param string $guard
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
function getModelForGuard(string $guard)
|
||||
{
|
||||
return collect(config('auth.guards'))
|
||||
->map(function ($guard) {
|
||||
if (! isset($guard['provider'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
return config("auth.providers.{$guard['provider']}.model");
|
||||
})->get($guard);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user