Files
projectsend/app/Modules/Identity/Models/Role.php
T
ignacionelson 495f3ae471 Let each role, and each person, choose where they land after signing in
A role now has a start page: the dashboard, files, upload, groups,
clients or the activity log (the last two for staff only). Anyone can
override their role's choice in their profile. The administrator role
takes a start page too, while everything else about it stays locked.

A choice is only used if the account can open that page now. Otherwise
the next one down is tried, ending at the dashboard, so a permission
removed later never lands somebody on a 403. A role cannot be saved
with a start page its own permissions block. A link followed before
signing in still wins, and a waiting getting-started or what's-new page
still goes first.

Applies to password, two-factor and provider sign-ins, and to the site
root for someone already signed in. StartPageTest opens every page for
real, with and without its permission, so the enum cannot drift from
the routes.

Requested by @Zodiac1978 in #1777.
2026-09-13 15:05:40 -03:00

68 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Identity\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use RuntimeException;
/**
* @property int $id
* @property string $name
* @property bool $is_system
* @property bool $is_administrator
* @property bool $client_scoped
* @property string|null $start_page a StartPage value; see StartPages
* @property-read int $users_count
* @property-read int $permissions_count
*/
class Role extends Model
{
protected $guarded = [];
protected static function booted(): void
{
// The built-in roles must always exist: they can never be
// deleted, renamed, or demoted from system status.
static::deleting(function (Role $role): void {
if ($role->is_system) {
throw new RuntimeException('System roles cannot be deleted.');
}
});
static::updating(function (Role $role): void {
if ((bool) $role->getOriginal('is_system') && ($role->isDirty('name') || $role->isDirty('is_system'))) {
throw new RuntimeException('System roles cannot be renamed or demoted.');
}
});
}
protected function casts(): array
{
return [
'is_system' => 'boolean',
'is_administrator' => 'boolean',
'client_scoped' => 'boolean',
];
}
/**
* @return HasMany<RolePermission, $this>
*/
public function permissions(): HasMany
{
return $this->hasMany(RolePermission::class);
}
/**
* @return HasMany<User, $this>
*/
public function users(): HasMany
{
return $this->hasMany(User::class);
}
}