Commit 75f9bc81 authored by Micheal Mand's avatar Micheal Mand Committed by Nicolas Widart

Add more user customization (#257)

* Make sure the config has a key for the presenter

Also set the default value if for some reason they set the presenter to
a falsey value.
Signed-off-by: 's avatarMicheal Mand <micheal@kmdwebdesigns.com>

* Add method to check for role slug

Also inherit docblocks from interface
Signed-off-by: 's avatarMicheal Mand <micheal@kmdwebdesigns.com>

* Add options for custom date fields and field casts
Signed-off-by: 's avatarMicheal Mand <micheal@kmdwebdesigns.com>

* Clean up config documentation a little bit
Signed-off-by: 's avatarMicheal Mand <micheal@kmdwebdesigns.com>

* Add `inRole` method from Sentinel to the User interface
Signed-off-by: 's avatarMicheal Mand <micheal@kmdwebdesigns.com>
parent 6e391ed7
......@@ -30,10 +30,11 @@ return [
*/
'login-columns' => ['email'],
/*
|--------------------------------------------------------------------------
| Define a class that will handle User presentation
|--------------------------------------------------------------------------
*/
|--------------------------------------------------------------------------
| Define a class that will handle User presentation
|--------------------------------------------------------------------------
| Default: \Modules\User\Presenters\UserPresenter::class
*/
'presenter' => \Modules\User\Presenters\UserPresenter::class,
/*
|--------------------------------------------------------------------------
......@@ -44,8 +45,8 @@ return [
/*
|--------------------------------------------------------------------------
| The default role for new user registrations
| Default: User
|--------------------------------------------------------------------------
| Default: User
*/
'default_role' => 'User',
/*
......@@ -61,6 +62,22 @@ return [
'first_name',
'last_name',
],
/*
|--------------------------------------------------------------------------
| Custom date fields
|--------------------------------------------------------------------------
| Set the fields that will be cast to Carbon dates
*/
'dates' => [
],
/*
|--------------------------------------------------------------------------
| Custom casted fields
|--------------------------------------------------------------------------
| Set the fields that will be casted by Eloquent
*/
'casts' => [
],
/*
|--------------------------------------------------------------------------
| Dynamic relations
......
......@@ -32,15 +32,21 @@ class User extends EloquentUser implements UserInterface
{
$this->loginNames = config('asgard.user.config.login-columns');
$this->fillable = config('asgard.user.config.fillable');
$this->presenter = config('asgard.user.config.presenter');
if (config()->has('asgard.user.config.presenter')) {
$this->presenter = config('asgard.user.config.presenter', UserPresenter::class);
}
if (config()->has('asgard.user.config.dates')) {
$this->dates = config('asgard.user.config.dates', []);
}
if (config()->has('asgard.user.config.casts')) {
$this->casts = config('asgard.user.config.casts', []);
}
parent::__construct($attributes);
}
/**
* Checks if a user belongs to the given Role ID
* @param int $roleId
* @return bool
* @inheritdoc
*/
public function hasRoleId($roleId)
{
......@@ -48,9 +54,15 @@ class User extends EloquentUser implements UserInterface
}
/**
* Checks if a user belongs to the given Role Name
* @param string $name
* @return bool
* @inheritdoc
*/
public function hasRoleSlug($slug)
{
return $this->roles()->whereSlug($slug)->count() >= 1;
}
/**
* @inheritdoc
*/
public function hasRoleName($name)
{
......@@ -58,8 +70,7 @@ class User extends EloquentUser implements UserInterface
}
/**
* Check if the current user is activated
* @return bool
* @inheritdoc
*/
public function isActivated()
{
......@@ -79,8 +90,7 @@ class User extends EloquentUser implements UserInterface
}
/**
* Get the first available api key
* @return string
* @inheritdoc
*/
public function getFirstApiKey()
{
......@@ -110,9 +120,7 @@ class User extends EloquentUser implements UserInterface
}
/**
* Check if the user has access to the given permission name
* @param string $permission
* @return boolean
* @inheritdoc
*/
public function hasAccess($permission)
{
......
......@@ -4,6 +4,13 @@ namespace Modules\User\Entities;
interface UserInterface
{
/**
* Checks if the user is in the given role.
* @param mixed $role
* @return bool
*/
public function inRole($role);
/**
* Checks if a user belongs to the given Role ID
* @param int $roleId
......@@ -11,6 +18,13 @@ interface UserInterface
*/
public function hasRoleId($roleId);
/**
* Checks if a user belongs to the given Role Slug
* @param string $slug
* @return bool
*/
public function hasRoleSlug($slug);
/**
* Checks if a user belongs to the given Role Name
* @param string $name
......
......@@ -30,10 +30,11 @@ return [
*/
'login-columns' => ['email'],
/*
|--------------------------------------------------------------------------
| Define a class that will handle User presentation
|--------------------------------------------------------------------------
*/
|--------------------------------------------------------------------------
| Define a class that will handle User presentation
|--------------------------------------------------------------------------
| Default: \Modules\User\Presenters\UserPresenter::class
*/
'presenter' => \Modules\User\Presenters\UserPresenter::class,
/*
|--------------------------------------------------------------------------
......@@ -44,8 +45,8 @@ return [
/*
|--------------------------------------------------------------------------
| The default role for new user registrations
| Default: User
|--------------------------------------------------------------------------
| Default: User
*/
'default_role' => 'User',
/*
......@@ -61,6 +62,22 @@ return [
'first_name',
'last_name',
],
/*
|--------------------------------------------------------------------------
| Custom date fields
|--------------------------------------------------------------------------
| Set the fields that will be cast to Carbon dates
*/
'dates' => [
],
/*
|--------------------------------------------------------------------------
| Custom casted fields
|--------------------------------------------------------------------------
| Set the fields that will be casted by Eloquent
*/
'casts' => [
],
/*
|--------------------------------------------------------------------------
| Dynamic relations
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment