1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
*/
namespace MvcCore\Ext\Auths\Basics\User;
use \MvcCore\Ext\Models\Db\Attrs;
/**
* Trait for `\MvcCore\Ext\Auths\Basics\User` class. Trait contains:
* - Instance property `$admin` and `$roles` with their public getters and setters to manipulate with user roles.
* - Method `IsAllowed()` to get allowed permissions from user instance or from user roles.
*/
trait Roles {
/**
* `TRUE` if user is administrator. Administrator has always allowed everything.
* Default value is `FALSE`.
* @column admin
* @var bool
*/
#[Attrs\Column('admin')]
protected $admin = FALSE;
/**
* Array of roles names assigned for current user instance.
* @column roles
* @var \string[]
*/
#[Attrs\Column('roles')]
protected $roles = [];
/**
* Get if user is Administrator. Administrator has always allowed everything.
* @return bool
*/
public function IsAdmin() {
/** @var $this \MvcCore\Ext\Auths\Basics\User */
return $this->admin;
}
/**
* Get if user is Administrator. Administrator has always allowed everything.
* @return bool
*/
public function GetAdmin() {
/** @var $this \MvcCore\Ext\Auths\Basics\User */
return $this->admin;
}
/**
* Set user to Administrator. Administrator has always allowed everything.
* @param bool $admin `TRUE` by default.
* @return \MvcCore\Ext\Auths\Basics\User
*/
public function SetAdmin ($admin = TRUE) {
/** @var $this \MvcCore\Ext\Auths\Basics\User */
$this->admin = (bool) $admin;
return $this;
}
/**
* Return array of user's roles names.
* @return \string[]
*/
public function & GetRoles () {
/** @var $this \MvcCore\Ext\Auths\Basics\User */
return $this->roles;
}
/**
* Set new user's roles or roles names.
* @param \string[]|\MvcCore\Ext\Auths\Basics\Role[] $rolesOrRolesNames
* @return \MvcCore\Ext\Auths\Basics\User
*/
public function SetRoles ($rolesOrRolesNames = []) {
/** @var $this \MvcCore\Ext\Auths\Basics\User */
$this->roles = [];
foreach ($rolesOrRolesNames as $roleOrRoleName)
$this->roles[] = static::getRoleName($roleOrRoleName);
return $this;
}
/**
* Add user role or role name.
* @param string|\MvcCore\Ext\Auths\Basics\Role $roleOrRoleName
* @throws \InvalidArgumentException
* @return \MvcCore\Ext\Auths\Basics\User
*/
public function AddRole ($roleOrRoleName) {
/** @var $this \MvcCore\Ext\Auths\Basics\User */
$roleName = static::getRoleName($roleOrRoleName);
if (!in_array($roleName, $this->roles, TRUE))
$this->roles[] = $roleName;
return $this;
}
/**
* Get `TRUE` if user has already assigned role or role name.
* @param string|\MvcCore\Ext\Auths\Basics\Role $roleOrRoleName
* @throws \InvalidArgumentException
* @return bool
*/
public function HasRole ($roleOrRoleName) {
/** @var $this \MvcCore\Ext\Auths\Basics\User */
$roleName = static::getRoleName($roleOrRoleName);
return in_array($roleName, $this->roles, TRUE);
}
/**
* Remove user role or role name from user roles.
* @param string|\MvcCore\Ext\Auths\Basics\Role $roleOrRoleName
* @throws \InvalidArgumentException
* @return \MvcCore\Ext\Auths\Basics\User
*/
public function RemoveRole ($roleOrRoleName) {
/** @var $this \MvcCore\Ext\Auths\Basics\User */
$roleName = static::getRoleName($roleOrRoleName);
$position = array_search($roleName, $this->roles);
if ($position !== FALSE) array_splice($this->roles, $position, 1);
return $this;
}
/**
* Get role name from given role instance or given role name.
* @param string|\MvcCore\Ext\Auths\Basics\Role $roleOrRoleName
* @throws \InvalidArgumentException
* @return string
*/
protected static function getRoleName ($roleOrRoleName) {
/** @var $this \MvcCore\Ext\Auths\Basics\User */
if (is_string($roleOrRoleName)) {
return $roleOrRoleName;
} else if ($roleOrRoleName instanceof \MvcCore\Ext\Auths\Basics\IRole) {
return $roleOrRoleName->GetName();
} else {
throw new \InvalidArgumentException(
'['.get_class()."] Given argument `{$roleOrRoleName}` doesn't "
."implement interface `\\MvcCore\\Ext\\Auths\\Basics\\IRole` "
."or it's not string with role name."
);
}
}
}