本文整理汇总了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);
}
示例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;
}
}
示例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;
}
示例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();
}
}
示例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);
}
示例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);
}
}
示例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
}
}
}
示例8: isVisible
public function isVisible()
{
if ($this->securityContext === null) {
return true;
}
return !$this->securityContext->getToken();
}
示例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);
}
}
示例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());
}
}
示例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;
}
示例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;
}
示例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)));
}
示例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;
}
示例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);
}