Laratrust\Traits\LaratrustUserTrait::hasRole PHP Method

hasRole() public method

Checks if the user has a role by its name.
public hasRole ( string | array $name, string | boolean $group = null, boolean $requireAll = false ) : boolean
$name string | array Role name or array of role names.
$group string | boolean Group name or requiredAll roles.
$requireAll boolean All roles in the array are required.
return boolean
    public function hasRole($name, $group = null, $requireAll = false)
    {
        $requireAll = is_bool($group) ? $group : $requireAll;
        $group = is_bool($group) ? null : $group;
        if (is_array($name)) {
            if (empty($name)) {
                return true;
            }
            foreach ($name as $roleName) {
                $hasRole = $this->hasRole($roleName, $group);
                if ($hasRole && !$requireAll) {
                    return true;
                } elseif (!$hasRole && $requireAll) {
                    return false;
                }
            }
            // If we've made it this far and $requireAll is FALSE, then NONE of the roles were found
            // If we've made it this far and $requireAll is TRUE, then ALL of the roles were found.
            // Return the value of $requireAll;
            return $requireAll;
        }
        if (!is_null($group)) {
            $group = call_user_func_array([Config::get('laratrust.group'), 'where'], ['name', $group])->first();
            $group = is_null($group) ? $group : $group->getKey();
        }
        foreach ($this->cachedRoles() as $role) {
            if ($role->name == $name && $role->pivot->group_id == $group) {
                return true;
            }
        }
        return false;
    }