Skip to content
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

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

Merged
merged 7 commits into from
Aug 21, 2024

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Aug 12, 2024

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
$userModel->addToDefaultGroup($user);

$this->write('The user is added to the default group.', 'green');
} else {
Copy link
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 [email protected] -g xxx

Copy link
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 [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))

Copy link
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
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 [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"


$this->write('The user is added to the default group.', 'green');
} else {
$user->addGroup($group);
Copy link
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 [email protected] -g admin,beta

Copy link
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 [email protected] -g mygroup
    shield:user addgroup -n username -g mygroup
    shield:user removegroup -n username -g mygroup

@kenjis
Copy link
Member Author

kenjis commented Aug 12, 2024

@warcooft

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

@warcooft
Copy link
Contributor

What do you think about adding interactions to show users a step-by-step guide. i created a similar command in a personal project to create a user like this.

  Complete the following form:
  -----------------------------

  User Name        : Emilia
  User Email       : [email protected]
  User Group       : admin
  New Password     : 12345678
  Confirm Password : 12345678

  Account created!
  ----------------
  Name      Emilia
  Email     [email protected]
  Group     admin
  Password  12345678

  Done.

@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 warcooft and datamweb August 20, 2024 06:31
Copy link
Collaborator

@datamweb datamweb left a comment

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
34 checks passed
@kenjis kenjis deleted the feat-command-user-create-g-option branch August 21, 2024 07:37
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