diff --git a/src/Commands/User.php b/src/Commands/User.php index 939438138..9c1a91019 100644 --- a/src/Commands/User.php +++ b/src/Commands/User.php @@ -53,6 +53,7 @@ class User extends BaseCommand shield:user options shield:user create -n newusername -e newuser@example.com + shield:user create -n newusername -e newuser@example.com -g mygroup shield:user activate -n username shield:user activate -e user@example.com @@ -159,7 +160,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 +253,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 = []; @@ -311,11 +313,18 @@ 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'); + } } /** diff --git a/tests/Commands/UserTest.php b/tests/Commands/UserTest.php index a85e713d6..9fdf3e148 100644 --- a/tests/Commands/UserTest.php +++ b/tests/Commands/UserTest.php @@ -100,6 +100,40 @@ public function testCreate(): void ]); } + public function testCreateWithGroupBeta(): void + { + $this->setMockIo([ + 'Secret Passw0rd!', + 'Secret Passw0rd!', + ]); + + command('shield:user create -n user1 -e user1@example.com -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' => 'user1@example.com']); + $this->seeInDatabase($this->tables['identities'], [ + 'user_id' => $user->id, + 'secret' => 'user1@example.com', + ]); + $this->seeInDatabase($this->tables['users'], [ + 'id' => $user->id, + 'active' => 0, + ]); + $this->seeInDatabase($this->tables['groups_users'], [ + 'user_id' => $user->id, + 'group' => 'beta', + ]); + } + public function testCreateNotUniqueName(): void { $user = $this->createUser([