Пример #1
0
 /**
  *
  * @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;
     });
 }
Пример #3
0
 /**
  * {@inheritDoc}
  */
 public function clearRemoved()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'clearRemoved', array());
     return parent::clearRemoved();
 }
Пример #4
0
 /**
  * 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);
 }
Пример #5
0
 public function testUserInitialState()
 {
     $user = new User();
     $this->assertNull($user->getId(), 'User::id should be null');
     $this->assertTrue($user->getAdded() instanceof DateTime, 'User::added should have a DateTime value.');
     $this->assertEquals($user->getAdded(), new DateTime(), 'User::added should be set to today when instantiated');
     $this->assertNull($user->getRemoved(), 'User::removed should be null');
     $this->assertNull($user->getIdentity(), 'User::identity should be null');
     $this->assertSame($user::STATUS_ACTIVE, $user->getStatus(), 'User::status code should be set to User:STATUS_ACTIVE by default');
     $this->assertTrue(is_array($user->getAttributes()), 'User::attributes should be an array');
     $this->assertTrue(count($user->getAttributes()) == 0, 'User::attributes array should be empty when instantiated');
     $this->assertTrue(is_array($user->getSessions()), 'User::sessions should be an array');
     $this->assertTrue(count($user->getSessions()) == 0, 'User::sessions array should be empty when instantiated');
 }