-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1164 from kenjis/feat-command-user-create-g-option
feat: add -g option to `shield:user create`
- Loading branch information
Showing
6 changed files
with
116 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -53,6 +54,7 @@ class User extends BaseCommand | |
shield:user <action> options | ||
shield:user create -n newusername -e [email protected] | ||
shield:user create -n newusername -e [email protected] -g mygroup | ||
shield:user activate -n username | ||
shield:user activate -e [email protected] | ||
|
@@ -159,7 +161,7 @@ public function run(array $params): int | |
try { | ||
switch ($action) { | ||
case 'create': | ||
$this->create($username, $email); | ||
$this->create($username, $email, $group); | ||
break; | ||
|
||
case 'activate': | ||
|
@@ -252,8 +254,9 @@ private function setValidationRules(): void | |
* | ||
* @param string|null $username User name to create (optional) | ||
* @param string|null $email User email to create (optional) | ||
* @param string|null $group Group to add user to (optional) | ||
*/ | ||
private function create(?string $username = null, ?string $email = null): void | ||
private function create(?string $username = null, ?string $email = null, ?string $group = null): void | ||
{ | ||
$data = []; | ||
|
||
|
@@ -303,6 +306,11 @@ private function create(?string $username = null, ?string $email = null): void | |
|
||
$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'); | ||
|
@@ -311,11 +319,26 @@ private function create(?string $username = null, ?string $email = null): void | |
$this->write('User "' . $username . '" created', 'green'); | ||
} | ||
|
||
// Add to default group | ||
$user = $userModel->findById($userModel->getInsertID()); | ||
$userModel->addToDefaultGroup($user); | ||
|
||
$this->write('The user is added to the default group.', 'green'); | ||
if ($group === null) { | ||
// Add to default group | ||
$userModel->addToDefaultGroup($user); | ||
|
||
$this->write('The user is added to the default group.', 'green'); | ||
} else { | ||
$user->addGroup($group); | ||
|
||
$this->write('The user is added to group "' . $group . '".', 'green'); | ||
} | ||
} | ||
|
||
private function validateGroup(string $group): bool | ||
{ | ||
/** @var GroupModel $groupModel */ | ||
$groupModel = model(GroupModel::class); | ||
|
||
return $groupModel->isValidGroup($group); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,59 @@ public function testCreate(): void | |
]); | ||
} | ||
|
||
public function testCreateWithGroupBeta(): void | ||
{ | ||
$this->setMockIo([ | ||
'Secret Passw0rd!', | ||
'Secret Passw0rd!', | ||
]); | ||
|
||
command('shield:user create -n user1 -e [email protected] -g beta'); | ||
|
||
$this->assertStringContainsString( | ||
'User "user1" created', | ||
$this->io->getFirstOutput() | ||
); | ||
$this->assertStringContainsString( | ||
'The user is added to group "beta"', | ||
$this->io->getFirstOutput() | ||
); | ||
|
||
$users = model(UserModel::class); | ||
$user = $users->findByCredentials(['email' => '[email protected]']); | ||
$this->seeInDatabase($this->tables['identities'], [ | ||
'user_id' => $user->id, | ||
'secret' => '[email protected]', | ||
]); | ||
$this->seeInDatabase($this->tables['users'], [ | ||
'id' => $user->id, | ||
'active' => 0, | ||
]); | ||
$this->seeInDatabase($this->tables['groups_users'], [ | ||
'user_id' => $user->id, | ||
'group' => 'beta', | ||
]); | ||
} | ||
|
||
public function testCreateWithInvalidGroup(): void | ||
{ | ||
$this->setMockIo([ | ||
'Secret Passw0rd!', | ||
'Secret Passw0rd!', | ||
]); | ||
|
||
command('shield:user create -n user1 -e [email protected] -g invalid'); | ||
|
||
$this->assertStringContainsString( | ||
'Invalid group: "invalid"', | ||
$this->io->getFirstOutput() | ||
); | ||
|
||
$users = model(UserModel::class); | ||
$user = $users->findByCredentials(['email' => '[email protected]']); | ||
$this->assertNull($user); | ||
} | ||
|
||
public function testCreateNotUniqueName(): void | ||
{ | ||
$user = $this->createUser([ | ||
|