/** * @param \User\Entity\User $user * @param $password * @return \User\Entity\Auth */ public function generateEquals(\User\Entity\User $user, $password) { //delete row $auth = $this->getObjectManager()->getRepository('User\\Entity\\Auth')->findOneBy(['userId' => $user->getId(), 'provider' => Auth::PROVIDER_EQUALS]); // ->findOneByUserId($user->getId()); if ($auth) { $this->getObjectManager()->remove($auth); $this->getObjectManager()->flush(); } // new auth row $row = new \User\Entity\Auth(); $row->setUserId($user->getId()); $row->setForeignKey($user->getEmail()); $row->setProvider(self::PROVIDER_EQUALS); $row->setTokenType(self::TYPE_ACCESS); // generate secret $alpha = range('a', 'z'); shuffle($alpha); $secret = array_slice($alpha, 0, rand(5, 15)); $secret = md5($user->getId() . join('', $secret)); $row->setTokenSecret($secret); // encrypt password and save as token $row->setToken(self::encrypt($row, $password)); $user->getAuths()->add($row); $row->setUser($user); $this->getObjectManager()->persist($row); $this->getObjectManager()->flush(); return $row; }