Skip to content

Commit 918051a

Browse files
authored
Merge pull request #1176 from kenjis/add-group-validation-to-user-command
fix: add missing validation for group name to `shield:user addgroup`/`removegroup`
2 parents 4482ec7 + c003977 commit 918051a

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/Commands/User.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,11 @@ private function addgroup($group = null, $username = null, $email = null): void
603603
$group = $this->prompt('Group', null, 'required');
604604
}
605605

606+
// Validate the group
607+
if (! $this->validateGroup($group)) {
608+
throw new CancelException('Invalid group: "' . $group . '"');
609+
}
610+
606611
$user = $this->findUser('Add user to group', $username, $email);
607612

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

643+
// Validate the group
644+
if (! $this->validateGroup($group)) {
645+
throw new CancelException('Invalid group: "' . $group . '"');
646+
}
647+
638648
$user = $this->findUser('Remove user from group', $username, $email);
639649

640650
$confirm = $this->prompt(

tests/Commands/UserTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,24 @@ public function testAddgroup(): void
595595
$this->assertTrue($user->inGroup('admin'));
596596
}
597597

598+
public function testAddgroupWithInvalidGroup(): void
599+
{
600+
$this->createUser([
601+
'username' => 'user10',
602+
'email' => 'user10@example.com',
603+
'password' => 'secret123',
604+
]);
605+
606+
$this->setMockIo(['y']);
607+
608+
command('shield:user addgroup -n user10 -g invalid');
609+
610+
$this->assertStringContainsString(
611+
'Invalid group: "invalid"',
612+
$this->io->getLastOutput()
613+
);
614+
}
615+
598616
public function testAddgroupCancel(): void
599617
{
600618
$this->createUser([
@@ -643,6 +661,32 @@ public function testRemovegroup(): void
643661
$this->assertFalse($user->inGroup('admin'));
644662
}
645663

664+
public function testRemovegroupWithInvalidGroup(): void
665+
{
666+
$this->createUser([
667+
'username' => 'user11',
668+
'email' => 'user11@example.com',
669+
'password' => 'secret123',
670+
]);
671+
$users = model(UserModel::class);
672+
$user = $users->findByCredentials(['email' => 'user11@example.com']);
673+
$user->addGroup('admin');
674+
$this->assertTrue($user->inGroup('admin'));
675+
676+
$this->setMockIo(['y']);
677+
678+
command('shield:user removegroup -n user11 -g invalid');
679+
680+
$this->assertStringContainsString(
681+
'Invalid group: "invalid"',
682+
$this->io->getLastOutput()
683+
);
684+
685+
$users = model(UserModel::class);
686+
$user = $users->findByCredentials(['email' => 'user11@example.com']);
687+
$this->assertTrue($user->inGroup('admin'));
688+
}
689+
646690
public function testRemovegroupCancel(): void
647691
{
648692
$this->createUser([

0 commit comments

Comments
 (0)