Ejemplo n.º 1
0
 /**
  * test credential removal
  *
  * @return void
  */
 public function testEraseCredentials()
 {
     $entity = new SecurityUser($this->getUserMock());
     $this->assertEmpty($entity->eraseCredentials());
 }
Ejemplo n.º 2
0
 /**
  * Tries to authenticate the provided token
  *
  * @param TokenInterface        $token        token to authenticate
  * @param UserProviderInterface $userProvider provider to auth against
  * @param string                $providerKey  key to auth with
  *
  * @return \Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken
  */
 public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
 {
     $username = $token->getCredentials();
     $securityUser = false;
     // If no username in Strategy, check if required.
     if ($this->securityRequired && !$username) {
         $this->logger->warning('Authentication key is required.');
         throw new AuthenticationException(sprintf('Authentication key is required.'));
     }
     /** @var SecurityUser $securityUser */
     if ($user = $this->userProvider->loadUserByUsername($username)) {
         $securityUser = new SecurityUser($user, [SecurityUser::ROLE_USER]);
     } elseif ($this->securityTestUsername) {
         $this->logger->info('Authentication, loading test user: '******'Authentication, loading anonymous user.');
             $securityUser = new SecurityUser(new AnonymousUser(), [SecurityUser::ROLE_ANONYMOUS]);
         } else {
             $this->logger->warning(sprintf('Authentication key "%s" could not be resolved.', $username));
             throw new AuthenticationException(sprintf('Authentication key "%s" could not be resolved.', $username));
         }
     }
     return new PreAuthenticatedToken($securityUser, $username, $providerKey, $securityUser->getRoles());
 }
Ejemplo n.º 3
0
 /**
  * {@inheritDoc}
  *
  * @param Request      $request The request object
  * @param SecurityUser $user    SecurityUser Object
  *
  * @return array
  */
 public function findAll(Request $request, SecurityUser $user = null)
 {
     $pageNumber = $request->query->get('page', 1);
     $numberPerPage = (int) $request->query->get('perPage', $this->getDefaultLimit());
     $startAt = ($pageNumber - 1) * $numberPerPage;
     /** @var \Doctrine\ODM\MongoDB\Query\Builder $queryBuilder */
     $queryBuilder = $this->repository->createQueryBuilder();
     if ($this->filterByAuthUser && $user && $user->hasRole(SecurityUser::ROLE_USER)) {
         $queryBuilder->field($this->filterByAuthField)->equals($user->getUser()->getId());
     }
     // *** do we have an RQL expression, do we need to filter data?
     if ($request->attributes->get('hasRql', false)) {
         $queryBuilder = $this->doRqlQuery($queryBuilder, $request->attributes->get('rqlQuery'));
     } else {
         // @todo [lapistano]: seems the offset is missing for this query.
         /** @var \Doctrine\ODM\MongoDB\Query\Builder $qb */
         $queryBuilder->find($this->repository->getDocumentName());
     }
     // define offset and limit
     if (!array_key_exists('skip', $queryBuilder->getQuery()->getQuery())) {
         $queryBuilder->skip($startAt);
     } else {
         $startAt = (int) $queryBuilder->getQuery()->getQuery()['skip'];
     }
     if (!array_key_exists('limit', $queryBuilder->getQuery()->getQuery())) {
         $queryBuilder->limit($numberPerPage);
     } else {
         $numberPerPage = (int) $queryBuilder->getQuery()->getQuery()['limit'];
     }
     /**
      * add a default sort on id if none was specified earlier
      *
      * not specifying something to sort on leads to very weird cases when fetching references
      */
     if (!array_key_exists('sort', $queryBuilder->getQuery()->getQuery())) {
         $queryBuilder->sort('_id');
     }
     // run query
     $query = $queryBuilder->getQuery();
     $records = array_values($query->execute()->toArray());
     $totalCount = $query->count();
     $numPages = (int) ceil($totalCount / $numberPerPage);
     $page = (int) ceil($startAt / $numberPerPage) + 1;
     if ($numPages > 1) {
         $request->attributes->set('paging', true);
         $request->attributes->set('page', $page);
         $request->attributes->set('numPages', $numPages);
         $request->attributes->set('startAt', $startAt);
         $request->attributes->set('perPage', $numberPerPage);
         $request->attributes->set('totalCount', $totalCount);
     }
     return $records;
 }