@param  \Illuminate\Contracts\Auth\Authenticatable|null  $user
@param  \Illuminate\Contracts\Auth\Access\Gate  $gate
@param  string  $ability
@param  array  $args
@return void

@throws \Nuwave\Lighthouse\Exceptions\AuthorizationException
559810
protected function authorize($user, Gate $gate, string $ability, array $args): void
    {
        $can = $gate->forUser($user)->check($ability, $args);

        if (! $can) {
            throw new AuthorizationException(
                "You are not authorized to access {$this->definitionNode->name->value}"
            );
        }
    }
520797
public function add_role( $args, $assoc_args ) {
		$user = $this->fetcher->get_check( $args[0] );

		$role = $args[1];

		self::validate_role( $role );

		$user->add_role( $role );

		WP_CLI::success( "Added '{$role}' role for {$user->user_login} ({$user->ID})." );
	}
32589
public function allows($ability, $arguments = [])
    {
        if (!is_array($arguments)) {
            $arguments = [ $arguments ];
        }

        array_unshift($arguments, $ability);
        return call_user_func_array([ $this->gatekeeper, 'allows'], $arguments);
    }
1243207
public function authorizeAtGate(GateInterface $gate, $ability, $arguments)
    {
        try {
            return $gate->authorize($ability, $arguments);
        }
        catch (UnauthorizedException $e) {
            $exception = $this->createGateUnauthorizedException($ability, $arguments, $e->getMessage(), $e);

            throw $exception;
        }
    }
664005
protected function buildAbilityCallback($ability, $callback)
    {
        return function () use ($ability, $callback) {
            if (Str::contains($callback, '@')) {
                [$class, $method] = Str::parseCallback($callback);
            } else {
                $class = $callback;
            }

            $policy = $this->resolvePolicy($class);

            $arguments = func_get_args();

            $user = array_shift($arguments);

            $result = $this->callPolicyBefore(
                $policy, $user, $ability, $arguments
            );

            if (! is_null($result)) {
                return $result;
            }

            return isset($method)
                    ? $policy->{$method}(...func_get_args())
                    : $policy(...func_get_args());
        };
    }
26347
public function authenticateUserAndHandleSuccess(UserInterface $user, Request $request, AuthenticatorInterface $authenticator, string $providerKey): ?Response
    {
        // create an authenticated token for the User
        $token = $authenticator->createAuthenticatedToken($user, $providerKey);
        // authenticate this in the system
        $this->authenticateWithToken($token, $request, $providerKey);

        // return the success metric
        return $this->handleAuthenticationSuccess($token, $request, $authenticator, $providerKey);
    }
235795
protected function runAfterCallback($authority, $ability, $result, $arguments = null)
    {
        if (! is_null($result)) {
            return $result;
        }

        if ($this->slot != 'after') {
            return;
        }

        if (count($arguments) > 2) {
            return;
        }

        $model = isset($arguments[0]) ? $arguments[0] : null;

        return $this->checkAtClipboard($authority, $ability, $model);
    }
119759
protected static function hasTypeAccessBelongs(array $capabilities, array $user, array $url): bool
    {
        $entity = static::getEntityFromUrl($url);

        $userGroups = static::getUserGroups($user);

        foreach ($capabilities as $capability) {
            if (!static::hasAccessInCapabilities($capability->getName(), $user['id'])) {
                continue;
            }

            // if url does not include an id and user has belongs to capability
            // access, to current module action, allow him access. (index action)
            if (empty($entity)) {
                return true;
            }

            // if url includes an id, check capability's field value from the entity
            // against current user id and if they match allow him access. (view, edit actions etc)
            $field = $capability->getField();

            foreach (array_keys($userGroups) as $id) {
                if ($entity->get($field) === $id) {
                    return true;
                }
            }
        }

        return false;
    }
762566
protected function can(string $action, ?Authorizable $user = null): bool
    {
        return ! \is_null($user)
                    ? $this->acl->canAs($user, $action)
                    : $this->acl->can($action);
    }
561481
public function can($capability, $args = null)
	{
		if (!$this->user())
		{
			return false;
		}

		$userCan = false;

		if (is_array($capability))
		{
			foreach ($capability as $cap)
			{
				// if only one capability is accepted
				if (current_user_can($cap, $args) === true)
				{
					$userCan = true;
					break;
				}
			}
		}
		else
		{
			$userCan = current_user_can($capability, $args);
		}

		return $userCan;
	}
1052277
public function doAuthentication(UserInterface $user)
    {
        /** @var \PServerCore\Entity\Repository\User $repository */
        $repository = $this->entityManager->getRepository($this->getUserEntityClassName());

        // fix if we have a proxy we don´t have a valid entity, so we have to clear before we can create a new select
        $username = $user->getUsername();
        $repository->clear();

        $userNew = $repository->getUser4UserName($username);

        $authService = $this->getAuthService();

        $authService->getStorage()->write($userNew);
    }
999634
protected function callBeforeCallbacks($user, $ability, array $arguments)
    {
        foreach ($this->beforeCallbacks as $before) {
            if (! $this->canBeCalledWithUser($user, $before)) {
                continue;
            }

            if (! is_null($result = $before($user, $ability, $arguments))) {
                return $result;
            }
        }
    }
554004
public function check($ability, $arguments = [])
    {
        if (!is_array($arguments)) {
            $arguments = [ $arguments ];
        }

        array_unshift($arguments, $ability);
        return call_user_func_array([ $this->gatekeeper, 'check'], $arguments);
    }
821815
public function authorize($ability, $arguments = array())
    {
        if (! is_array($arguments)) {
            $arguments = array_slice(func_get_args(), 1);
        }

        $result = $this->raw($ability, $arguments);

        if ($result instanceof Response) {
            return $result;
        }

        return $result ? $this->allow() : $this->deny();
    }
1185455
protected static function hasTypeAccessOwner(array $capabilities, array $user, array $url): bool
    {
        $entity = static::getEntityFromUrl($url);

        foreach ($capabilities as $capability) {
            if (!static::hasAccessInCapabilities($capability->getName(), $user['id'])) {
                continue;
            }

            // if url does not include an id and user has owner capability
            // access, to current module action, allow him access. (index action)
            if (empty($entity)) {
                return true;
            }

            // if url includes an id, check capability's field value from the entity
            // against current user id and if they match allow him access. (view, edit actions etc)
            $field = $capability->getField();

            if ($entity->get($field) === $user['id']) {
                return true;
            }
        }

        return false;
    }
854228
public function cannot($args = null)
    {
        $args = is_array($args) ? $args : func_get_args();
        return call_user_func_array([$this->getCurrentAuthority(), 'cannot'], $args);
    }
262183
public function findByAbilitiesAndStatus(User $user, array $abilities, $status)
    {
        return $this->createQueryBuilder('ap')
            ->select('ap')
            ->where('ap.user = :user')
            ->andWhere('ap.ability IN (:abilities)')
            ->andWhere('ap.status = :status')
            ->orderBy('ap.id')
            ->setParameters([
                ':user' => $user,
                ':abilities' => $abilities,
                ':status' => $status,
            ])
            ->getQuery()
            ->getResult();
    }