Created
October 6, 2015 16:15
-
-
Save 4lun/d382affc54e03b78c56d to your computer and use it in GitHub Desktop.
Basic role implementation for Laravel 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
use Illuminate\Support\Str; | |
use App\Role; | |
/** | |
* Adds roles to a model | |
* Note: requires a Role model pivot table {model}_roles and a role table with | |
* the columns [id, key, name] | |
* | |
* @param array $roles | |
*/ | |
trait HasRoles | |
{ | |
/** | |
* Filter based on role. | |
* | |
* @param string|array $roles | |
* | |
* @return \Illuminate\Database\Eloquent\Builder | |
*/ | |
public function scopeRole($query, $roles) | |
{ | |
foreach ((array) $roles as $role) { | |
$query = $query->whereHas('roles', function ($query) use ($role) { | |
$query->where('key', $role); | |
}); | |
} | |
return $query; | |
} | |
/** | |
* Returns all roles. | |
* | |
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | |
*/ | |
public function roles() | |
{ | |
return $this->belongsToMany( | |
'App\Role', | |
Str::snake(class_basename($this)).'_role' | |
)->withTimestamps(); | |
} | |
/** | |
* Return array of roles. | |
* | |
* @return array | |
*/ | |
public function getRoleKeys() | |
{ | |
return array_pluck($this->roles->toArray(), 'key'); | |
} | |
/** | |
* Returns true if model matches passed role(s). | |
* | |
* @param string|array $roles | |
* | |
* @return bool | |
*/ | |
public function hasRole($roles) | |
{ | |
$diff = array_diff( | |
(array) $roles, | |
$this->getRoleKeys() | |
); | |
// True only if model has ALL requested roles | |
return (count($diff) === 0); | |
} | |
/** | |
* Iterates over the roles that match the passed array and passes each role | |
* to the passed $callback closure. | |
* | |
* @param string|array $roles Name or list of role keys. | |
* @param \Closure $callback Closure to call on each matching role. | |
* | |
* @return Illuminate\Database\Eloquent\Model | |
*/ | |
private function roleIterator($roles, $callback) | |
{ | |
$roles = Role::whereIn('key', (array) $roles)->get(); | |
foreach ($roles as $role) { | |
$callback($role); | |
} | |
$this->load('roles'); | |
return $this; | |
} | |
/** | |
* Add role(s) to model. | |
* | |
* @param string|array $roles Name or list of role keys. | |
* | |
* @return Illuminate\Database\Eloquent\Model | |
*/ | |
public function addRole($roles) | |
{ | |
return $this->roleIterator($roles, function ($role) { | |
$this->roles()->attach($role->id); | |
}); | |
} | |
/** | |
* Remove role(s) from model. | |
* | |
* @param string|array $roles Name or list of role keys. | |
* | |
* @return Illuminate\Database\Eloquent\Model | |
*/ | |
public function removeRole($roles) | |
{ | |
return $this->roleIterator($roles, function ($role) { | |
$this->roles()->detach($role->id); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment