for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace NukaCode\Users\Traits;
use NukaCode\Users\Models\Permission;
use NukaCode\Users\Models\Role;
trait HasRoles
{
/**
* A user may have multiple roles.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles()
return $this->belongsToMany(Role::class, 'acl_role_user');
}
* Assign the given role to the user.
* @param string $role
* @return mixed
* @throws \Exception
public function assignRole($role)
if (Role::count() === 0) {
throw new \Exception('No roles have been created.');
return $this->roles()->save(
Role::whereName($role)->firstOrFail()
);
* Determine if the user has the given role.
* @param mixed $role
* @return bool
public function hasRole($role)
if (is_string($role)) {
return $this->roles->contains('name', $role);
roles
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
return ! ! $role->intersect($this->roles)->count();
* Determine if the user may perform the given permission.
* @param Permission $permission
public function hasPermission(Permission $permission)
return $this->hasRole($permission->roles);
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: