update lock clucknut
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 14s
Build, Push and Deploy / build-and-push (push) Successful in 3m14s
Build, Push and Deploy / deploy-staging (push) Successful in 25s
Build, Push and Deploy / deploy-production (push) Has been skipped

This commit is contained in:
2026-04-18 20:32:18 +07:00
parent 4554035227
commit dcaf267458
3359 changed files with 153185 additions and 205489 deletions

View File

@@ -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;