當前位置: 首頁>>代碼示例>>PHP>>正文


PHP SecurityContextInterface::getToken方法代碼示例

本文整理匯總了PHP中Symfony\Component\Security\Core\SecurityContextInterface::getToken方法的典型用法代碼示例。如果您正苦於以下問題:PHP SecurityContextInterface::getToken方法的具體用法?PHP SecurityContextInterface::getToken怎麽用?PHP SecurityContextInterface::getToken使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Symfony\Component\Security\Core\SecurityContextInterface的用法示例。


在下文中一共展示了SecurityContextInterface::getToken方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: execute

 /**
  * {@inheritdoc}
  */
 public function execute(BlockContextInterface $blockContext, Response $response = null)
 {
     $criteria = array();
     if ('admin' !== $blockContext->getSetting('mode')) {
         $criteria['customer'] = $this->customerManager->findOneBy(array('user' => $this->securityContext->getToken()->getUser()));
     }
     return $this->renderPrivateResponse($blockContext->getTemplate(), array('context' => $blockContext, 'settings' => $blockContext->getSettings(), 'block' => $blockContext->getBlock(), 'orders' => $this->orderManager->findBy($criteria, array('createdAt' => 'DESC'), $blockContext->getSetting('number'))), $response);
 }
開發者ID:johnulist,項目名稱:ecommerce-1,代碼行數:11,代碼來源:RecentOrdersBlockService.php

示例2: handle

 /**
  * @param GetResponseEvent $event
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if (!$request->headers->has('cookie')) {
         return;
     }
     if (strstr($request->headers->get('cookie'), 'SimpleSAMLAuthToken') === false) {
         return;
     }
     if (!$request->query->has('csrf-token')) {
         $this->logger->notice('Ssp Firewall: Auth Token cookie but no CSRF Token');
         return;
     }
     $csrfToken = $request->query->getAlnum('csrf-token');
     if (!$this->csrfProvider->isCsrfTokenValid('api', $csrfToken)) {
         $this->logger->notice('Ssp Firewall: Invalid CSRF token for api use: ' . $csrfToken);
         return;
     }
     try {
         $authToken = $this->authenticationManager->authenticate(new SspToken());
         $this->securityContext->setToken($authToken);
     } catch (AuthenticationException $failed) {
         $this->logger->warning('Ssp Firewall: failed:' . $failed->getMessage());
         $token = $this->securityContext->getToken();
         if ($token instanceof SspToken) {
             $this->securityContext->setToken(null);
         }
         return;
     }
 }
開發者ID:baszoetekouw,項目名稱:janus,代碼行數:33,代碼來源:SspListener.php

示例3: getUser

 /**
  * @return \Symfony\Component\Security\Core\User\UserInterface
  */
 private function getUser()
 {
     if (is_null($this->user)) {
         $this->user = $this->securityContext->getToken()->getUser();
     }
     return $this->user;
 }
開發者ID:hyrmedia,項目名稱:KunstmaanBundlesCMS,代碼行數:10,代碼來源:LogPageEventsSubscriber.php

示例4: onKernelRequest

    /**
     * If user is logged-in in legacy_mode (e.g. legacy admin interface),
     * will inject currently logged-in user in the repository.
     *
     * @param GetResponseEvent $event
     */
    public function onKernelRequest( GetResponseEvent $event )
    {
        /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver */
        $request = $event->getRequest();
        $session = $request->getSession();
        if (
            $event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST
            || !$this->configResolver->getParameter( 'legacy_mode' )
            || !( $session->isStarted() && $session->has( 'eZUserLoggedInID' ) )
        )
        {
            return;
        }

        try
        {
            $apiUser = $this->repository->getUserService()->loadUser( $session->get( 'eZUserLoggedInID' ) );
            $this->repository->setCurrentUser( $apiUser );

            $token = $this->securityContext->getToken();
            if ( $token instanceof TokenInterface )
            {
                $token->setUser( new User( $apiUser ) );
                $token->setAuthenticated( true );
            }
        }
        catch ( NotFoundException $e )
        {
            // Invalid user ID, the user may have been removed => invalidate the token and the session.
            $this->securityContext->setToken( null );
            $session->invalidate();
        }
    }
開發者ID:ezsystemstraining,項目名稱:ez54training,代碼行數:39,代碼來源:RequestListener.php

示例5: handle

 /**
  * This interface must be implemented by firewall listeners.
  *
  * @param GetResponseEvent $event
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     $apiKey = $request->headers->get('Authorization', $request->query->get('api_key'));
     if (!$apiKey) {
         if (true === $this->forceApiKey) {
             $response = new Response();
             $response->setStatusCode(401);
             $event->setResponse($response);
         }
         return;
     }
     $token = new ApiKeyUserToken();
     $token->setApiKey($apiKey);
     try {
         $authToken = $this->authenticationManager->authenticate($token);
         $this->securityContext->setToken($authToken);
         return;
     } catch (AuthenticationException $failed) {
         $token = $this->securityContext->getToken();
         if ($token instanceof ApiKeyUserToken && $token->getCredentials() == $apiKey) {
             $this->securityContext->setToken(null);
         }
         $message = $failed->getMessage();
     }
     if ($this->isJsonRequest($request)) {
         $response = new JsonResponse(array('error' => $message));
     } else {
         $response = new Response();
         $response->setContent($message);
     }
     $response->setStatusCode(401);
     $event->setResponse($response);
 }
開發者ID:atipso,項目名稱:api-key-bundle,代碼行數:39,代碼來源:ApiKeyListener.php

示例6: onKernelController

 public function onKernelController(FilterControllerEvent $event)
 {
     if (!is_array($controller = $event->getController())) {
         return;
     }
     $className = class_exists('Doctrine\\Common\\Util\\ClassUtils') ? ClassUtils::getClass($controller[0]) : get_class($controller[0]);
     $object = new \ReflectionClass($className);
     $method = $object->getMethod($controller[1]);
     /**
      * @var UserType\UserTypeInterface[] $requiredUserTypes
      */
     $requiredUserTypes = array_merge($this->getConfigurations($this->reader->getClassAnnotations($object)), $this->getConfigurations($this->reader->getMethodAnnotations($method)));
     if (count($requiredUserTypes) === 0) {
         return;
     }
     $token = $this->securityContext->getToken();
     if (null === $token) {
         throw new AccessDeniedException(sprintf("You are not authenticated, a known user type is required for access."));
     }
     $user = $token->getUser();
     foreach ($requiredUserTypes as $userType) {
         $userClass = $userType->getUserClass();
         if (!interface_exists($userClass) && !class_exists($userClass)) {
             throw new RuntimeException(sprintf("The user type class '%s' does not exist", $userClass));
         }
         if (!$user instanceof $userClass) {
             throw new AccessDeniedException(sprintf("You do not have the required user type. A user type of '%s' is required.", $userClass));
         }
         unset($userType, $userClass);
     }
 }
開發者ID:viettut,項目名稱:todo,代碼行數:31,代碼來源:UserTypeSecurityListener.php

示例7: handle

 /**
  * {@inheritdoc}
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if ($this->securityContext->getToken() !== null) {
         return;
     }
     if ($request->getRequestUri() == '/app_dev.php/api/login' || $request->getRequestUri() == '/api/login') {
         return;
     }
     //Try to reach token from HTTP headers
     if ($request->headers->has('X-Auth-Token')) {
         $tokenId = $request->headers->get('X-Auth-Token');
     } else {
         $tokenId = $request->get('token');
     }
     //by token
     if (isset($tokenId)) {
         $user = $this->userProvider->findUserByToken($tokenId);
         if (!$user) {
             throw new BadCredentialsException();
         }
         try {
             $token = new ApiToken([], $this->providerId, $this->key);
             $token->setUser($user);
             $authenticatedToken = $this->authenticationManager->authenticate($token);
             $this->securityContext->setToken($authenticatedToken);
         } catch (AuthenticationException $e) {
             //log something
         }
     }
 }
開發者ID:Exanrus,項目名稱:crm-bundle,代碼行數:34,代碼來源:ApiListener.php

示例8: isVisible

 public function isVisible()
 {
     if ($this->securityContext === null) {
         return true;
     }
     return !$this->securityContext->getToken();
 }
開發者ID:mikegibson,項目名稱:sentient,代碼行數:7,代碼來源:LoginAction.php

示例9: onResponse

 /**
  * Checking request and response and decide whether we need a redirect
  *
  * @param FilterResponseEvent $event
  */
 public function onResponse(FilterResponseEvent $event)
 {
     $request = $event->getRequest();
     $response = $event->getResponse();
     if ($request->get(self::HASH_NAVIGATION_HEADER) || $request->headers->get(self::HASH_NAVIGATION_HEADER)) {
         $location = '';
         $isFullRedirect = false;
         if ($response->isRedirect()) {
             $location = $response->headers->get('location');
             if ($request->attributes->get('_fullRedirect') || !is_object($this->security->getToken())) {
                 $isFullRedirect = true;
             }
         }
         if ($response->isNotFound() || $response->getStatusCode() == 503 && !$this->isDebug) {
             $location = $request->getUri();
             $isFullRedirect = true;
         }
         if ($location) {
             $response = $this->templating->renderResponse('OroNavigationBundle:HashNav:redirect.html.twig', array('full_redirect' => $isFullRedirect, 'location' => $location));
         }
         // disable cache for ajax navigation pages and change content type to json
         $response->headers->set('Content-Type', 'application/json');
         $response->headers->addCacheControlDirective('no-cache', true);
         $response->headers->addCacheControlDirective('max-age', 0);
         $response->headers->addCacheControlDirective('must-revalidate', true);
         $response->headers->addCacheControlDirective('no-store', true);
         $event->setResponse($response);
     }
 }
開發者ID:ramunasd,項目名稱:platform,代碼行數:34,代碼來源:ResponseHashnavListener.php

示例10: blame

 /**
  * Assigns the Security token's user to the vote.
  *
  * @param  VoteEvent $vote
  * @return void
  */
 public function blame(VoteEvent $event)
 {
     $vote = $event->getVote();
     if (null === $this->securityContext) {
         if ($this->logger) {
             $this->logger->debug("Vote Blamer did not receive the security.context service.");
         }
         return;
     }
     if (!$vote instanceof SignedVoteInterface) {
         if ($this->logger) {
             $this->logger->debug("Vote does not implement SignedVoteInterface, skipping");
         }
         return;
     }
     if (null === $this->securityContext->getToken()) {
         if ($this->logger) {
             $this->logger->debug("There is no firewall configured. We cant get a user.");
         }
         return;
     }
     if ($this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
         $vote->setVoter($this->securityContext->getToken()->getUser());
     }
 }
開發者ID:saberyounis,項目名稱:Sonata-Project,代碼行數:31,代碼來源:VoteBlamerListener.php

示例11: process

 /**
  * Process form
  *
  * @param AccountUser $accountUser
  * @return bool True on successful processing, false otherwise
  */
 public function process(AccountUser $accountUser)
 {
     if (in_array($this->request->getMethod(), ['POST', 'PUT'], true)) {
         $this->form->submit($this->request);
         if ($this->form->isValid()) {
             if (!$accountUser->getId()) {
                 if ($this->form->get('passwordGenerate')->getData()) {
                     $generatedPassword = $this->userManager->generatePassword(10);
                     $accountUser->setPlainPassword($generatedPassword);
                 }
                 if ($this->form->get('sendEmail')->getData()) {
                     $this->userManager->sendWelcomeEmail($accountUser);
                 }
             }
             $token = $this->securityContext->getToken();
             if ($token instanceof OrganizationContextTokenInterface) {
                 $organization = $token->getOrganizationContext();
                 $accountUser->setOrganization($organization)->addOrganization($organization);
             }
             $this->userManager->updateUser($accountUser);
             return true;
         }
     }
     return false;
 }
開發者ID:hafeez3000,項目名稱:orocommerce,代碼行數:31,代碼來源:AccountUserHandler.php

示例12: getCustomer

 /**
  * Gets customer based on currently logged user.
  *
  * @return CustomerInterface|null
  */
 public function getCustomer()
 {
     if ($this->securityContext->getToken() && $this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED') && $this->securityContext->getToken()->getUser() instanceof UserInterface) {
         return $this->securityContext->getToken()->getUser()->getCustomer();
     }
     return null;
 }
開發者ID:aleherse,項目名稱:Sylius,代碼行數:12,代碼來源:CustomerContext.php

示例13: onCoreRequest

 /**
  * @param GetResponseEvent $event
  */
 public function onCoreRequest(GetResponseEvent $event)
 {
     if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) {
         return;
     }
     $token = $this->securityContext->getToken();
     if (!$token) {
         return;
     }
     if (!$token instanceof UsernamePasswordToken) {
         return;
     }
     $key = $this->helper->getSessionKey($this->securityContext->getToken());
     $request = $event->getRequest();
     $session = $event->getRequest()->getSession();
     $user = $this->securityContext->getToken()->getUser();
     if (!$session->has($key)) {
         return;
     }
     if ($session->get($key) === true) {
         return;
     }
     $state = 'init';
     if ($request->getMethod() == 'POST') {
         if ($this->helper->checkCode($user, $request->get('_code')) == true) {
             $session->set($key, true);
             return;
         }
         $state = 'error';
     }
     $event->setResponse($this->templating->renderResponse('SonataUserBundle:Admin:Security/two_step_form.html.twig', array('state' => $state)));
 }
開發者ID:lordrhodos,項目名稱:SonataUserBundle,代碼行數:35,代碼來源:RequestListener.php

示例14: get

 /**
  * Get the customer.
  *
  * @throws \RuntimeException
  *
  * @return \Sonata\Component\Customer\CustomerInterface
  */
 public function get()
 {
     $customer = null;
     $user = null;
     if (true === $this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
         // user is authenticated
         $user = $this->securityContext->getToken()->getUser();
         if (!$user instanceof UserInterface) {
             throw new \RuntimeException('User must be an instance of FOS\\UserBundle\\Model\\UserInterface');
         }
         $customer = $this->customerManager->findOneBy(array('user' => $user->getId()));
     }
     if (!$customer) {
         $basket = $this->getBasket();
         if ($basket && $basket->getCustomer()) {
             $customer = $basket->getCustomer();
         }
     }
     if (!$customer) {
         $customer = $this->customerManager->create();
     }
     if (!$customer->getLocale()) {
         $customer->setLocale($this->locale);
     }
     if ($user && $customer) {
         $customer->setUser($user);
     }
     return $customer;
 }
開發者ID:kinkinweb,項目名稱:lhvb,代碼行數:36,代碼來源:CustomerSelector.php

示例15: handle

 /**
  * This interface must be implemented by firewall listeners.
  *
  * @param GetResponseEvent $event
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->keyExtractor->hasKey($request)) {
         $response = new Response();
         $response->setStatusCode(401);
         $event->setResponse($response);
         return;
     }
     $apiKey = $this->keyExtractor->extractKey($request);
     $token = new ApiKeyUserToken();
     $token->setApiKey($apiKey);
     try {
         $authToken = $this->authenticationManager->authenticate($token);
         $this->securityContext->setToken($authToken);
         return;
     } catch (AuthenticationException $failed) {
         $token = $this->securityContext->getToken();
         if ($token instanceof ApiKeyUserToken && $token->getCredentials() == $apiKey) {
             $this->securityContext->setToken(null);
         }
         $message = $failed->getMessage();
     }
     $response = new Response();
     $response->setContent($message);
     $response->setStatusCode(403);
     $event->setResponse($response);
 }
開發者ID:tuandv2611,項目名稱:api-key-bundle,代碼行數:33,代碼來源:ApiKeyListener.php


注:本文中的Symfony\Component\Security\Core\SecurityContextInterface::getToken方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。