/** * Load a user by login token, stored as a setting */ static function getByToken($token) { $config = self::getStaticService('config'); $setting = \Db\Sql\Settings::getByKeyValue($config->settings->cookieToken, $token->getValue(), ['first' => TRUE]); if (!$setting || !valid($setting->object_id)) { return FALSE; } return \Db\Sql\Users::findFirst($setting->object_id); }
/** * Return settings by a key/value combination. * * @param string $key * @param string $value * @param array $options * @return array of Settings */ static function getByKeyValue($key, $value, $options = array()) { $settings = \Db\Sql\Settings::query()->where('key = :key:')->andWhere('value = :value:')->bind(array('key' => $key, 'value' => $value))->execute(); return get($options, 'first') === TRUE ? $settings->getFirst() : $settings; }
/** * Unsets the cookie token * * @param integer $userId * @return bool */ public function destroyToken($userId = NULL) { $config = $this->getService('config'); $cookies = $this->getService('cookies'); $auth = $this->getService('auth'); $userId = $userId ? $userId : $auth->getUserId(); $setting = \Db\Sql\Settings::get($userId, 'user', $config->settings->cookieToken, ['first' => TRUE]); return $setting && $setting->delete() && $cookies->get('token')->delete(); }