Skip to content

Add assign-role command #2834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/Commands/AssignRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Spatie\Permission\Commands;

use Illuminate\Console\Command;
use Spatie\Permission\Contracts\Role as RoleContract;

class AssignRole extends Command
{
protected $signature = 'permission:assign-role
{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}';

protected $description = 'Assign a role to a user';

public function handle()
{
$roleName = $this->argument('name');
$userId = $this->argument('userId');
$guardName = $this->argument('guard');
$userModelClass = $this->argument('userModelNamespace');

// Validate that the model class exists and is instantiable
if (! class_exists($userModelClass)) {
$this->error("User model class [{$userModelClass}] does not exist.");

return Command::FAILURE;
}

$user = (new $userModelClass)::find($userId);

if (! $user) {
$this->error("User with ID {$userId} not found.");

return Command::FAILURE;
}

/** @var \Spatie\Permission\Contracts\Role $roleClass */
$roleClass = app(RoleContract::class);

$role = $roleClass::findOrCreate($roleName, $guardName);

$user->assignRole($role);

$this->info("Role `{$role->name}` assigned to user ID {$userId} successfully.");

return Command::SUCCESS;
}
}
1 change: 1 addition & 0 deletions src/PermissionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ protected function registerCommands(): void
Commands\CreatePermission::class,
Commands\Show::class,
Commands\UpgradeForTeams::class,
Commands\AssignRole::class,
]);
}

Expand Down
59 changes: 59 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\Attributes\Test;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Tests\TestModels\User;

class CommandTest extends TestCase
{
Expand Down Expand Up @@ -248,4 +249,62 @@ public function it_can_respond_to_about_command_with_teams()
$this->assertRegExp($pattern, $output);
}
}

/** @test */
#[Test]
public function test_is_assign_role_to_user()
{
$user = User::first();

Artisan::call('permission:assign-role', [
'name' => 'testRole',
'userId' => $user->id,
'guard' => 'web',
'userModelNamespace' => User::class,
]);

$output = Artisan::output();

$this->assertStringContainsString('Role `testRole` assigned to user ID '.$user->id.' successfully.', $output);
$this->assertCount(1, Role::where('name', 'testRole')->get());
$this->assertCount(1, $user->roles);
$this->assertTrue($user->hasRole('testRole'));
}

/** @test */
#[Test]
public function test_assigning_role_fails_when_user_not_found()
{

Artisan::call('permission:assign-role', [
'name' => 'testRole',
'userId' => 99999,
'guard' => 'web',
'userModelNamespace' => User::class,
]);

$output = Artisan::output();

$this->assertStringContainsString('User with ID 99999 not found.', $output);
}

/** @test */
#[Test]
public function test_assigning_role_fails_when_namspace_not_existed()
{
$user = User::first();

$userModelClass = 'App\Models\NonExistentUser';

Artisan::call('permission:assign-role', [
'name' => 'testRole',
'userId' => $user->id,
'guard' => 'web',
'userModelNamespace' => $userModelClass,
]);

$output = Artisan::output();

$this->assertStringContainsString("User model class [{$userModelClass}] does not exist.", $output);
}
}