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
...@@ -33,6 +33,7 @@ return [ ...@@ -33,6 +33,7 @@ return [
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| 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, 'presenter' => \Modules\User\Presenters\UserPresenter::class,
/* /*
...@@ -44,8 +45,8 @@ return [ ...@@ -44,8 +45,8 @@ return [
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| The default role for new user registrations | The default role for new user registrations
| Default: User
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Default: User
*/ */
'default_role' => 'User', 'default_role' => 'User',
/* /*
...@@ -61,6 +62,22 @@ return [ ...@@ -61,6 +62,22 @@ return [
'first_name', 'first_name',
'last_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 | Dynamic relations
......
...@@ -32,15 +32,21 @@ class User extends EloquentUser implements UserInterface ...@@ -32,15 +32,21 @@ class User extends EloquentUser implements UserInterface
{ {
$this->loginNames = config('asgard.user.config.login-columns'); $this->loginNames = config('asgard.user.config.login-columns');
$this->fillable = config('asgard.user.config.fillable'); $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); parent::__construct($attributes);
} }
/** /**
* Checks if a user belongs to the given Role ID * @inheritdoc
* @param int $roleId
* @return bool
*/ */
public function hasRoleId($roleId) public function hasRoleId($roleId)
{ {
...@@ -48,9 +54,15 @@ class User extends EloquentUser implements UserInterface ...@@ -48,9 +54,15 @@ class User extends EloquentUser implements UserInterface
} }
/** /**
* Checks if a user belongs to the given Role Name * @inheritdoc
* @param string $name */
* @return bool public function hasRoleSlug($slug)
{
return $this->roles()->whereSlug($slug)->count() >= 1;
}
/**
* @inheritdoc
*/ */
public function hasRoleName($name) public function hasRoleName($name)
{ {
...@@ -58,8 +70,7 @@ class User extends EloquentUser implements UserInterface ...@@ -58,8 +70,7 @@ class User extends EloquentUser implements UserInterface
} }
/** /**
* Check if the current user is activated * @inheritdoc
* @return bool
*/ */
public function isActivated() public function isActivated()
{ {
...@@ -79,8 +90,7 @@ class User extends EloquentUser implements UserInterface ...@@ -79,8 +90,7 @@ class User extends EloquentUser implements UserInterface
} }
/** /**
* Get the first available api key * @inheritdoc
* @return string
*/ */
public function getFirstApiKey() public function getFirstApiKey()
{ {
...@@ -110,9 +120,7 @@ class User extends EloquentUser implements UserInterface ...@@ -110,9 +120,7 @@ class User extends EloquentUser implements UserInterface
} }
/** /**
* Check if the user has access to the given permission name * @inheritdoc
* @param string $permission
* @return boolean
*/ */
public function hasAccess($permission) public function hasAccess($permission)
{ {
......
...@@ -4,6 +4,13 @@ namespace Modules\User\Entities; ...@@ -4,6 +4,13 @@ namespace Modules\User\Entities;
interface UserInterface 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 * Checks if a user belongs to the given Role ID
* @param int $roleId * @param int $roleId
...@@ -11,6 +18,13 @@ interface UserInterface ...@@ -11,6 +18,13 @@ interface UserInterface
*/ */
public function hasRoleId($roleId); 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 * Checks if a user belongs to the given Role Name
* @param string $name * @param string $name
......
...@@ -33,6 +33,7 @@ return [ ...@@ -33,6 +33,7 @@ return [
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| 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, 'presenter' => \Modules\User\Presenters\UserPresenter::class,
/* /*
...@@ -44,8 +45,8 @@ return [ ...@@ -44,8 +45,8 @@ return [
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| The default role for new user registrations | The default role for new user registrations
| Default: User
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Default: User
*/ */
'default_role' => 'User', 'default_role' => 'User',
/* /*
...@@ -61,6 +62,22 @@ return [ ...@@ -61,6 +62,22 @@ return [
'first_name', 'first_name',
'last_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 | 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