X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/919660678bec2b94eaa84ac60d0313f5ef07dfb7..refs/pull/3298/head:/app/Auth/Role.php diff --git a/app/Auth/Role.php b/app/Auth/Role.php index b32954767..51b2ce301 100644 --- a/app/Auth/Role.php +++ b/app/Auth/Role.php @@ -1,27 +1,47 @@ -belongsToMany(User::class); + return $this->belongsToMany(User::class)->orderBy('name', 'asc'); } /** * Get all related JointPermissions. - * @return \Illuminate\Database\Eloquent\Relations\HasMany */ - public function jointPermissions() + public function jointPermissions(): HasMany { return $this->hasMany(JointPermission::class); } @@ -29,17 +49,15 @@ class Role extends Model /** * The RolePermissions that belong to the role. */ - public function permissions() + public function permissions(): BelongsToMany { - return $this->belongsToMany(Permissions\RolePermission::class, 'permission_role', 'role_id', 'permission_id'); + return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id'); } /** * Check if this role has a permission. - * @param $permissionName - * @return bool */ - public function hasPermission($permissionName) + public function hasPermission(string $permissionName): bool { $permissions = $this->getRelationValue('permissions'); foreach ($permissions as $permission) { @@ -47,53 +65,66 @@ class Role extends Model return true; } } + return false; } /** * Add a permission to this role. - * @param \BookStack\Auth\Permissions\RolePermission $permission */ - public function attachPermission(Permissions\RolePermission $permission) + public function attachPermission(RolePermission $permission) { $this->permissions()->attach($permission->id); } /** * Detach a single permission from this role. - * @param \BookStack\Auth\Permissions\RolePermission $permission */ - public function detachPermission(Permissions\RolePermission $permission) + public function detachPermission(RolePermission $permission) { - $this->permissions()->detach($permission->id); + $this->permissions()->detach([$permission->id]); } /** - * Get the role object for the specified role. - * @param $roleName - * @return Role + * Get the role of the specified display name. */ - public static function getRole($roleName) + public static function getRole(string $displayName): ?self { - return static::where('name', '=', $roleName)->first(); + return static::query()->where('display_name', '=', $displayName)->first(); } /** * Get the role object for the specified system role. - * @param $roleName - * @return Role */ - public static function getSystemRole($roleName) + public static function getSystemRole(string $systemName): ?self + { + return static::query()->where('system_name', '=', $systemName)->first(); + } + + /** + * Get all visible roles. + */ + public static function visible(): Collection + { + return static::query()->where('hidden', '=', false)->orderBy('name')->get(); + } + + /** + * Get the roles that can be restricted. + */ + public static function restrictable(): Collection { - return static::where('system_name', '=', $roleName)->first(); + return static::query() + ->where('system_name', '!=', 'admin') + ->orderBy('display_name', 'asc') + ->get(); } /** - * Get all visible roles - * @return mixed + * {@inheritdoc} */ - public static function visible() + public function logDescriptor(): string { - return static::where('hidden', '=', false)->orderBy('name')->get(); + return "({$this->id}) {$this->display_name}"; } }