X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/257a5a23ecaf7ce779969d575ff8a0b976181d13..refs/pull/3918/head:/app/Auth/Role.php diff --git a/app/Auth/Role.php b/app/Auth/Role.php index c8bb47e45..b293d1af2 100644 --- a/app/Auth/Role.php +++ b/app/Auth/Role.php @@ -1,26 +1,48 @@ -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); } @@ -28,17 +50,23 @@ class Role extends Model /** * The RolePermissions that belong to the role. */ - public function permissions() + public function permissions(): BelongsToMany + { + return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id'); + } + + /** + * Get the entity permissions assigned to this role. + */ + public function entityPermissions(): HasMany { - return $this->belongsToMany(Permissions\RolePermission::class, 'permission_role', 'role_id', 'permission_id'); + return $this->hasMany(EntityPermission::class); } /** * 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) { @@ -46,53 +74,47 @@ 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::where('system_name', '=', $roleName)->first(); + return static::query()->where('system_name', '=', $systemName)->first(); } /** - * 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}"; } }