drkwolf/laravel-user

There is no license information available for the latest version (5.8.4) of this package.

laravel auth

5.8.4 2019-04-25 10:44 UTC

This package is auto-updated.

Last update: 2024-04-15 01:00:01 UTC


README

Manage User model that has multiple roles, complex data that change with the role or the context, and multiple contacts

Features:

  • handle optional data: filter and validation
  • handle contacts

Dependencies

  • drkwolf/laravel-handler : for request handler and data presenter
  • spatie/laravel-medialibrary : Optional, form user's avatar
  • laravel/passport : Optional, authentication
  • santigarcor/laratrust : Optional managing teams and groups

Database

Schema::create('users', function(Blueprint $table) {
    $table->increments('id');

    $table->string('username')->unique()->nullable();
    $table->string('phone')->unique()->nullable();
    $table->string('email')->unique()->nullable();
    $table->string('password')->nullable();

    $table->tinyInteger('active')->default(0);

    $table->string('first_name')->nullable();
    $table->string('last_name')->nullable();
    $table->enum('sex', ['M', 'F'])->nullable();

    $table->json('options')->nullable();
    $table->json('contacts')->nullable();

    $table->timestamps();
    $table->softDeletes();
});

Schema::create('tutor_user', function(Blueprint $table) {
    $table->integer('user_id');
    $table->integer('tutor_id');
    $table->json('options')->nullable();

    $table->timestamps();
});

Configuration

publish the configuration file

php artisan vendor:publish --provider="drkwolf\Larauser\LarauserServiceProvider" --tag="config"

setup user model parameters

'model' => [
    // field name ($request)
    'avatar_field' => 'avatar',
    // filesystem disk where default pic is
    // public disk should have url attribute !
    'avatar_disk' => 'public',
    'avatar_collection' => 'avatars',
    'avatar_default' => 'defaults/avatar-102.png',
    // validation rules
    'rules' => [
        'default' => [
            'first_name'    => 'required|string|max:50'
            // ...
        ]
    ]
]

rules can also be a function

'model' => [
    //...
    'rules' => [
        'default' => function ($user_model) {
            return [
                // ...
                'email'         => 'nullable|email|unique:users,email' . ",{$user_model->id}"
            ]
        }
        ],
    ]
]

default Handlers

UserCredentialHandler

Update credentials password, email, username and phone

action fields event
update password, email*, username*, phone* CredentialUpdatedEvent
resetPassword password CredentialUpdatedEvent

*: optional fields

UserHandler

handle user main fields, options and contacts

action fields event
attachAvatar attachAvatar
create * UserCreatedEvent
update * UserUpdatedEvent

UserInfoHandler

same as UserHandler but doesn't handle options field

Managing Users

use drkwolf\Package\Presenter\DefaultPresenter as Presenter;
// creating
$presenter = new Presenter();
$action = 'create'
$response = UserHandler::resolve(
    $action, $params = [],
    $presenter, $request->data, 'admin');

// updating
$presenter = new UserPresenter();
$action = 'update'
$response = UserHandler::resolve(
    $action, $params = [],
    $presenter, $request->data, 'admin');

HasOptions Trait

see docs/options

HasContacts Trait

see docs/contacts