70 lines
2.2 KiB
PHP
70 lines
2.2 KiB
PHP
<?php
|
|
|
|
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).
|
|
*
|
|
* 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');
|
|
}
|
|
|
|
public function isManagerSuperAdmin(): bool
|
|
{
|
|
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 ! $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()) === [];
|
|
}
|
|
}
|