public function testGetViewer() { $user = CMTest_TH::createUser(); $uri = '/'; $headers = array('Host' => 'example.ch', 'Connection' => 'keep-alive'); $mock = $this->getMockForAbstractClass('CM_Http_Request_Abstract', array($uri, $headers)); /** @var CM_Http_Request_Abstract $mock */ $this->assertNull($mock->getViewer()); $headers = array('Host' => 'example.ch', 'Connection' => 'keep-alive', 'Cookie' => 'sessionId=a1d2726e5b3801226aafd12fd62496c8'); $mock = $this->getMockForAbstractClass('CM_Http_Request_Abstract', array($uri, $headers)); try { $mock->getViewer(true); $this->fail(); } catch (CM_Exception_AuthRequired $ex) { $this->assertTrue(true); } $session = new CM_Session(); $session->setUser($user); $headers = array('Host' => 'example.ch', 'Connection' => 'keep-alive', 'Cookie' => 'sessionId=' . $session->getId()); unset($session); $mock = $this->getMockForAbstractClass('CM_Http_Request_Abstract', array($uri, $headers)); $this->assertEquals($user, $mock->getViewer(true)); $user2 = CMTest_TH::createUser(); $mock = $this->getMockForAbstractClass('CM_Http_Request_Abstract', array($uri, $headers, null, $user2)); $this->assertEquals($user2, $mock->getViewer(true)); }
public function testOnRedisMessageSubscribeUser() { $adapter = new CM_MessageStream_Adapter_SocketRedis([]); $user = CMTest_TH::createUser(); $session = new CM_Session(); $session->setUser($user); $session->write(); $message = array('type' => 'subscribe', 'data' => array('channel' => 'foo:' . CM_Model_StreamChannel_Message::getTypeStatic(), 'clientKey' => 'bar', 'data' => array('sessionId' => $session->getId()))); $adapter->onRedisMessage(json_encode($message)); $streamChannel = CM_Model_StreamChannel_Message::findByKeyAndAdapter('foo', $adapter->getType()); $streamSubscribe = CM_Model_Stream_Subscribe::findByKeyAndChannel('bar', $streamChannel); $this->assertEquals($user, $streamSubscribe->getUser()); }
/** * @return CM_Session */ public function getSession() { if (!$this->hasSession()) { $this->_session = new CM_Session(null, $this); $this->_session->start(); } return $this->_session; }
protected function _registerCallbacks() { $this->_registerClockworkCallbacks('10 seconds', array('CM_Model_User::offlineDelayed' => function () { CM_Model_User::offlineDelayed(); })); $this->_registerClockworkCallbacks('1 minute', array('CM_Model_User::offlineOld' => function () { CM_Model_User::offlineOld(); }, 'CM_ModelAsset_User_Roles::deleteOld' => function () { CM_ModelAsset_User_Roles::deleteOld(); }, 'CM_Paging_Useragent_Abstract::deleteOlder' => function () { CM_Paging_Useragent_Abstract::deleteOlder(100 * 86400); }, 'CM_File_UserContent_Temp::deleteOlder' => function () { CM_File_UserContent_Temp::deleteOlder(86400); }, 'CM_SVM_Model::deleteOldTrainings' => function () { CM_SVM_Model::deleteOldTrainings(3000); }, 'CM_Paging_Ip_Blocked::deleteOlder' => function () { CM_Paging_Ip_Blocked::deleteOld(); }, 'CM_Captcha::deleteOlder' => function () { CM_Captcha::deleteOlder(3600); }, 'CM_Session::deleteExpired' => function () { CM_Session::deleteExpired(); }, 'CM_Stream_Video::synchronize' => function () { CM_Stream_Video::getInstance()->synchronize(); }, 'CM_Stream_Video::checkStreams' => function () { CM_Stream_Video::getInstance()->checkStreams(); }, 'CM_MessageStream_Service::synchronize' => function () { CM_Service_Manager::getInstance()->getStreamMessage()->synchronize(); })); $this->_registerClockworkCallbacks('15 minutes', array('CM_Mail::processQueue' => function () { CM_Mail::processQueue(500); }, 'CM_Action_Abstract::aggregate' => function () { CM_Action_Abstract::aggregate(); }, 'CM_Action_Abstract::deleteTransgressionsOlder' => function () { CM_Action_Abstract::deleteTransgressionsOlder(3 * 31 * 86400); }, 'CM_Paging_Log_Abstract::cleanup' => function () { foreach (CM_Paging_Log_Abstract::getClassChildren() as $logClass) { /** @var CM_Paging_Log_Abstract $log */ $log = new $logClass(); $log->cleanUp(); } })); $this->_registerClockworkCallbacks('8 days', array('CMService_MaxMind::upgrade' => function () { try { $maxMind = new CMService_MaxMind(); $maxMind->upgrade(); } catch (Exception $exception) { if (!is_a($exception, 'CM_Exception')) { $exception = new CM_Exception($exception->getMessage(), ['file' => $exception->getFile(), 'line' => $exception->getLine(), 'trace' => $exception->getTraceAsString()]); } $exception->setSeverity(CM_Exception::FATAL); throw $exception; } })); }
/** * @param CM_Session|null $session */ public function setSession(CM_Session $session = null) { if (null !== $session) { $session->setRequest($this); $session->start(); } $this->_session = $session; $this->resetViewer(); }
/** * @param CM_Model_User|null $user * @return CM_Session */ public static function createSession(CM_Model_User $user = null) { if (is_null($user)) { $user = self::createUser(); } $session = new CM_Session(); $session->setUser($user); $session->write(); return $session; }
public function testFindById() { $session = new CM_Session(); $session->set('foo', 'bar'); $session->write(); $this->assertEquals($session, CM_Session::findById($session->getId())); $this->assertNull(CM_Session::findById('foo')); $this->assertNull(CM_Session::findById('')); }
/** * @param string $message * @throws CM_Exception_Invalid */ public function onRedisMessage($message) { $message = CM_Params::jsonDecode($message); $type = $message['type']; $data = $message['data']; switch ($type) { case 'subscribe': $channel = $data['channel']; $clientKey = $data['clientKey']; $start = time(); $data = CM_Params::factory((array) $data['data'], true); $user = null; if ($data->has('sessionId')) { if ($session = CM_Session::findById($data->getString('sessionId'))) { $user = $session->getUser(false); } } $this->_subscribe($channel, $clientKey, $start, $user); break; case 'unsubscribe': $channel = $data['channel']; $clientKey = $data['clientKey']; $this->_unsubscribe($channel, $clientKey); break; case 'message': break; default: throw new CM_Exception_Invalid('Invalid socket-redis event type'); } }
public function testSetSessionFromCookie() { $requestFoo = new CM_Http_Request_Get('/foo'); $sessionFoo = new CM_Session(null, $requestFoo); $sessionFoo->set('foo', 'bar'); $sessionFoo->write(); $sessionFooId = $sessionFoo->getId(); $requestBar = new CM_Http_Request_Get('/bar', ['cookie' => 'sessionId=' . $sessionFooId . ';']); $sessionBar = $requestBar->getSession(); $this->assertEquals($sessionFooId, $sessionBar->getId()); $this->assertEquals('bar', $sessionBar->get('foo')); $this->assertEquals($requestBar, $sessionBar->getRequest()); }
protected function _registerCallbacks() { $this->_registerClockworkCallbacks('1 second', ['CM_Jobdistribution_DelayedQueue::queueOutstanding' => function () { $delayedQueue = $this->getServiceManager()->getDelayedJobQueue(); $delayedQueue->queueOutstanding(); }]); $this->_registerClockworkCallbacks('1 minute', array('CM_Model_User::offlineOld' => function () { CM_Model_User::offlineOld(); }, 'CM_ModelAsset_User_Roles::deleteOld' => function () { CM_ModelAsset_User_Roles::deleteOld(); }, 'CM_Paging_Useragent_Abstract::deleteOlder' => function () { CM_Paging_Useragent_Abstract::deleteOlder(100 * 86400); }, 'CM_File_UserContent_Temp::deleteOlder' => function () { CM_File_UserContent_Temp::deleteOlder(86400); }, 'CM_SVM_Model::deleteOldTrainings' => function () { CM_SVM_Model::deleteOldTrainings(3000); }, 'CM_Paging_Ip_Blocked::deleteOlder' => function () { CM_Paging_Ip_Blocked::deleteOld(); }, 'CM_Captcha::deleteOlder' => function () { CM_Captcha::deleteOlder(3600); }, 'CM_Session::deleteExpired' => function () { CM_Session::deleteExpired(); }, 'CM_MessageStream_Service::synchronize' => function () { CM_Service_Manager::getInstance()->getStreamMessage()->synchronize(); })); if ($this->getServiceManager()->has('janus')) { $this->_registerClockworkCallbacks('1 minute', array('CM_Janus_Service::synchronize' => function () { $this->getServiceManager()->getJanus('janus')->synchronize(); }, 'CM_Janus_Service::checkStreams' => function () { $this->getServiceManager()->getJanus('janus')->checkStreams(); })); } $this->_registerClockworkCallbacks('15 minutes', array('CM_Action_Abstract::aggregate' => function () { CM_Action_Abstract::aggregate(); }, 'CM_Action_Abstract::deleteTransgressionsOlder' => function () { CM_Action_Abstract::deleteTransgressionsOlder(3 * 31 * 86400); }, 'CM_Paging_Log::cleanup' => function () { $allLevelsList = array_values(CM_Log_Logger::getLevels()); foreach (CM_Paging_Log::getClassChildren() as $pagingLogClass) { /** @type CM_Paging_Log $log */ $log = new $pagingLogClass($allLevelsList); $log->cleanUp(); } (new CM_Paging_Log($allLevelsList, false))->cleanUp(); //deletes all untyped records })); if ($this->getServiceManager()->has('maxmind')) { $this->_registerClockworkCallbacks('8 days', array('CMService_MaxMind::upgrade' => function () { try { /** @var CMService_MaxMind $maxMind */ $maxMind = $this->getServiceManager()->get('maxmind', 'CMService_MaxMind'); $maxMind->upgrade(); } catch (Exception $exception) { if (!is_a($exception, 'CM_Exception')) { $exception = new CM_Exception($exception->getMessage(), null, ['file' => $exception->getFile(), 'line' => $exception->getLine(), 'trace' => $exception->getTraceAsString()]); } $exception->setSeverity(CM_Exception::FATAL); throw $exception; } })); } }
/** * @param string $streamName * @param string $clientKey * @param int $start * @param string $data * @throws CM_Exception_NotAllowed */ public function subscribe($streamName, $clientKey, $start, $data) { $streamName = (string) $streamName; $clientKey = (string) $clientKey; $start = (int) $start; $data = (string) $data; $user = null; $params = CM_Params::factory(CM_Params::jsonDecode($data), true); if ($params->has('sessionId')) { if ($session = CM_Session::findById($params->getString('sessionId'))) { $user = $session->getUser(false); } } /** @var CM_Model_StreamChannel_Abstract $streamChannel */ $streamChannel = CM_Model_StreamChannel_Abstract::findByKeyAndAdapter($streamName, $this->getType()); if (!$streamChannel) { throw new CM_Exception_NotAllowed(); } try { CM_Model_Stream_Subscribe::createStatic(array('streamChannel' => $streamChannel, 'user' => $user, 'start' => $start, 'key' => $clientKey)); } catch (CM_Exception $ex) { throw new CM_Exception_NotAllowed('Cannot subscribe: ' . $ex->getMessage()); } }