Skip to content

Commit 641c573

Browse files
committed
feat: check invalid group name
1 parent 7ae3ff5 commit 641c573

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Commands/User.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use CodeIgniter\Shield\Config\Auth;
2020
use CodeIgniter\Shield\Entities\User as UserEntity;
2121
use CodeIgniter\Shield\Exceptions\UserNotFoundException;
22+
use CodeIgniter\Shield\Models\GroupModel;
2223
use CodeIgniter\Shield\Models\UserModel;
2324
use CodeIgniter\Shield\Validation\ValidationRules;
2425
use Config\Services;
@@ -305,6 +306,11 @@ private function create(?string $username = null, ?string $email = null, ?string
305306

306307
$user = new UserEntity($data);
307308

309+
// Validate the group
310+
if ($group !== null && ! $this->validateGroup($group)) {
311+
throw new CancelException('Invalid group: "' . $group . '"');
312+
}
313+
308314
if ($username === null) {
309315
$userModel->allowEmptyInserts()->save($user);
310316
$this->write('New User created', 'green');
@@ -327,6 +333,14 @@ private function create(?string $username = null, ?string $email = null, ?string
327333
}
328334
}
329335

336+
private function validateGroup(string $group): bool
337+
{
338+
/** @var GroupModel $groupModel */
339+
$groupModel = model(GroupModel::class);
340+
341+
return $groupModel->isValidGroup($group);
342+
}
343+
330344
/**
331345
* Activate an existing user by username or email
332346
*

tests/Commands/UserTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,25 @@ public function testCreateWithGroupBeta(): void
134134
]);
135135
}
136136

137+
public function testCreateWithInvalidGroup(): void
138+
{
139+
$this->setMockIo([
140+
'Secret Passw0rd!',
141+
'Secret Passw0rd!',
142+
]);
143+
144+
command('shield:user create -n user1 -e user1@example.com -g invalid');
145+
146+
$this->assertStringContainsString(
147+
'Invalid group: "invalid"',
148+
$this->io->getFirstOutput()
149+
);
150+
151+
$users = model(UserModel::class);
152+
$user = $users->findByCredentials(['email' => 'user1@example.com']);
153+
$this->assertNull($user);
154+
}
155+
137156
public function testCreateNotUniqueName(): void
138157
{
139158
$user = $this->createUser([

0 commit comments

Comments
 (0)