2025-06-15 09:56:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
2025-06-16 06:31:14 +00:00
|
|
|
namespace Blax\Roles\Traits;
|
2025-06-15 09:56:28 +00:00
|
|
|
|
2025-06-18 16:49:14 +00:00
|
|
|
use Blax\Roles\Models\Role;
|
|
|
|
|
|
2025-06-15 09:56:28 +00:00
|
|
|
trait HasRoles
|
|
|
|
|
{
|
2025-06-18 16:49:14 +00:00
|
|
|
use HasPermissions;
|
|
|
|
|
|
2025-06-15 09:56:28 +00:00
|
|
|
/**
|
2025-06-15 16:29:50 +00:00
|
|
|
* Get all roles for the user.
|
2025-06-15 09:56:28 +00:00
|
|
|
*
|
2025-06-15 16:29:50 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
2025-06-15 09:56:28 +00:00
|
|
|
*/
|
|
|
|
|
public function roles()
|
|
|
|
|
{
|
2025-06-15 16:29:50 +00:00
|
|
|
return $this->morphToMany(
|
2025-06-16 05:37:35 +00:00
|
|
|
config('roles.models.role', \Blax\Roles\Models\Role::class),
|
2025-06-15 16:29:50 +00:00
|
|
|
'member',
|
2025-06-16 05:37:35 +00:00
|
|
|
config('roles.table_names.role_members', 'role_members')
|
2025-06-15 09:56:28 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-06-15 16:29:50 +00:00
|
|
|
* Check if the user has a specific role.
|
2025-06-15 09:56:28 +00:00
|
|
|
*
|
2025-06-18 16:52:27 +00:00
|
|
|
* @param int|string|Role $role
|
2025-06-15 09:56:28 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2025-06-18 16:52:27 +00:00
|
|
|
public function hasRole(string|Role $role): bool
|
2025-06-15 09:56:28 +00:00
|
|
|
{
|
2025-06-18 16:52:27 +00:00
|
|
|
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) {
|
2025-06-18 17:02:51 +00:00
|
|
|
return $this->roles()->wherePivot('role_id', $role->id)->exists();
|
2025-06-18 16:52:27 +00:00
|
|
|
} else {
|
|
|
|
|
throw new \InvalidArgumentException('Role must be a string, numeric ID, or an instance of Role.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $role
|
2025-06-18 17:02:51 +00:00
|
|
|
? $this->roles()->wherePivot('role_id', $role->id)->exists()
|
2025-06-18 16:52:27 +00:00
|
|
|
: false;
|
2025-06-15 09:56:28 +00:00
|
|
|
}
|
2025-06-18 16:49:14 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assigns the role to the memberable
|
2025-06-19 12:45:06 +00:00
|
|
|
*
|
2025-06-18 16:49:14 +00:00
|
|
|
* @param int|string|Role $role
|
2025-06-19 12:45:06 +00:00
|
|
|
*
|
2025-06-18 16:49:14 +00:00
|
|
|
* @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);
|
2025-06-19 12:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($role instanceof Role) {
|
2025-06-18 16:49:14 +00:00
|
|
|
$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
|
2025-06-19 12:45:06 +00:00
|
|
|
*
|
2025-06-18 16:49:14 +00:00
|
|
|
* @param int|string|Role $role
|
2025-06-19 12:45:06 +00:00
|
|
|
*
|
2025-06-18 16:49:14 +00:00
|
|
|
* @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
|
2025-06-19 12:45:06 +00:00
|
|
|
*
|
2025-06-18 16:49:14 +00:00
|
|
|
* @param array $roles
|
2025-06-19 12:45:06 +00:00
|
|
|
*
|
2025-06-18 16:49:14 +00:00
|
|
|
* @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;
|
2025-06-19 09:34:12 +00:00
|
|
|
} elseif (is_object($role) && isset($role->id)) {
|
|
|
|
|
$roleModel = config('roles.models.role', \Blax\Roles\Models\Role::class)::find($role->id);
|
|
|
|
|
} elseif (is_array($role) && isset($role['id'])) {
|
|
|
|
|
$roleModel = config('roles.models.role', \Blax\Roles\Models\Role::class)::find($role['id']);
|
|
|
|
|
} else {
|
|
|
|
|
throw new \InvalidArgumentException('Role must be a string, numeric ID, or an instance of Role.');
|
2025-06-18 16:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 09:34:12 +00:00
|
|
|
if (@$roleModel instanceof Role) {
|
2025-06-18 16:49:14 +00:00
|
|
|
$roleIds[] = $roleModel->id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->roles()->sync($roleIds);
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if the memberable has any of the given roles
|
2025-06-19 12:45:06 +00:00
|
|
|
*
|
2025-06-18 16:49:14 +00:00
|
|
|
* @param array $roles
|
2025-06-19 12:45:06 +00:00
|
|
|
*
|
2025-06-18 16:49:14 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function hasAnyRole(array $roles): bool
|
|
|
|
|
{
|
|
|
|
|
foreach ($roles as $role) {
|
|
|
|
|
if ($this->hasRole($role)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-06-19 09:34:12 +00:00
|
|
|
|
2025-06-18 16:49:14 +00:00
|
|
|
/**
|
|
|
|
|
* Checks if the memberable has all of the given roles
|
2025-06-19 12:45:06 +00:00
|
|
|
*
|
2025-06-18 16:49:14 +00:00
|
|
|
* @param array $roles
|
2025-06-19 12:45:06 +00:00
|
|
|
*
|
2025-06-18 16:49:14 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function hasAllRoles(array $roles): bool
|
|
|
|
|
{
|
|
|
|
|
foreach ($roles as $role) {
|
|
|
|
|
if (!$this->hasRole($role)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-06-15 09:56:28 +00:00
|
|
|
}
|