From b95d12700ccd5987e580ee30efc50c76019de97a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 25 Mar 2015 15:18:11 +0100 Subject: [PATCH] Add logic for getting the user from the rss token to the Manager --- lib/private/activitymanager.php | 65 ++++++++++++++++++++++++++++++++ lib/private/server.php | 8 +++- lib/public/activity/imanager.php | 10 +++++ 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/lib/private/activitymanager.php b/lib/private/activitymanager.php index c6cd5a1fe8..26db0c78df 100644 --- a/lib/private/activitymanager.php +++ b/lib/private/activitymanager.php @@ -28,8 +28,34 @@ namespace OC; use OCP\Activity\IConsumer; use OCP\Activity\IExtension; use OCP\Activity\IManager; +use OCP\IConfig; +use OCP\IRequest; +use OCP\IUserSession; class ActivityManager implements IManager { + /** @var IRequest */ + protected $request; + + /** @var IUserSession */ + protected $session; + + /** @var IConfig */ + protected $config; + + /** + * constructor of the controller + * + * @param IRequest $request + * @param IUserSession $session + * @param IConfig $config + */ + public function __construct(IRequest $request, + IUserSession $session, + IConfig $config) { + $this->request = $request; + $this->session = $session; + $this->config = $config; + } /** * @var \Closure[] @@ -348,4 +374,43 @@ class ActivityManager implements IManager { return array(' and ((' . implode(') or (', $conditions) . '))', $parameters); } + + /** + * Get the user we need to use + * + * Either the user is logged in, or we try to get it from the token + * + * @return string + * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique + */ + public function getCurrentUserId() { + if (!$this->session->isLoggedIn()) { + return $this->getUserFromToken(); + } else { + return $this->session->getUser()->getUID(); + } + } + + /** + * Get the user for the token + * + * @return string + * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique + */ + protected function getUserFromToken() { + $token = (string) $this->request->getParam('token', ''); + if (strlen($token) !== 30) { + throw new \UnexpectedValueException('The token is invalid'); + } + + $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token); + + if (sizeof($users) !== 1) { + // No unique user found + throw new \UnexpectedValueException('The token is invalid'); + } + + // Token found login as that user + return array_shift($users); + } } diff --git a/lib/private/server.php b/lib/private/server.php index 592f8d9a04..6b212ee94a 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -223,8 +223,12 @@ class Server extends SimpleContainer implements IServerContainer { new ArrayCache() ); }); - $this->registerService('ActivityManager', function ($c) { - return new ActivityManager(); + $this->registerService('ActivityManager', function (Server $c) { + return new ActivityManager( + $c->getRequest(), + $c->getUserSession(), + $c->getConfig() + ); }); $this->registerService('AvatarManager', function ($c) { return new AvatarManager(); diff --git a/lib/public/activity/imanager.php b/lib/public/activity/imanager.php index f7885860c4..2e55c8b45b 100644 --- a/lib/public/activity/imanager.php +++ b/lib/public/activity/imanager.php @@ -136,4 +136,14 @@ interface IManager { * @return array */ function getQueryForFilter($filter); + + /** + * Get the user we need to use + * + * Either the user is logged in, or we try to get it from the token + * + * @return string + * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique + */ + public function getCurrentUserId(); }