*/ 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 */ 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()) === []; } }