Add user roles system

This commit is contained in:
2026-01-16 23:14:47 +01:00
parent c0be2e294a
commit e5ef197ed6
21 changed files with 206 additions and 85 deletions

View File

@@ -3,6 +3,8 @@
namespace App\Models;
//use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Enums\UserRole;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
@@ -25,7 +27,6 @@ class User extends Authenticatable
'email',
'password',
'locale',
'is_banned',
// Discord
'discord_id',
'discord_avatar',
@@ -54,7 +55,7 @@ class User extends Authenticatable
'name' => 'string',
'email' => 'string',
'locale' => 'string',
'roles' => 'json',
'roles' => 'array',
'tag_blacklist' => 'array',
// Discord
'discord_id' => 'integer',
@@ -119,4 +120,44 @@ class User extends Authenticatable
return asset('images/default-avatar.webp');
}
/**
* Check if user has a specific role
*/
public function hasRole(UserRole $role): bool
{
return in_array($role->value, $this->roles ?? [], true);
}
/**
* Add Role to User
*/
public function addRole(UserRole $role): void
{
if ($this->hasRole($role)) {
return;
}
// Get all current roles
$roles = $this->roles ?? [];
// Add new role
$roles[] = $role->value;
$this->roles = $roles;
$this->save();
}
/**
* Remove Role from User
*/
public function removeRole(UserRole $role): void
{
if (!$this->hasRole($role)) {
return;
}
$this->roles = array_diff($this->roles, [$role->value]);
$this->save();
}
}