Skip to content

Commit 56662ca

Browse files
committed
feat: add -g option to shield:user create
1 parent 2554481 commit 56662ca

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/Commands/User.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class User extends BaseCommand
5353
shield:user <action> options
5454
5555
shield:user create -n newusername -e [email protected]
56+
shield:user create -n newusername -e [email protected] -g mygroup
5657
5758
shield:user activate -n username
5859
shield:user activate -e [email protected]
@@ -159,7 +160,7 @@ public function run(array $params): int
159160
try {
160161
switch ($action) {
161162
case 'create':
162-
$this->create($username, $email);
163+
$this->create($username, $email, $group);
163164
break;
164165

165166
case 'activate':
@@ -252,8 +253,9 @@ private function setValidationRules(): void
252253
*
253254
* @param string|null $username User name to create (optional)
254255
* @param string|null $email User email to create (optional)
256+
* @param string|null $group Group to add user to (optional)
255257
*/
256-
private function create(?string $username = null, ?string $email = null): void
258+
private function create(?string $username = null, ?string $email = null, ?string $group = null): void
257259
{
258260
$data = [];
259261

@@ -311,11 +313,18 @@ private function create(?string $username = null, ?string $email = null): void
311313
$this->write('User "' . $username . '" created', 'green');
312314
}
313315

314-
// Add to default group
315316
$user = $userModel->findById($userModel->getInsertID());
316-
$userModel->addToDefaultGroup($user);
317317

318-
$this->write('The user is added to the default group.', 'green');
318+
if ($group === null) {
319+
// Add to default group
320+
$userModel->addToDefaultGroup($user);
321+
322+
$this->write('The user is added to the default group.', 'green');
323+
} else {
324+
$user->addGroup($group);
325+
326+
$this->write('The user is added to group "' . $group . '".', 'green');
327+
}
319328
}
320329

321330
/**

tests/Commands/UserTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,40 @@ public function testCreate(): void
100100
]);
101101
}
102102

103+
public function testCreateWithGroupBeta(): void
104+
{
105+
$this->setMockIo([
106+
'Secret Passw0rd!',
107+
'Secret Passw0rd!',
108+
]);
109+
110+
command('shield:user create -n user1 -e [email protected] -g beta');
111+
112+
$this->assertStringContainsString(
113+
'User "user1" created',
114+
$this->io->getFirstOutput()
115+
);
116+
$this->assertStringContainsString(
117+
'The user is added to group "beta"',
118+
$this->io->getFirstOutput()
119+
);
120+
121+
$users = model(UserModel::class);
122+
$user = $users->findByCredentials(['email' => '[email protected]']);
123+
$this->seeInDatabase($this->tables['identities'], [
124+
'user_id' => $user->id,
125+
'secret' => '[email protected]',
126+
]);
127+
$this->seeInDatabase($this->tables['users'], [
128+
'id' => $user->id,
129+
'active' => 0,
130+
]);
131+
$this->seeInDatabase($this->tables['groups_users'], [
132+
'user_id' => $user->id,
133+
'group' => 'beta',
134+
]);
135+
}
136+
103137
public function testCreateNotUniqueName(): void
104138
{
105139
$user = $this->createUser([

0 commit comments

Comments
 (0)