I hasRole trait
This commit is contained in:
parent
318e6a90ac
commit
4d2252d360
|
|
@ -2,8 +2,12 @@
|
|||
|
||||
namespace Blax\Roles\Traits;
|
||||
|
||||
use Blax\Roles\Models\Role;
|
||||
|
||||
trait HasRoles
|
||||
{
|
||||
use HasPermissions;
|
||||
|
||||
/**
|
||||
* Get all roles for the user.
|
||||
*
|
||||
|
|
@ -28,4 +32,110 @@ trait HasRoles
|
|||
{
|
||||
return $this->roles()->where('slug', $roleSlug)->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns the role to the memberable
|
||||
*
|
||||
* @param int|string|Role $role
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function assignRole(string|Role $role)
|
||||
{
|
||||
if (is_string($role) && !is_numeric($role)) {
|
||||
$role = config('roles.models.role', \Blax\Roles\Models\Role::class)::where('slug', $role)->first();
|
||||
} elseif (is_numeric($role)) {
|
||||
$role = config('roles.models.role', \Blax\Roles\Models\Role::class)::find($role);
|
||||
} elseif ($role instanceof Role) {
|
||||
$this->roles()->attach($role);
|
||||
} else {
|
||||
throw new \InvalidArgumentException('Role must be a string, numeric ID, or an instance of Role.');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the role from the memberable
|
||||
*
|
||||
* @param int|string|Role $role
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function removeRole(string|Role $role)
|
||||
{
|
||||
if (is_string($role) && !is_numeric($role)) {
|
||||
$role = config('roles.models.role', \Blax\Roles\Models\Role::class)::where('slug', $role)->first();
|
||||
} elseif (is_numeric($role)) {
|
||||
$role = config('roles.models.role', \Blax\Roles\Models\Role::class)::find($role);
|
||||
} elseif ($role instanceof Role) {
|
||||
$this->roles()->detach($role);
|
||||
} else {
|
||||
throw new \InvalidArgumentException('Role must be a string, numeric ID, or an instance of Role.');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncs the roles for the memberable
|
||||
*
|
||||
* @param array $roles
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function syncRoles(array $roles)
|
||||
{
|
||||
$roleIds = [];
|
||||
foreach ($roles as $role) {
|
||||
if (is_string($role) && !is_numeric($role)) {
|
||||
$roleModel = config('roles.models.role', \Blax\Roles\Models\Role::class)::where('slug', $role)->first();
|
||||
} elseif (is_numeric($role)) {
|
||||
$roleModel = config('roles.models.role', \Blax\Roles\Models\Role::class)::find($role);
|
||||
} elseif ($role instanceof Role) {
|
||||
$roleModel = $role;
|
||||
}
|
||||
|
||||
if ($roleModel instanceof Role) {
|
||||
$roleIds[] = $roleModel->id;
|
||||
}
|
||||
}
|
||||
|
||||
$this->roles()->sync($roleIds);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the memberable has any of the given roles
|
||||
*
|
||||
* @param array $roles
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAnyRole(array $roles): bool
|
||||
{
|
||||
foreach ($roles as $role) {
|
||||
if ($this->hasRole($role)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Checks if the memberable has all of the given roles
|
||||
*
|
||||
* @param array $roles
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAllRoles(array $roles): bool
|
||||
{
|
||||
foreach ($roles as $role) {
|
||||
if (!$this->hasRole($role)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ namespace Blax\Roles\Tests\Unit;
|
|||
|
||||
use Orchestra\Testbench\PHPUnit\TestCase;
|
||||
|
||||
// TODO complete this WIP
|
||||
class PermissionTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
|
|
|
|||
Loading…
Reference in New Issue