Skip to content

Commit

Permalink
Add permissions to role
Browse files Browse the repository at this point in the history
  • Loading branch information
denismitr committed Apr 15, 2017
1 parent 066ab1e commit 49995a0
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,48 @@ public function permissions()
}


/**
* Get users who belong to this role
*
* @return Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users()
{
return $this->belongsToMany(User::class);
}

/**
* Give role a permission
*
* @param string $permissions
* @return $this
*/
public function givePermissionTo(...$permissions)
{
foreach ($permissions as $key => $permission) {
if ($this->hasPermissionTo($permission)) {
unset($permissions[$key]);
} else {
$permissions[$key] = Permission::fromName($permission);
}
}

$this->permissions()->saveMany($permissions);

$this->load('permissions');

return $this;
}


/**
* Verify if role has a permission
*
* @param string $permission
* @return bool
*/
public function hasPermissionTo(string $permission)
{
return (bool) $this->permissions->where('name', $permission)->count();
}
}

0 comments on commit 49995a0

Please sign in to comment.