luilliarcec/laravel-user-commands

Laravel User Commands is a package that provides the necessary commands for user manipulation from the console.

3.1.1 2021-09-02 22:28 UTC

This package is auto-updated.

Last update: 2024-04-29 04:46:55 UTC


README

run-tests Latest Version on Packagist Quality Score Total Downloads GitHub license

If like me, you have ever considered having commands to manipulate users within your application, this package will help you.

Installation

You can install the package via composer:

composer require luilliarcec/laravel-user-commands

Now publish the configuration file into your app's config directory, by running the following command:

php artisan vendor:publish --provider="Luilliarcec\UserCommands\UserCommandsServiceProvider"

That is all. 😀

Usage

The package has 4 basic commands

Commands Description
user:create Create a new user in your app
user:reset-password Restore a user's password
user:delete Delete a user
user:restore Restore a user

Create Users

php artisan user:create
Fields

All the fields defined in your fillable property of your model will be used when executing the command. If you want to add more fields you can do it from your config file (your file takes precedence over your model, so if you define the fields in your config file the fields of your fillable property will be ignored)

Rules

Whether you have fields defined in your model or in the configuration file, the filled rule will be dynamically applied to those fields. If you want to add custom rules you can do it from the rules key from your configuration file, these will be merged in such a way that those fields that have not been given a custom rule will use the filled rule by default.

After all, you are free to extend the command and configure it to your liking.

php artisan user:create -a username:larcec -a "other_field:Value of field"

or

php artisan user:create --attributes=username:larcec --attributes="other_field:Value of field"

If you want to mark the user's email as verified you can pass the argument --verified Ex.:

php artisan user:create --verified

If your model uses roles and permissions, you must configure the model of the roles and permissions and the name of the relationships in the configuration file.

You can then pass the permissions or roles as arguments.

php artisan user:create -p "user-create" -p "user-edit"

It should be noted that the roles and permissions
must already exist in your database and will be searched by the name field

Once the user has been created, the notification that it has been created will be sent if it implements the MustVerifyEmail interface and if the verification.verify route name exists

If you want to apply your own logic or just save default data for all users, you can extend the command and apply your necessary logic, or copy the prepareForSave method and add your necessary data. Ex.:

<?php

namespace App\Commands;

use Luilliarcec\UserCommands\Commands\CreateNewUserCommand as CreateNewUserCommandBase;

class CreateNewUserCommand extends CreateNewUserCommandBase
{
    protected function prepareForSave(): array
    {
        return $this->merge([
            'password' => Hash::make($this->data['password']),
            'username' => Username::make($this->data['name']),
        ]);
    }
}

$this->data will contain the data of the user that was asked, if you want to access your data that you entered with the --attribute flag you can do it by calling the attributes function which will return a key => value array with your data.

Reset Password User

The command to reset password user receives the value parameter as required. It will be searched by email and if it is not found it will be searched by id

php artisan user:reset-password luis@email.com

However if you want to search for a specific field you can pass it after the value

php artisan user:reset-password larcec -f username

or

php artisan user:reset-password larcec --field username

After executing the command it will ask you to enter a new password and confirm it

Delete Users

In the same way as the command to reset password user, the user is searched by email or id or by specifying a specific field.

php artisan user:delete luis@email.com

or

php artisan user:delete larcec --field username

If your model uses logical elimination, this is executed by default, however if you want to eliminate completely you can pass the --force argument

php artisan user:delete larcec --field username --force

Restore Users

In the same way as the command to reset password user, the user is searched by email or id or by specifying a specific field.

php artisan user:restore luis@email.com

or

php artisan user:restore larcec --field username

Note that this command will only run if your model uses SoftDelete trait

Users With Roles and Permissions

If you want to add roles and permissions to your users, don't forget to configure the relationship name in your configuration file in addition to the role and permission model.

To grant permissions to your user is as easy as:

php artisan user:create -p "user-create" -p "user-edit"

or

php artisan user:create --permissions="user-create" --permissions="user-edit"

If you want to grant all permissions, you can do this:

php artisan user:create -p *

Note that you must send a single permission flag otherwise it will not work.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email luilliarcec@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.