Skip to content

Commit

Permalink
Update Role.php
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebacker committed Aug 20, 2021
1 parent a971491 commit 012ca9a
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']),
Expand All @@ -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
*
Expand Down Expand Up @@ -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
{
Expand All @@ -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
*/
Expand All @@ -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('/');
Expand Down

0 comments on commit 012ca9a

Please sign in to comment.