-
Notifications
You must be signed in to change notification settings - Fork 143
feat: add -g option to shield:user create
#1164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
56662ca
4cac2d8
e79bf78
7ae3ff5
641c573
0fe1251
2e7c54b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if an invalid group is set?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $ ./spark shield:user create -n user2 -e [email protected] -g xxx
CodeIgniter v4.5.3 Command Line Tool - Server Time: 2024-08-20 04:13:43 UTC+00:00
Password : password
Password confirmation : password
User "user2" created
[CodeIgniter\Shield\Authorization\AuthorizationException]
xxx is not a valid group.
at VENDORPATH/codeigniter4/shield/src/Authorization/AuthorizationException.php:24
Backtrace:
1 VENDORPATH/codeigniter4/shield/src/Authorization/Traits/Authorizable.php:50
CodeIgniter\Shield\Authorization\AuthorizationException::forUnknownGroup('xxx')
2 VENDORPATH/codeigniter4/shield/src/Commands/User.php:324
CodeIgniter\Shield\Entities\User()->addGroup('xxx')
3 VENDORPATH/codeigniter4/shield/src/Commands/User.php:163
CodeIgniter\Shield\Commands\User()->create('user2', '[email protected]', 'xxx')
4 SYSTEMPATH/CLI/Commands.php:70
CodeIgniter\Shield\Commands\User()->run([...])
5 SYSTEMPATH/CLI/Console.php:48
CodeIgniter\CLI\Commands()->run('shield:user', [...])
6 SYSTEMPATH/Boot.php:351
CodeIgniter\CLI\Console()->run()
7 SYSTEMPATH/Boot.php:104
CodeIgniter\Boot::runCommand(Object(CodeIgniter\CLI\Console))
8 ROOTPATH/spark:84
CodeIgniter\Boot::bootSpark(Object(Config\Paths))
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The above is not good, but the same as $ ./spark shield:user addgroup -n user1 -g mygroup
CodeIgniter v4.5.3 Command Line Tool - Server Time: 2024-08-20 04:12:13 UTC+00:00
Add the user "user1" to the group "mygroup" ? [y, n]: y
[CodeIgniter\Shield\Authorization\AuthorizationException]
mygroup is not a valid group.
at VENDORPATH/codeigniter4/shield/src/Authorization/AuthorizationException.php:24
Backtrace:
1 VENDORPATH/codeigniter4/shield/src/Authorization/Traits/Authorizable.php:50
CodeIgniter\Shield\Authorization\AuthorizationException::forUnknownGroup('mygroup')
2 VENDORPATH/codeigniter4/shield/src/Commands/User.php:600
CodeIgniter\Shield\Entities\User()->addGroup('mygroup')
3 VENDORPATH/codeigniter4/shield/src/Commands/User.php:195
CodeIgniter\Shield\Commands\User()->addgroup('mygroup', 'user1', null)
4 SYSTEMPATH/CLI/Commands.php:70
CodeIgniter\Shield\Commands\User()->run([...])
5 SYSTEMPATH/CLI/Console.php:48
CodeIgniter\CLI\Commands()->run('shield:user', [...])
6 SYSTEMPATH/Boot.php:351
CodeIgniter\CLI\Console()->run()
7 SYSTEMPATH/Boot.php:104
CodeIgniter\Boot::runCommand(Object(CodeIgniter\CLI\Console))
8 ROOTPATH/spark:84
CodeIgniter\Boot::bootSpark(Object(Config\Paths))
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added checking the group name. $ ./spark shield:user create -n user2 -e [email protected] -g xxx
CodeIgniter v4.5.4 Command Line Tool - Server Time: 2024-08-20 06:02:07 UTC+00:00
Password : password
Password confirmation : password
Invalid group: "xxx" |
||
| $user->addGroup($group); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to be able to set up multiple groups. For example: $user->addGroup('admin', 'beta');shield:user create -n user1 -e [email protected] -g admin,beta
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is another enhancement. |
||
|
|
||
| $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); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| 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([ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.