Skip to content

Commit

Permalink
feat: add -g option to shield:user create
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Aug 20, 2024
1 parent 2554481 commit 56662ca
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Commands/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class User extends BaseCommand
shield:user <action> 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
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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');
}
}

/**
Expand Down
34 changes: 34 additions & 0 deletions tests/Commands/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit 56662ca

Please sign in to comment.