update lock clucknut
All checks were successful
All checks were successful
This commit is contained in:
@@ -3,7 +3,9 @@
|
||||
namespace Spatie\Permission\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Spatie\Permission\Contracts\Role;
|
||||
use Spatie\Permission\Contracts\Role as RoleContract;
|
||||
use Spatie\Permission\PermissionRegistrar;
|
||||
|
||||
class AssignRole extends Command
|
||||
{
|
||||
@@ -11,17 +13,24 @@ class AssignRole extends Command
|
||||
{name : The name of the role}
|
||||
{userId : The ID of the user to assign the role to}
|
||||
{guard? : The name of the guard}
|
||||
{userModelNamespace=App\Models\User : The fully qualified class name of the user model}';
|
||||
{userModelNamespace=App\Models\User : The fully qualified class name of the user model}
|
||||
{--team-id=}';
|
||||
|
||||
protected $description = 'Assign a role to a user. (Note: does not support Teams.)';
|
||||
protected $description = 'Assign a role to a user';
|
||||
|
||||
public function handle()
|
||||
public function handle(PermissionRegistrar $permissionRegistrar)
|
||||
{
|
||||
$roleName = $this->argument('name');
|
||||
$userId = $this->argument('userId');
|
||||
$guardName = $this->argument('guard');
|
||||
$userModelClass = $this->argument('userModelNamespace');
|
||||
|
||||
if (! $permissionRegistrar->teams && $this->option('team-id')) {
|
||||
$this->warn('Teams feature disabled, argument --team-id has no effect. Either enable it in permissions config file or remove --team-id parameter');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate that the model class exists and is instantiable
|
||||
if (! class_exists($userModelClass)) {
|
||||
$this->error("User model class [{$userModelClass}] does not exist.");
|
||||
@@ -37,13 +46,18 @@ public function handle()
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
/** @var \Spatie\Permission\Contracts\Role $roleClass */
|
||||
$teamIdAux = getPermissionsTeamId();
|
||||
setPermissionsTeamId($this->option('team-id') ?: null);
|
||||
|
||||
/** @var Role $roleClass */
|
||||
$roleClass = app(RoleContract::class);
|
||||
|
||||
$role = $roleClass::findOrCreate($roleName, $guardName);
|
||||
|
||||
$user->assignRole($role);
|
||||
|
||||
setPermissionsTeamId($teamIdAux);
|
||||
|
||||
$this->info("Role `{$role->name}` assigned to user ID {$userId} successfully.");
|
||||
|
||||
return Command::SUCCESS;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Spatie\Permission\Contracts;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
|
||||
|
||||
/**
|
||||
* @property int|string $id
|
||||
@@ -24,7 +25,7 @@ public function roles(): BelongsToMany;
|
||||
* Find a permission by its name.
|
||||
*
|
||||
*
|
||||
* @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist
|
||||
* @throws PermissionDoesNotExist
|
||||
*/
|
||||
public static function findByName(string $name, ?string $guardName): self;
|
||||
|
||||
@@ -32,7 +33,7 @@ public static function findByName(string $name, ?string $guardName): self;
|
||||
* Find a permission by its id.
|
||||
*
|
||||
*
|
||||
* @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist
|
||||
* @throws PermissionDoesNotExist
|
||||
*/
|
||||
public static function findById(int|string $id, ?string $guardName): self;
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Spatie\Permission\Contracts;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
interface PermissionsTeamResolver
|
||||
{
|
||||
public function getPermissionsTeamId(): int|string|null;
|
||||
@@ -9,7 +11,7 @@ public function getPermissionsTeamId(): int|string|null;
|
||||
/**
|
||||
* Set the team id for teams/groups support, this id is used when querying permissions/roles
|
||||
*
|
||||
* @param int|string|\Illuminate\Database\Eloquent\Model|null $id
|
||||
* @param int|string|Model|null $id
|
||||
*/
|
||||
public function setPermissionsTeamId($id): void;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Spatie\Permission\Contracts;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Spatie\Permission\Exceptions\RoleDoesNotExist;
|
||||
|
||||
/**
|
||||
* @property int|string $id
|
||||
@@ -24,7 +25,7 @@ public function permissions(): BelongsToMany;
|
||||
* Find a role by its name and guard name.
|
||||
*
|
||||
*
|
||||
* @throws \Spatie\Permission\Exceptions\RoleDoesNotExist
|
||||
* @throws RoleDoesNotExist
|
||||
*/
|
||||
public static function findByName(string $name, ?string $guardName): self;
|
||||
|
||||
@@ -32,7 +33,7 @@ public static function findByName(string $name, ?string $guardName): self;
|
||||
* Find a role by its id and guard name.
|
||||
*
|
||||
*
|
||||
* @throws \Spatie\Permission\Exceptions\RoleDoesNotExist
|
||||
* @throws RoleDoesNotExist
|
||||
*/
|
||||
public static function findById(int|string $id, ?string $guardName): self;
|
||||
|
||||
@@ -44,7 +45,7 @@ public static function findOrCreate(string $name, ?string $guardName): self;
|
||||
/**
|
||||
* Determine if the user may perform the given permission.
|
||||
*
|
||||
* @param string|int|\Spatie\Permission\Contracts\Permission|\BackedEnum $permission
|
||||
* @param string|int|Permission|\BackedEnum $permission
|
||||
*/
|
||||
public function hasPermissionTo($permission, ?string $guardName): bool;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Spatie\Permission;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Spatie\Permission\Contracts\PermissionsTeamResolver;
|
||||
|
||||
class DefaultTeamResolver implements PermissionsTeamResolver
|
||||
@@ -11,11 +12,11 @@ class DefaultTeamResolver implements PermissionsTeamResolver
|
||||
/**
|
||||
* Set the team id for teams/groups support, this id is used when querying permissions/roles
|
||||
*
|
||||
* @param int|string|\Illuminate\Database\Eloquent\Model|null $id
|
||||
* @param int|string|Model|null $id
|
||||
*/
|
||||
public function setPermissionsTeamId($id): void
|
||||
{
|
||||
if ($id instanceof \Illuminate\Database\Eloquent\Model) {
|
||||
if ($id instanceof Model) {
|
||||
$id = $id->getKey();
|
||||
}
|
||||
$this->teamId = $id;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Spatie\Permission\Contracts\Permission as PermissionContract;
|
||||
use Spatie\Permission\Exceptions\PermissionAlreadyExists;
|
||||
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
|
||||
@@ -14,8 +15,8 @@
|
||||
use Spatie\Permission\Traits\RefreshesPermissionCache;
|
||||
|
||||
/**
|
||||
* @property ?\Illuminate\Support\Carbon $created_at
|
||||
* @property ?\Illuminate\Support\Carbon $updated_at
|
||||
* @property ?Carbon $created_at
|
||||
* @property ?Carbon $updated_at
|
||||
*/
|
||||
class Permission extends Model implements PermissionContract
|
||||
{
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Spatie\Permission\Contracts\Permission;
|
||||
use Spatie\Permission\Contracts\Role as RoleContract;
|
||||
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
|
||||
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
|
||||
@@ -15,8 +17,8 @@
|
||||
use Spatie\Permission\Traits\RefreshesPermissionCache;
|
||||
|
||||
/**
|
||||
* @property ?\Illuminate\Support\Carbon $created_at
|
||||
* @property ?\Illuminate\Support\Carbon $updated_at
|
||||
* @property ?Carbon $created_at
|
||||
* @property ?Carbon $updated_at
|
||||
*/
|
||||
class Role extends Model implements RoleContract
|
||||
{
|
||||
@@ -188,7 +190,7 @@ protected static function findByParam(array $params = []): ?RoleContract
|
||||
/**
|
||||
* Determine if the role may perform the given permission.
|
||||
*
|
||||
* @param string|int|\Spatie\Permission\Contracts\Permission|\BackedEnum $permission
|
||||
* @param string|int|Permission|\BackedEnum $permission
|
||||
*
|
||||
* @throws PermissionDoesNotExist|GuardDoesNotMatch
|
||||
*/
|
||||
|
||||
@@ -101,7 +101,7 @@ protected function getCacheStoreFromConfig(): Repository
|
||||
/**
|
||||
* Set the team id for teams/groups support, this id is used when querying permissions/roles
|
||||
*
|
||||
* @param int|string|\Illuminate\Database\Eloquent\Model|null $id
|
||||
* @param int|string|Model|null $id
|
||||
*/
|
||||
public function setPermissionsTeamId($id): void
|
||||
{
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\View\Compilers\BladeCompiler;
|
||||
use Laravel\Octane\Contracts\OperationTerminated;
|
||||
use Spatie\Permission\Contracts\Permission as PermissionContract;
|
||||
use Spatie\Permission\Contracts\Role as RoleContract;
|
||||
|
||||
@@ -101,7 +102,7 @@ protected function registerOctaneListener(): void
|
||||
|
||||
$dispatcher = $this->app[Dispatcher::class];
|
||||
// @phpstan-ignore-next-line
|
||||
$dispatcher->listen(function (\Laravel\Octane\Contracts\OperationTerminated $event) {
|
||||
$dispatcher->listen(function (OperationTerminated $event) {
|
||||
// @phpstan-ignore-next-line
|
||||
$event->sandbox->make(PermissionRegistrar::class)->setPermissionsTeamId(null);
|
||||
});
|
||||
@@ -110,7 +111,7 @@ protected function registerOctaneListener(): void
|
||||
return;
|
||||
}
|
||||
// @phpstan-ignore-next-line
|
||||
$dispatcher->listen(function (\Laravel\Octane\Contracts\OperationTerminated $event) {
|
||||
$dispatcher->listen(function (OperationTerminated $event) {
|
||||
// @phpstan-ignore-next-line
|
||||
$event->sandbox->make(PermissionRegistrar::class)->clearPermissionsCollection();
|
||||
});
|
||||
|
||||
12
vendor/spatie/laravel-permission/src/helpers.php
vendored
12
vendor/spatie/laravel-permission/src/helpers.php
vendored
@@ -1,20 +1,24 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Spatie\Permission\Guard;
|
||||
use Spatie\Permission\PermissionRegistrar;
|
||||
|
||||
if (! function_exists('getModelForGuard')) {
|
||||
function getModelForGuard(string $guard): ?string
|
||||
{
|
||||
return Spatie\Permission\Guard::getModelForGuard($guard);
|
||||
return Guard::getModelForGuard($guard);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (! function_exists('setPermissionsTeamId')) {
|
||||
/**
|
||||
* @param int|string|null|\Illuminate\Database\Eloquent\Model $id
|
||||
* @param int|string|null|Model $id
|
||||
*/
|
||||
function setPermissionsTeamId($id)
|
||||
{
|
||||
app(\Spatie\Permission\PermissionRegistrar::class)->setPermissionsTeamId($id);
|
||||
app(PermissionRegistrar::class)->setPermissionsTeamId($id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +28,6 @@ function setPermissionsTeamId($id)
|
||||
*/
|
||||
function getPermissionsTeamId()
|
||||
{
|
||||
return app(\Spatie\Permission\PermissionRegistrar::class)->getPermissionsTeamId();
|
||||
return app(PermissionRegistrar::class)->getPermissionsTeamId();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user