Laratrust\Traits\LaratrustUserTrait::can PHP Method

can() public method

Check if user has a permission by its name.
public can ( string | array $permission, string | boolean $group = null, boolean $requireAll = false ) : boolean
$permission string | array Permission string or array of permissions.
$group string | boolean Group name or requiredAll roles.
$requireAll boolean All permissions in the array are required.
return boolean
    public function can($permission, $group = null, $requireAll = false)
    {
        $requireAll = is_bool($group) ? $group : $requireAll;
        $group = is_bool($group) ? null : $group;
        if (is_array($permission)) {
            if (empty($permission)) {
                return true;
            }
            foreach ($permission as $permName) {
                $hasPerm = $this->can($permName, $group);
                if ($hasPerm && !$requireAll) {
                    return true;
                } elseif (!$hasPerm && $requireAll) {
                    return false;
                }
            }
            // If we've made it this far and $requireAll is FALSE, then NONE of the perms were found
            // If we've made it this far and $requireAll is TRUE, then ALL of the perms 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->cachedPermissions() as $perm) {
            if (str_is($permission, $perm->name)) {
                return true;
            }
        }
        foreach ($this->cachedRoles() as $role) {
            // Validate against the Permission table
            if ($role->pivot->group_id != $group) {
                continue;
            }
            foreach ($role->cachedPermissions() as $perm) {
                if (str_is($permission, $perm->name)) {
                    return true;
                }
            }
        }
        return false;
    }