/**
  * Get the associated ChildUser object
  *
  * @param  ConnectionInterface $con Optional Connection object.
  * @return ChildUser The associated ChildUser object.
  * @throws PropelException
  */
 public function getUser(ConnectionInterface $con = null)
 {
     if ($this->aUser === null && $this->userid !== null) {
         $this->aUser = ChildUserQuery::create()->findPk($this->userid, $con);
         /* The following can be used additionally to
               guarantee the related object contains a reference
               to this object.  This level of coupling may, however, be
               undesirable since it could result in an only partially populated collection
               in the referenced object.
               $this->aUser->addEveApis($this);
            */
     }
     return $this->aUser;
 }
 /**
  * Returns a new ChildUserQuery object.
  *
  * @param     string $modelAlias The alias of a model in the query
  * @param     Criteria $criteria Optional Criteria to build the query from
  *
  * @return ChildUserQuery
  */
 public static function create($modelAlias = null, Criteria $criteria = null)
 {
     if ($criteria instanceof ChildUserQuery) {
         return $criteria;
     }
     $query = new ChildUserQuery();
     if (null !== $modelAlias) {
         $query->setModelAlias($modelAlias);
     }
     if ($criteria instanceof Criteria) {
         $query->mergeWith($criteria);
     }
     return $query;
 }
 public function __construct($app)
 {
     $this->app = $app;
     $app->group('/user', function () use($app) {
         $app->get('/autocomplete', function () {
             $userService = new \Core\Service\UserService();
             echo json_encode($userService->getAutocomplete($_GET['s']));
         });
         $app->get('/check', function () {
             $userAvailible = ECP\UserQuery::create()->filterByName($_GET['name'])->count() == 0;
             echo json_encode((object) array('isAvailible' => $userAvailible));
         });
         $app->get('/status', function () {
             $userService = new \Core\Service\UserService();
             $isLoggedIn = $userService->isLoggedIn();
             if (!$isLoggedIn) {
                 die(json_encode((object) array('isLoggedIn' => false)));
             }
             $user = $userService->getLoggedInUser();
             echo json_encode((object) array('isLoggedIn' => true, 'id' => $user->id, 'username' => $user->username));
         });
         $app->post('/login', function () {
             $p = getPost();
             $user = ECP\UserQuery::create()->filterByName($p->username)->filterByPassword(sha1($p->password))->filterByConfirmationCode('')->findOne();
             if (!$user) {
                 die(json_encode((object) array('status' => 'incorrect credentials')));
             }
             $_SESSION['ecp'] = (object) array('id' => $user->getId(), 'username' => $user->getName());
             echo $this->getBoolStatus(true);
         });
         $app->post('/register', function () {
             $userService = new \Core\Service\UserService();
             $p = getPost();
             if (strpos($p->username, '/') !== false) {
                 die_err('Slashes are not allowed in names!');
             }
             $code = generateCode();
             $user = new ECP\User();
             $user->setName($p->username);
             $user->setPassword(sha1($p->password));
             $user->setEmail($p->email);
             $user->setCreated(time());
             $user->setConfirmationCode($code);
             $user->save();
             $userService->sendRegistrationMail($user);
             echo $this->getBoolStatus(true);
         });
         $app->post('/logout', function () {
             unset($_SESSION['ecp']);
             echo '{}';
         });
         $app->post('/recover-password', function () {
             $userService = new \Core\Service\UserService();
             $p = getPost();
             $users = ECP\UserQuery::create()->filterByEmail($p->email)->filterByConfirmationCode('')->find();
             foreach ($users as $user) {
                 $code = generateCode();
                 $user->setRecoverPasswordCode($code);
                 $user->save();
                 $userService->sendRecoverPassword($user);
             }
             echo $this->getBoolStatus(true);
         });
         $app->get('/reset-password-check', function () {
             $userCount = ECP\UserQuery::create()->filterByRecoverPasswordCode($_GET['code'])->count();
             echo $this->getBoolStatus($userCount != 0);
         });
         $app->post('/reset-password', function () {
             $p = getPost();
             $users = ECP\UserQuery::create()->filterByRecoverPasswordCode($p->code)->find();
             $found = false;
             foreach ($users as $user) {
                 $user->setRecoverPasswordCode('');
                 $user->setPassword(sha1($p->password));
                 $user->save();
                 $found = true;
             }
             echo $this->getBoolStatus($found);
         });
         $app->post('/confirm-registration', function () {
             $p = getPost();
             $users = ECP\UserQuery::create()->filterByConfirmationCode($p->code)->find();
             $found = false;
             foreach ($users as $user) {
                 $user->setConfirmationCode('');
                 $user->save();
                 $found = true;
             }
             echo $this->getBoolStatus($found);
         });
     });
 }
 protected function createQuery()
 {
     return ECP\UserQuery::create();
 }
 /**
  * Performs an INSERT on the database, given a User or Criteria object.
  *
  * @param mixed               $criteria Criteria or User object containing data that is used to create the INSERT statement.
  * @param ConnectionInterface $con the ConnectionInterface connection to use
  * @return mixed           The new primary key.
  * @throws PropelException Any exceptions caught during processing will be
  *                         rethrown wrapped into a PropelException.
  */
 public static function doInsert($criteria, ConnectionInterface $con = null)
 {
     if (null === $con) {
         $con = Propel::getServiceContainer()->getWriteConnection(UserTableMap::DATABASE_NAME);
     }
     if ($criteria instanceof Criteria) {
         $criteria = clone $criteria;
         // rename for clarity
     } else {
         $criteria = $criteria->buildCriteria();
         // build Criteria from User object
     }
     if ($criteria->containsKey(UserTableMap::COL_ID) && $criteria->keyContainsValue(UserTableMap::COL_ID)) {
         throw new PropelException('Cannot insert a value for auto-increment primary key (' . UserTableMap::COL_ID . ')');
     }
     // Set the correct dbName
     $query = UserQuery::create()->mergeWith($criteria);
     // use transaction because $criteria could contain info
     // for more than one table (I guess, conceivably)
     return $con->transaction(function () use($con, $query) {
         return $query->doInsert($con);
     });
 }
 /**
  * Builds a Criteria object containing the primary key for this object.
  *
  * Unlike buildCriteria() this method includes the primary key values regardless
  * of whether or not they have been modified.
  *
  * @throws LogicException if no primary key is defined
  *
  * @return Criteria The Criteria object containing value(s) for primary key(s).
  */
 public function buildPkeyCriteria()
 {
     $criteria = ChildUserQuery::create();
     $criteria->add(UserTableMap::COL_ID, $this->id);
     return $criteria;
 }