From 012ca9ad1fdaed17c77afff8619f5938e3dfc91e Mon Sep 17 00:00:00 2001 From: Samuel De Backer Date: Fri, 20 Aug 2021 10:59:16 +0200 Subject: [PATCH] Update Role.php --- src/Models/Role.php | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Models/Role.php b/src/Models/Role.php index 80e6997..ab6185c 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -6,6 +6,7 @@ use Laracasts\Presenter\PresentableTrait; use Spatie\Permission\Contracts\Role as RoleContract; use Spatie\Permission\Exceptions\GuardDoesNotMatch; +use Spatie\Permission\Exceptions\RoleAlreadyExists; use Spatie\Permission\Exceptions\RoleDoesNotExist; use Spatie\Permission\Guard; use Spatie\Permission\Traits\HasPermissions; @@ -61,7 +62,7 @@ public function permissions(): BelongsToMany /** * A role belongs to some users of the model associated with its guard. */ - public function users(): MorphToMany + public function users(): BelongsToMany { return $this->morphedByMany( getModelForGuard($this->attributes['guard_name']), @@ -75,7 +76,7 @@ public function users(): MorphToMany /** * Find a role by its name and guard name. * - * @param string|null $guardName + * @param null|string $guardName * * @throws \Spatie\Permission\Exceptions\RoleDoesNotExist * @@ -110,7 +111,7 @@ public static function findById(int $id, $guardName = null): RoleContract /** * Find or create role by its name (and optionally guardName). * - * @param string|null $guardName + * @param null|string $guardName */ public static function findOrCreate(string $name, $guardName = null): RoleContract { @@ -128,7 +129,7 @@ public static function findOrCreate(string $name, $guardName = null): RoleContra /** * Determine if the user may perform the given permission. * - * @param string|Permission $permission + * @param Permission|string $permission * * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch */ @@ -155,6 +156,32 @@ public function hasPermissionTo($permission): bool return $this->permissions->contains('id', $permission->id); } + /** + * Fill model from array. + */ + protected function fillModelFromArray(array $attributes) + { + $this->attributes = $attributes; + if (isset($attributes['id'])) { + $this->exists = true; + $this->original['id'] = $attributes['id']; + } + + return $this; + } + + /** + * Get model from array. + * + * @return \Spatie\Permission\Contracts\Role + */ + public static function getModelFromArray(array $attributes): ?RoleContract + { + $roles = new static(); + + return $roles->fillModelFromArray($attributes); + } + public function uri($locale = null): string { return url('/');