feat: add -g option to `shield:user create` by kenjis · Pull Request #1164 · codeigniter4/shield · GitHub
Skip to content

feat: add -g option to shield:user create#1164

Merged
kenjis merged 7 commits into
codeigniter4:developfrom
kenjis:feat-command-user-create-g-option
Aug 21, 2024
Merged

feat: add -g option to shield:user create#1164
kenjis merged 7 commits into
codeigniter4:developfrom
kenjis:feat-command-user-create-g-option

Conversation

@kenjis

@kenjis kenjis commented Aug 12, 2024

Copy link
Copy Markdown
Member

Needs #1162

Description

  • add -g option to shield:user create
  • add GroupModel::isValidGroup()

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added the enhancement New feature or request label Aug 12, 2024
Comment thread src/Commands/User.php
Comment thread src/Commands/User.php

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if an invalid group is set?
shield:user create -n user1 -e user1@example.com -g xxx

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ ./spark shield:user create -n user2 -e user2@example.com -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', 'user2@example.com', '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))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above is not good, but the same as addgroup.

$ ./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))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added checking the group name.

$ ./spark shield:user create -n user2 -e user2@example.com -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"

Comment thread src/Commands/User.php

$this->write('The user is added to the default group.', 'green');
} else {
$user->addGroup($group);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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 user1@example.com -g admin,beta

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another enhancement.
If we add this, it is better to change all -g option.

    shield:user create -n newusername -e newuser@example.com -g mygroup
    shield:user addgroup -n username -g mygroup
    shield:user removegroup -n username -g mygroup

@kenjis

kenjis commented Aug 12, 2024

Copy link
Copy Markdown
Member Author

@warcooft

'-i' => 'User id',
'-n' => 'User name',
'-e' => 'User email',

@warcooft

Copy link
Copy Markdown
Contributor

@kenjis kenjis force-pushed the feat-command-user-create-g-option branch from c8a94c4 to 56662ca Compare August 20, 2024 04:06
@kenjis kenjis requested review from datamweb and warcooft August 20, 2024 06:31

@datamweb datamweb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks.

@kenjis kenjis merged commit 60f1ae3 into codeigniter4:develop Aug 21, 2024
@kenjis kenjis deleted the feat-command-user-create-g-option branch August 21, 2024 07:37
@michalsn michalsn mentioned this pull request Jul 14, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants