/** * * @param array $config * * @return EntityInterface */ protected function hydrateEntity(array $config) { /* * run dependency check */ $this->checkDependencies(); $attribute = $this->factory->createInstance($config); /* * use the identity specified in the configuration to * get the existing user */ $user = $this->manager->getRepository(User::getEntityClass())->findOneBy(array('identity' => $config['identity'])); /* * if the user is found, return the attribute with the user * attached * * otherwise return null, which will make the import method skip this record */ if ($user) { return $attribute->setUser($user); } else { return null; } }
/** * tests to be run during authentication * * @return array */ private function getAuthenticationTests() { return array(function ($adapter) { $identity = $adapter->getIdentity(); $result = $adapter->getResultPrototype(); if (empty($identity)) { $result->setCode($result::FAILURE_IDENTITY_NOT_FOUND)->addMessage('Invalid Username Or Password')->addMessage('Username Cannot Be Empty'); } else { $result->setCode($result::SUCCESS); } return $result; }, function ($adapter) { $credential = $adapter->getCredential(); $result = $adapter->getResultPrototype(); if (empty($credential)) { $result->setCode($result::FAILURE_CREDENTIAL_INVALID)->addMessage("Invalid Username Or Password")->addMessage('Password Cannot Be Empty'); } else { $result->setCode($result::SUCCESS); } return $result; }, function ($adapter) { $identity = $adapter->getIdentity(); $em = $adapter->getEntityManager(); $result = $adapter->getResultPrototype(); /* * find active user with this identity */ $users = $em->getRepository(User::getEntityClass())->findBy(array('identity' => $identity, 'status' => User::STATUS_ACTIVE, 'removed' => null)); if (count($users) == 0) { $result->setCode($result::FAILURE_IDENTITY_NOT_FOUND)->addMessage('Invalid Username Or Password')->addMessage('User Not Found'); } else { $result->setCode($result::SUCCESS); } return $result; }, function ($adapter) { $identity = $adapter->getIdentity(); $credential = $adapter->getCredential(); $em = $adapter->getEntityManager(); $result = $adapter->getResultPrototype(); /* * find active user with this identity */ $users = $em->getRepository(User::getEntityClass())->findBy(array('identity' => $identity, 'status' => User::STATUS_ACTIVE, 'removed' => null)); /* * loop through each user found to test * credentials */ foreach ($users as $user) { /* * if the user credentials check out * then return a successful result with the user * identity populated */ if ($user->checkCredential($credential)) { $result->setIdentity($user->getId())->setCode($result::SUCCESS)->addMessage(sprintf("User %s Has Been Authenticated Successfully", $identity)); return $result; } } /* * fall through to return an invalid credential * result if no positive result is acheived above * * TODO: you could also trigger an event here to keep track * of the number of failed attempts. each failed attempt * can trigger an event that caches the failed attempt count per user. * * it can finally set a user as inactive after so many attempts if done so. * * i leave this up to the developer at this revision to implement based on * their specific business rules. you would have to make this class * event aware and add the event manager as a dependency i * would imagine. Zend makes it easy enough by using the * EventManagerAwareTrait */ $result->setCode($result::FAILURE_CREDENTIAL_INVALID)->addMessage("Invalid Username Or Password")->addMessage("An Invalid Login Attempt From This IP Address Has Been Recorded"); // may be extra, just to scare them a little return $result; }); }
/** * return the user represented by the ID * integer passed * * @param int $id * * @return User */ private function getUserById($id) { /* * run dependency check */ $this->checkDependencies(); $em = $this->entityManager; return $em->getRepository(User::getEntityClass())->find($id); }