3.0 DOCUMENTATION
NOTE: This version is in active development.

USER COMPONENT



HOW IT WORKS:

EVENTS ON BACKEND:

When dealing with backend, in every action like creating user, creating a new group etc has its own events triggered where you can hook in to process any other relevant data to your application. So if you have some work to be done before a user is created or after a user is created, events will help you.


EVENT: PARAMETERS PASSED: DESCRIPTION:
user.creating
Param 1: Array of data of the user to be created This event is triggered before the user is being created.
user.created
Param 1: Eloquent object of the user created This event is triggered after the user is successfully created.
user.updating
Param 1: Array of data of the user to be updated This event is triggered before the user is being updated.
user.updated
Param 1: Eloquent object of user that has been updated. This event is triggered after the user is updated.
user.deleting
Param 1: Array of data of the user to be deleted This event is triggered before the user is deleted.
user.deleted
Param 1: Eloquent object of the user that has been deleted This event is triggered after the user is successfully deleted.
group.creating
Param 1: Array of group data to be created This event is triggered before a group is created.
group.created
Param 1: Eloquent object of the group that has been created. This event is triggered after a group is successfully created.
group.deleting
Param 1: Array of group data to be deleted This event is triggered before a group is deleted.
group.deleted
Param 1: Eloquent object of the group that has been deleted This event is triggered after a group is successfully deleted.

API:

The best way to deal user component is to use the commands. You can use the raw Eloquent query but its more convenient to use the built-in commands for ease of query.

Query Users with or without parameters

NOTE: When using the commands publicly and you get permission errors, put this on your parameters to bypass the permission checking.

'disablePermissionChecking' => true

On your controller, you can do this:


$commandDispatcher = app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher');

$result = $commandDispatcher->dispatchFromArray(
    'Darryldecode\Backend\Components\User\Commands\QueryUsersCommand',
    array(
        'firstName' => '', // (optional) string
        'lastName' => '', // (optional) string
        'email' => '', // (optional) string
        'groupId' => '', // (optional) int
        'orderBy' => 'created_at', // (optional) string
        'orderSort' => 'DESC', // (optional) string. Values:(ASC|DESC) Default: DESC
        'paginated' => true, // (optional) boolean. Default: true
        'perPage' => 15, // (optional) int. Default: 15
        'with' => array('groups'), // (optional) array.
        'disablePermissionChecking' => true // (required) this is mandatory when a command is used as API
    )
);

$result->isSuccessful(); // check if the command transaction was successful or not
$result->getStatusCode(); // the status code Ex 400,200,201 etc
$result->getData(); // the data or set of results
$result->getMessage(); // the message (success or error)
    

Display single user


$commandDispatcher = app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher');

$result = $commandDispatcher->dispatchFromArray(
'Darryldecode\Backend\Components\User\Commands\QueryUsersCommand',
    array(
        'id' => '', // (required) int
        'with' => array('groups'), // (optional) array.
        'disablePermissionChecking' => true // (required) this is mandatory when a command is used as API
    )
);

$result->isSuccessful(); // check if the command transaction was successful or not
$result->getStatusCode(); // the status code Ex 400,200,201 etc
$result->getData(); // the data or set of results
$result->getMessage(); // the message (success or error)
    

CREATING A USER:


$commandDispatcher = app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher');

$result = $commandDispatcher->dispatchFromArray(
    'Darryldecode\Backend\Components\User\Commands\CreateUserCommand',
    array(
        'firstName' => 'John', // (required) string.
        'lastName' => 'Doe', // (require) string.
        'email' => 'jd@mail.com', // (require) string.
        'password' => 'somerandompassword', // (require) string.
        'permissions' => array(
                'user.manage' => 1, // 1 allow, 0 inherit, -1 deny
                'user.delete' => 1,
            ), // (optional) array.
        'groups' => array(
                1 // the ID of the group you want the user to be associated with
            ), // (optional) array.
        'disablePermissionChecking' => true // (required) this is mandatory when a command is used as API
    )
);
    

UPDATING A USER:


$commandDispatcher = app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher');

// When updating, you can provide only the fields you want to update
$result = $commandDispatcher->dispatchFromArray(
    'Darryldecode\Backend\Components\User\Commands\UpdateUserCommand',
    array(
        'firstName' => 'John', // (optional) string.
        'lastName' => 'Doe', // (optional) string.
        'email' => 'jd@mail.com', // (optional) string.
        'password' => 'somerandompassword', // (optional) string.
        'permissions' => array(
        'user.manage' => 1, // 1 allow, 0 inherit, -1 deny
        'user.delete' => 1,
        ), // (optional) array.
        'groups' => array(
        1 => true // the ID of the group you want the user to be associated with
        ), // (optional) array.
        'disablePermissionChecking' => true // (required) this is mandatory when a command is used as API
    )
);
    

DELETING A USER:


$commandDispatcher = app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher');

$result = $commandDispatcher->dispatchFromArray(
    'Darryldecode\Backend\Components\User\Commands\DeleteUserCommand',
    array(
        'id' => $userId,
        'disablePermissionChecking' => true // (required) this is mandatory when a command is used as API
    )
);
    

Extending User Model

There are cases you want to extend the User's Model like adding new relations to it. To do that, follow steps below:

STEP 1.)

Navigate to "config/backend/backend.php" config file. Open that file and you will see on config array that looks like this:


        /*
        * built-in component models being used
        *
        * NOTE:
        *
        * The purpose of this is for extensibility, if you want to extend relationships for user/content model
        * you can change this to your own and make sure to extend this models
        */
        'user_model'    => 'Darryldecode\Backend\Components\User\Models\User',
        'content_model' => 'Darryldecode\Backend\Components\ContentBuilder\Models\Content',
    

You will see above we have this two currently used models, Change it to your new model and make sure to extend it, like this:


namespace App\Backend\Extensions;

use Darryldecode\Backend\Components\User\Models\User;

// your new class, name it whatever you want and just make sure you extent the main User Model
class UserExtended extends User {

    // your new added relations to the User Model
    public function newAddedRelation() {
        return $this->belongsToMany('anything here..');
    }
}

After you have created your new User Model, make sure to update the backend config file, like so:


        /*
        * built-in component models being used
        *
        * NOTE:
        *
        * The purpose of this is for extensibility, if you want to extend relationships for user/content model
        * you can change this to your own and make sure to extend this models
        */
        'user_model'    => 'App\Backend\Extensions\UserExtended', // <-- your new User Model
        'content_model' => 'Darryldecode\Backend\Components\ContentBuilder\Models\Content',
    

As well as the Application's used Auth Model. Open "config/auth.php":


/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/
'model' => 'App\Backend\Extensions\UserExtended', // <-- your new User Model

VOILA! You have now full control of your User's Model! When querying user, you can now add querying its relations like so,


$commandDispatcher = app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher');

$result = $commandDispatcher->dispatchFromArray(
    'Darryldecode\Backend\Components\User\Commands\QueryUsersCommand',
    array(
        'with' => array('newAddedRelation'), // <-- include your new relation
    )
);

User Object Available Methods

There are different ways to get user object:

First, using the typical Laravel Version of getting the current logged in user:


$User = \Auth::user();

Second, is in your controller that extends the \Darryldecode\Backend\Base\Controllers\BaseController, you can access the parent user property which contains the current logged in user or false if not logged in.


// inside your controller method that extends \Darryldecode\Backend\Base\Controllers\BaseController
$User = $this->user; // returns false if not logged in

Third, is just by querying a user:


$commandDispatcher = app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher');

$result = $commandDispatcher->dispatchFromArray(
    'Darryldecode\Backend\Components\User\Commands\QueryUsersCommand',
    array(
        'id' => '', // (required) int
        'with' => array('groups'), // (optional) array.
    )
);

$UserObject = $result->getData();
Check if User belongs to a specific group:

$group = 1; // using group ID
$group = 'members'; // or using group name
$group = $GroupObject; // or the group object

$UserObject->inGroup($group); // returns true or false
Check if User has the given permission:

// check if user has this permission
$permission = 'blog.create';

$UserObject->hasPermission($permission); // returns true or false

// or a set of any of this permissions
$permissions = array('blog.create','blog.delete');

$UserObject->hasAnyPermission($permissions); // returns true or false
Check if User is a superuser:

$UserObject->isSuperUser(); // returns true or false
Get User's combined permissions. This will include permission acquired from its group and its specific given permissions:

$UserObject->getCombinedPermissions(); // returns array of permissions
More documentation coming soon or you may help us improve the docs by making pull request here: https://github.com/darryldecode/laravelbackend-site