Skip to content
Merged
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
10 changes: 10 additions & 0 deletions src/Commands/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ private function addgroup($group = null, $username = null, $email = null): void
$group = $this->prompt('Group', null, 'required');
}

// Validate the group
if (! $this->validateGroup($group)) {
throw new CancelException('Invalid group: "' . $group . '"');
}

$user = $this->findUser('Add user to group', $username, $email);

$confirm = $this->prompt(
Expand Down Expand Up @@ -635,6 +640,11 @@ private function removegroup($group = null, $username = null, $email = null): vo
$group = $this->prompt('Group', null, 'required');
}

// Validate the group
if (! $this->validateGroup($group)) {
throw new CancelException('Invalid group: "' . $group . '"');
}

$user = $this->findUser('Remove user from group', $username, $email);

$confirm = $this->prompt(
Expand Down
44 changes: 44 additions & 0 deletions tests/Commands/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,24 @@ public function testAddgroup(): void
$this->assertTrue($user->inGroup('admin'));
}

public function testAddgroupWithInvalidGroup(): void
{
$this->createUser([
'username' => 'user10',
'email' => '[email protected]',
'password' => 'secret123',
]);

$this->setMockIo(['y']);

command('shield:user addgroup -n user10 -g invalid');

$this->assertStringContainsString(
'Invalid group: "invalid"',
$this->io->getLastOutput()
);
}

public function testAddgroupCancel(): void
{
$this->createUser([
Expand Down Expand Up @@ -643,6 +661,32 @@ public function testRemovegroup(): void
$this->assertFalse($user->inGroup('admin'));
}

public function testRemovegroupWithInvalidGroup(): void
{
$this->createUser([
'username' => 'user11',
'email' => '[email protected]',
'password' => 'secret123',
]);
$users = model(UserModel::class);
$user = $users->findByCredentials(['email' => '[email protected]']);
$user->addGroup('admin');
$this->assertTrue($user->inGroup('admin'));

$this->setMockIo(['y']);

command('shield:user removegroup -n user11 -g invalid');

$this->assertStringContainsString(
'Invalid group: "invalid"',
$this->io->getLastOutput()
);

$users = model(UserModel::class);
$user = $users->findByCredentials(['email' => '[email protected]']);
$this->assertTrue($user->inGroup('admin'));
}

public function testRemovegroupCancel(): void
{
$this->createUser([
Expand Down