Skip to content
This repository has been archived by the owner on May 19, 2020. It is now read-only.

Commit

Permalink
Added methods to sync roles through permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidesu committed Jun 3, 2016
1 parent 53ad482 commit f9330f8
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,53 @@ public function roles()
{
return $this->belongsToMany('\Caffeinated\Shinobi\Models\Role')->withTimestamps();
}

/**
* Assigns the given role to the permission.
*
* @param integer $roleId
* @return bool
*/
public function assignRole($roleId = null)
{
$roles = $this->roles;

if (! $roles->contains($roleId)) {
return $this->roles()->attach($roleId);
}

return false;
}

/**
* Revokes the given role from the permission.
*
* @param integer $roleId
* @return bool
*/
public function revokeRole($roleId = '')
{
return $this->roles()->detach($roleId);
}

/**
* Syncs the given role(s) with the permission.
*
* @param array $roleIds
* @return bool
*/
public function syncRoles(array $roleIds = array())
{
return $this->roles()->sync($roleIds);
}

/**
* Revokes all roles from the permission.
*
* @return bool
*/
public function revokeAllRoles()
{
return $this->roles()->detach();
}
}

0 comments on commit f9330f8

Please sign in to comment.