Skip to content

Commit

Permalink
feat: check invalid group name
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Aug 20, 2024
1 parent 7ae3ff5 commit 641c573
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Commands/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use CodeIgniter\Shield\Config\Auth;
use CodeIgniter\Shield\Entities\User as UserEntity;
use CodeIgniter\Shield\Exceptions\UserNotFoundException;
use CodeIgniter\Shield\Models\GroupModel;
use CodeIgniter\Shield\Models\UserModel;
use CodeIgniter\Shield\Validation\ValidationRules;
use Config\Services;
Expand Down Expand Up @@ -305,6 +306,11 @@ private function create(?string $username = null, ?string $email = null, ?string

$user = new UserEntity($data);

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

if ($username === null) {
$userModel->allowEmptyInserts()->save($user);
$this->write('New User created', 'green');
Expand All @@ -327,6 +333,14 @@ private function create(?string $username = null, ?string $email = null, ?string
}
}

private function validateGroup(string $group): bool
{
/** @var GroupModel $groupModel */
$groupModel = model(GroupModel::class);

Check failure on line 339 in src/Commands/User.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.3 Static Analysis

Call to function model with CodeIgniter\Shield\Models\GroupModel::class is discouraged.

Check failure on line 339 in src/Commands/User.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.2 Static Analysis

Call to function model with CodeIgniter\Shield\Models\GroupModel::class is discouraged.

Check failure on line 339 in src/Commands/User.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.0 Static Analysis

Call to function model with CodeIgniter\Shield\Models\GroupModel::class is discouraged.

Check failure on line 339 in src/Commands/User.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1 Static Analysis

Call to function model with CodeIgniter\Shield\Models\GroupModel::class is discouraged.

Check failure on line 339 in src/Commands/User.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 7.4 Static Analysis

Call to function model with CodeIgniter\Shield\Models\GroupModel::class is discouraged.

return $groupModel->isValidGroup($group);
}

/**
* Activate an existing user by username or email
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Commands/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@ public function testCreateWithGroupBeta(): void
]);
}

public function testCreateWithInvalidGroup(): void
{
$this->setMockIo([
'Secret Passw0rd!',
'Secret Passw0rd!',
]);

command('shield:user create -n user1 -e user1@example.com -g invalid');

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

$users = model(UserModel::class);
$user = $users->findByCredentials(['email' => 'user1@example.com']);
$this->assertNull($user);
}

public function testCreateNotUniqueName(): void
{
$user = $this->createUser([
Expand Down

0 comments on commit 641c573

Please sign in to comment.