Users module: accounts, groups, permissions with needs, manager:create-user

This commit is contained in:
Matthijs Iriks committed 2026-12-04 01:25:29 +01:00
1 parent 08289db744
commit f86f5aea28
25 files changed
+744 -58

No files matched your search

+40 -5
View File
@@ -4,15 +4,18 @@ namespace IriksIT\Manager\Concerns;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use IriksIT\Manager\Models\Group;
use IriksIT\Manager\Support\ModuleRegistry;
/**
* Put this on the User model of the host app. Needs the Users module (its migration
* adds `manager_super_admin` to the users table and creates the groups).
* Put this on the User model of the host app (needs the Users module).
*
* A super admin may do everything. Everyone else may do what any of their groups may do.
* A super admin may do everything of the manager. Everyone else may do what any of their
* groups allows. `is_super_admin` is never fillable and no form sets it: only
* `manager:create-user` makes one. Keep it out of `$fillable` in the host model.
*/
trait HasManagerAccess
{
/** @return BelongsToMany<Group, $this> */
public function managerGroups(): BelongsToMany
{
return $this->belongsToMany(Group::class, config('manager.table_prefix').'group_user', 'user_id', 'group_id');
@@ -20,15 +23,47 @@ trait HasManagerAccess
public function isManagerSuperAdmin(): bool
{
return (bool) $this->manager_super_admin;
return (bool) $this->getAttribute('is_super_admin');
}
/**
* Everything this account may do in the manager.
*
* @return list<string>
*/
public function managerPermissions(): array
{
$registry = app(ModuleRegistry::class);
if ($this->isManagerSuperAdmin()) {
return $registry->permissionValues();
}
return $registry->complete($this->managerGroups->flatMap(fn (Group $group) => $group->allowed())->all());
}
public function hasManagerPermission(string $permission): bool
{
return in_array($permission, $this->managerPermissions(), true);
}
/**
* Whether this account may do everything $other may. Someone who manages accounts only
* touches accounts that stay within their own rights; otherwise a new password for a
* mightier account would be a way up.
*/
public function managerOutranks(self $other): bool
{
if ($this->isManagerSuperAdmin()) {
return true;
}
return $this->managerGroups->contains(fn (Group $group) => in_array($permission, $group->permissions ?? [], true));
return ! $other->isManagerSuperAdmin() && array_diff($other->managerPermissions(), $this->managerPermissions()) === [];
}
/** Whether this account may put someone in $group: only when the group gives nothing it lacks itself. */
public function managerMayGive(Group $group): bool
{
return $this->isManagerSuperAdmin() || array_diff($group->allowed(), $this->managerPermissions()) === [];
}
}