Example #1
0
 /**
  * Is a display name available
  *
  * @param   string          $displayName
  * @param   array|object    $fields [optional]
  * @return  bool
  */
 public function isDisplayNameAvailable($displayName, $fields = array())
 {
     $fields = (object) $fields;
     $displayName = Structure::trimDisplayName($displayName);
     if (3 > mb_strlen($displayName)) {
         return 'user.action.register.displayName.tooShort';
     }
     return $this->getMapper()->isDisplayNameExists($displayName, empty($fields->id) ? null : $fields->id) ? 'user.action.register.displayName.taken' : true;
 }
Example #2
0
 /**
  * Performs an authentication attempt
  *
  * @return \Zend\Authentication\Result
  * @throws \Zend\Authentication\Adapter\Exception\ExceptionInterface
  *         If authentication cannot be performed
  */
 public function authenticate()
 {
     $registered = false;
     $model = $this->getModel();
     $mode = $this->openid_mode;
     $openId = $this->openid_identity;
     $consumer = new Consumer\FederatedConsumer();
     $ax = new Extension\Ax(array('email' => true, 'firstname' => false, 'lastname' => false, 'language' => false));
     $consumer->setHttpClient($this->getServiceLocator()->get('Zend\\Http\\Client'));
     $success = $mode == 'id_res' ? $consumer->verify((array) $this->getOptions(), $openId, $ax) : $consumer->login($openId, null, null, $ax, $this->getServiceLocator()->get('Response'));
     if (!$success) {
         return new Result(Result::FAILURE_CREDENTIAL_INVALID, null, array((string) $consumer->getError()));
     }
     $data = $ax->getProperties();
     if (empty($data['email'])) {
         return new Result(Result::FAILURE_CREDENTIAL_INVALID, null);
     }
     $email = $data['email'];
     $user = $model->findByEmail($email);
     if (empty($user)) {
         if (!$this->isRegistrationEnabled()) {
             return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, null);
         }
         $displayName = null;
         if (!empty($data['firstname']) && !empty($data['lastname'])) {
             $displayName = $data['firstname'] . ' ' . $data['lastname'];
         } else {
             if (!empty($data['firstname'])) {
                 $displayName = $data['firstname'];
             } else {
                 if (!empty($data['lastname'])) {
                     $displayName = $data['lastname'];
                 } else {
                     $displayName = preg_replace('/@.*$/', '', $email);
                 }
             }
         }
         $i = 1;
         $displayName = UserStructure::trimDisplayName($displayName);
         $originalName = $displayName;
         while (!$model->isDisplayNameAvailable($displayName)) {
             $displayName = $originalName . ' ' . ++$i;
         }
         $user = $model->create(array('confirmed' => true, 'status' => 'active', 'displayName' => $displayName, 'email' => $email, 'locale' => !empty($data['language']) ? $data['language'] : (string) $this->getServiceLocator()->get('Locale'), 'password' => String::generateRandom(10)));
         if ($user->save()) {
             $registered = true;
             $user = $model->findByEmail($email);
         } else {
             return new Result(Result::FAILURE_UNCATEGORIZED, null);
         }
     }
     if (empty($user) || empty($user->id) || $user->isBanned()) {
         return new Result(Result::FAILURE_CREDENTIAL_INVALID, null);
     } else {
         if ($user->isInactive()) {
             $user->makeActive();
             if (!$user->save()) {
                 return new Result(Result::FAILURE_UNCATEGORIZED, null);
             }
         }
     }
     $model->associateIdentity($user->id, $openId);
     return new Result(Result::SUCCESS, $user, array('loginWith' => 'openid', 'registered' => $registered));
 }
Example #3
0
 /**
  * Is display name available?
  *
  * @param   string  $displayName
  * @return  bool
  */
 public function isDisplayNameAvailable($displayName)
 {
     return !$this->getMapper()->isDisplayNameExists(Structure::trimDisplayName($displayName), null);
 }
 /**
  * Performs an authentication attempt
  *
  * @return \Zend\Authentication\Result
  * @throws \Zend\Authentication\Adapter\Exception\ExceptionInterface
  *         If authentication cannot be performed
  */
 public function authenticate()
 {
     $registered = false;
     $model = $this->getModel();
     $settings = $this->getServiceLocator()->get('Grid\\Facebook\\Model\\ApplicationSettings\\AdapterFactory')->factory(array('application' => 'login'));
     $appId = $settings->getSetting('appId');
     $appSecret = $settings->getSetting('appSecret');
     if (empty($appId) || empty($appSecret)) {
         return new Result(Result::FAILURE_UNCATEGORIZED, null, array('appId and/or appSecret not set'));
     }
     $service = $this->getServiceLocator();
     $client = new OAuth\Client($service->get('Zend\\Http\\Client'), $this->getSessionManager(), $service->get('Zork\\Db\\SiteInfo'));
     $data = $client->login(array('client_id' => $appId, 'client_secret' => $appSecret), $service->get('Request'), $service->get('Response'));
     if (empty($data) || empty($data['email'])) {
         return new Result(Result::FAILURE_CREDENTIAL_INVALID, null, array('Cannot parse graph response or email not sent'));
     }
     $email = $data['email'];
     $user = $model->findByEmail($email);
     if (empty($user)) {
         if (!$this->isRegistrationEnabled()) {
             return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, null);
         }
         $displayName = empty($data['name']) ? preg_replace('/@.*$/', '', $email) : $data['name'];
         $i = 1;
         $displayName = UserStructure::trimDisplayName($displayName);
         $originalName = $displayName;
         while (!$model->isDisplayNameAvailable($displayName)) {
             $displayName = $originalName . ' ' . ++$i;
         }
         $user = $model->create(array('confirmed' => true, 'status' => 'active', 'displayName' => $displayName, 'email' => $email, 'locale' => !empty($data['language']) ? $data['language'] : (string) $this->getServiceLocator()->get('Locale'), 'password' => String::generateRandom(10)));
         if ($user->save()) {
             $registered = true;
             $user = $model->findByEmail($email);
         } else {
             return new Result(Result::FAILURE_UNCATEGORIZED, null);
         }
     }
     if (empty($user) || empty($user->id) || $user->isBanned()) {
         return new Result(Result::FAILURE_CREDENTIAL_INVALID, null);
     } else {
         if ($user->isInactive()) {
             $user->makeActive();
             if (!$user->save()) {
                 return new Result(Result::FAILURE_UNCATEGORIZED, null);
             }
         }
     }
     $model->associateIdentity($user->id, empty($data['link']) ? 'urn:facebook:' . (empty($data['id']) ? $email : $data['id']) : $data['link']);
     return new Result(Result::SUCCESS, $user, array('loginWith' => 'facebook', 'registered' => $registered));
 }