/** * N.B.: if the user passed as input is a supporter, the method prefix '1' * to the token, making it 41-character long, rather than 40 * * @param PcApiApp $apiApp * @param int $userId * @return string */ public static function createToken(PcApiApp $apiApp, $userId) { $apiAppId = $apiApp->getId(); // if there is already a token entry for the application and the user, we delete it $c = new Criteria(); $c->add(PcApiTokenPeer::API_APP_ID, $apiAppId); $c->add(PcApiTokenPeer::USER_ID, $userId); PcApiTokenPeer::doDelete($c); $apiTokenEntry = new PcApiToken(); $tokenPrefix = PcUserPeer::retrieveByPK($userId)->isSupporter() ? '1' : ''; // we want to be extra-sure the token is unique $token = ''; $safetyCounter = 0; // to avoid infinite loop under any circumstances do { $token = $tokenPrefix . PcUtils::generate40CharacterRandomHash(); $c = new Criteria(); $c->add(PcApiTokenPeer::TOKEN, $token); $alreadyExisting = PcApiTokenPeer::doSelectOne($c); $safetyCounter++; if ($safetyCounter == 100) { throw new Exception("Detected possible infinite loop while creating API token"); } } while (is_object($alreadyExisting)); $apiTokenEntry->setToken($token)->setApiAppId($apiAppId)->setUserId($userId)->setExpiryTimestamp(time() + sfConfig::get('app_api_tokenValidity') * 3600)->save(); return $token; }
/** * Retrieve multiple objects by pkey. * * @param array $pks List of primary keys * @param PropelPDO $con the connection to use * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function retrieveByPKs($pks, PropelPDO $con = null) { if ($con === null) { $con = Propel::getConnection(PcApiTokenPeer::DATABASE_NAME, Propel::CONNECTION_READ); } $objs = null; if (empty($pks)) { $objs = array(); } else { $criteria = new Criteria(PcApiTokenPeer::DATABASE_NAME); $criteria->add(PcApiTokenPeer::TOKEN, $pks, Criteria::IN); $objs = PcApiTokenPeer::doSelect($criteria, $con); } return $objs; }
/** * * @param string $token * @return boolean - whether the token is valid or not */ private function isTokenValid($token) { if ($this->methodName == 'getToken') { return true; } $c = new Criteria(); $c->add(PcApiTokenPeer::TOKEN, $token); $apiTokenEntry = PcApiTokenPeer::doSelectOne($c); if (!is_object($apiTokenEntry)) { return false; } if ($apiTokenEntry->getExpiryTimestamp() < time() || $apiTokenEntry == null || $apiTokenEntry->getUserId() <= 0) { return false; } return true; }
/** * If this collection has already been initialized with * an identical criteria, it returns the collection. * Otherwise if this PcApiApp is new, it will return * an empty collection; or if this PcApiApp has previously * been saved, it will retrieve related PcApiTokens from storage. * * This method is protected by default in order to keep the public * api reasonable. You can provide public methods for those you * actually need in PcApiApp. */ public function getPcApiTokensJoinPcUser($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN) { if ($criteria === null) { $criteria = new Criteria(PcApiAppPeer::DATABASE_NAME); } elseif ($criteria instanceof Criteria) { $criteria = clone $criteria; } if ($this->collPcApiTokens === null) { if ($this->isNew()) { $this->collPcApiTokens = array(); } else { $criteria->add(PcApiTokenPeer::API_APP_ID, $this->id); $this->collPcApiTokens = PcApiTokenPeer::doSelectJoinPcUser($criteria, $con, $join_behavior); } } else { // the following code is to determine if a new query is // called for. If the criteria is the same as the last // one, just return the collection. $criteria->add(PcApiTokenPeer::API_APP_ID, $this->id); if (!isset($this->lastPcApiTokenCriteria) || !$this->lastPcApiTokenCriteria->equals($criteria)) { $this->collPcApiTokens = PcApiTokenPeer::doSelectJoinPcUser($criteria, $con, $join_behavior); } } $this->lastPcApiTokenCriteria = $criteria; return $this->collPcApiTokens; }
/** * Populates the object using an array. * * This is particularly useful when populating an object from one of the * request arrays (e.g. $_POST). This method goes through the column * names, checking to see whether a matching key exists in populated * array. If so the setByName() method is called for that column. * * You can specify the key type of the array by additionally passing one * of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME, * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM. * The default key type is the column's phpname (e.g. 'AuthorId') * * @param array $arr An array to populate the object from. * @param string $keyType The type of keys the array uses. * @return void */ public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME) { $keys = PcApiTokenPeer::getFieldNames($keyType); if (array_key_exists($keys[0], $arr)) { $this->setToken($arr[$keys[0]]); } if (array_key_exists($keys[1], $arr)) { $this->setApiAppId($arr[$keys[1]]); } if (array_key_exists($keys[2], $arr)) { $this->setUserId($arr[$keys[2]]); } if (array_key_exists($keys[3], $arr)) { $this->setExpiryTimestamp($arr[$keys[3]]); } }