/**
  * Update basic user profile info when logged with fb/gool
  *
  * @param Request $r
  * @return array
  * @throws InvalidDatabaseOperationException
  * @throws InvalidParameterException
  */
 public static function apiUpdateBasicInfo(Request $r)
 {
     self::authenticateRequest($r);
     //Buscar que el nuevo username no este ocupado si es que selecciono uno nuevo
     if ($r['username'] != $r['current_user']->getUsername()) {
         $testu = UsersDAO::FindByUsername($r['username']);
         if (!is_null($testu)) {
             throw new InvalidParameterException('parameterUsernameInUse', 'username');
         }
         Validators::isValidUsername($r['username'], 'username');
         $r['current_user']->setUsername($r['username']);
     }
     SecurityTools::testStrongPassword($r['password']);
     $hashedPassword = SecurityTools::hashString($r['password']);
     $r['current_user']->setPassword($hashedPassword);
     UsersDAO::save($r['current_user']);
     return array('status' => 'ok');
 }
 private static function getUniqueUsernameFromEmail($s_Email)
 {
     $idx = strpos($s_Email, '@');
     $username = substr($s_Email, 0, $idx);
     try {
         Validators::isValidUsername($username, 'username');
     } catch (InvalidParameterException $e) {
         // How can we know whats wrong with the username?
         // Things that could go wrong:
         //		generated email is too short
         $username = '******';
     }
     $suffix = '';
     for (;;) {
         // Maybe we can bring all records from db
         // with prefix $username, beacuse this:
         $userexists = UsersDAO::FindByUsername($username . $suffix);
         // will query db every single time probably.
         if (empty($userexists)) {
             break;
         }
         if (empty($suffix)) {
             $suffix = 1;
         } else {
             $suffix++;
         }
     }
     return $username . $suffix;
 }