public function testValidTokenIsValid() { $repo = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->setMethods(['findValidToken'])->getMock(); $tokenValue = self::$DI['app']['random.low']->generateString(8); $token = new Token(); $token->setType(TokenManipulator::TYPE_PASSWORD); $repo->expects($this->once())->method('findValidToken')->with($tokenValue)->will($this->returnValue($token)); $constraint = new PasswordToken($repo); $this->assertTrue($constraint->isValid($tokenValue)); }
/** * @param User|null $user * @param string $type * @param \DateTime|null $expiration * @param mixed|null $data * * @return Token */ public function create(User $user = null, $type, \DateTime $expiration = null, $data = null) { $this->removeExpiredTokens(); $n = 0; do { if ($n++ > 1024) { throw new \RuntimeException('Unable to create a token.'); } $value = $this->random->generateString(32, self::LETTERS_AND_NUMBERS); $found = null !== $this->om->getRepository('Phraseanet:Token')->find($value); } while ($found); $token = new Token(); $token->setUser($user)->setType($type)->setValue($value)->setExpiration($expiration)->setData($data); $this->om->persist($token); $this->om->flush(); return $token; }
private function insertOneValidationToken(EntityManager $em, \Pimple $DI) { $user = $DI['user']; $token = new Token(); $token->setValue($this->container['random.low']->generateString(12, TokenManipulator::LETTERS_AND_NUMBERS)); $token->setUser($user); $token->setType(TokenManipulator::TYPE_VALIDATE); $token->setData($DI['basket_1']->getId()); $DI['token_validation'] = $token; $em->persist($token); }
/** * Build a zip of downloaded documents * * @param Application $app * @param Request $request * @param Token $token * * @return Response */ public function downloadExecute(Application $app, Request $request, Token $token) { if (false === ($list = @unserialize($token->getData()))) { return $app->json(['success' => false, 'message' => 'Invalid datas']); } set_time_limit(0); // Force the session to be saved and closed. $app['session']->save(); ignore_user_abort(true); \set_export::build_zip($app, $token, $list, sprintf($app['tmp.download.path'] . '/%s.zip', $token->getValue())); return $app->json(['success' => true, 'message' => '']); }
/** * * @param Application $app * @param String $token * @param Array $list * @param string $zipFile * * @return string */ public static function build_zip(Application $app, Token $token, array $list, $zipFile) { if (isset($list['complete']) && $list['complete'] === true) { return; } $files = $list['files']; $list['complete'] = false; $token->setData(serialize($list)); $app['manipulator.token']->update($token); $toRemove = []; $archiveFiles = []; foreach ($files as $record) { if (isset($record["subdefs"])) { foreach ($record["subdefs"] as $o => $obj) { $path = p4string::addEndSlash($obj["path"]) . $obj["file"]; if (is_file($path)) { $name = $obj["folder"] . $record["export_name"] . $obj["ajout"] . '.' . $obj["exportExt"]; $archiveFiles[$app['unicode']->remove_diacritics($name)] = $path; if ($o == 'caption') { if (!in_array(dirname($path), $toRemove)) { $toRemove[] = dirname($path); } $toRemove[] = $path; } } } } } $app['zippy']->create($zipFile, $archiveFiles); $list['complete'] = true; $token->setData(serialize($list)); $app['manipulator.token']->update($token); $app['filesystem']->remove($toRemove); $app['filesystem']->chmod($zipFile, 0760); return $zipFile; }
/** * Build a zip of downloaded documents * * @param Token $token * * @return Response */ public function downloadExecute(Token $token) { if (false === ($list = @unserialize($token->getData()))) { return $this->app->json(['success' => false, 'message' => 'Invalid datas']); } set_time_limit(0); // Force the session to be saved and closed. /** @var Session $session */ $session = $this->app['session']; $session->save(); ignore_user_abort(true); if ($list['count'] > 1) { \set_export::build_zip($this->app, $token, $list, sprintf($this->app['tmp.download.path'] . '/%s.zip', $token->getValue())); } else { $list['complete'] = true; $token->setData(serialize($list)); /** @var EntityManagerInterface $manager */ $manager = $this->app['orm.em']; $manager->persist($token); $manager->flush(); } return $this->app->json(['success' => true, 'message' => '']); }