From 11f5904dd58cbcb45e0e3e6c466f187e30323155 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Wed, 16 Dec 2020 22:30:19 +0100 Subject: Fix mapper->find and empty user sessions Signed-off-by: Sean Molenaar --- lib/Controller/AdminController.php | 5 ++--- lib/Controller/ApiController.php | 19 ++++++++++++------- lib/Controller/Controller.php | 20 +++++++++++++------- lib/Controller/Exceptions/NotLoggedInException.php | 14 ++++++++++++++ lib/Controller/ExportController.php | 5 ++--- lib/Controller/FeedApiController.php | 6 +++--- lib/Controller/FeedController.php | 5 ++--- lib/Controller/FolderApiController.php | 5 ++--- lib/Controller/FolderController.php | 5 ++--- lib/Controller/ItemApiController.php | 5 ++--- lib/Controller/ItemController.php | 5 ++--- lib/Controller/PageController.php | 5 ++--- lib/Controller/UserApiController.php | 5 ++--- lib/Controller/UtilityApiController.php | 5 ++--- lib/Service/Service.php | 6 +++--- 15 files changed, 65 insertions(+), 50 deletions(-) create mode 100644 lib/Controller/Exceptions/NotLoggedInException.php (limited to 'lib') diff --git a/lib/Controller/AdminController.php b/lib/Controller/AdminController.php index 059a0c4a7..8a0dfd99c 100644 --- a/lib/Controller/AdminController.php +++ b/lib/Controller/AdminController.php @@ -36,13 +36,12 @@ class AdminController extends Controller /** * AdminController constructor. * - * @param string $appName The name of the app * @param IRequest $request The request * @param IConfig $config Config for nextcloud */ - public function __construct(string $appName, IRequest $request, IConfig $config) + public function __construct(IRequest $request, IConfig $config) { - parent::__construct($appName, $request); + parent::__construct(Application::NAME, $request); $this->config = $config; } diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index de7a0919c..1ffa1272d 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -15,8 +15,10 @@ namespace OCA\News\Controller; +use OCA\News\AppInfo\Application; +use OCA\News\Controller\Exceptions\NotLoggedInException; +use \OCP\IUser; use \OCP\IRequest; -use OCP\IUser; use \OCP\IUserSession; use \OCP\AppFramework\ApiController as BaseApiController; @@ -28,7 +30,7 @@ use \OCP\AppFramework\ApiController as BaseApiController; class ApiController extends BaseApiController { /** - * @var IUserSession + * @var IUserSession|null */ private $userSession; @@ -37,13 +39,12 @@ class ApiController extends BaseApiController * * Stores the user session to be able to leverage the user in further methods * - * @param string $appName The name of the app - * @param IRequest $request The request - * @param IUserSession $userSession The user session + * @param IRequest $request The request + * @param IUserSession|null $userSession The user session */ - public function __construct(string $appName, IRequest $request, IUserSession $userSession) + public function __construct(IRequest $request, ?IUserSession $userSession) { - parent::__construct($appName, $request); + parent::__construct(Application::NAME, $request); $this->userSession = $userSession; } @@ -52,6 +53,10 @@ class ApiController extends BaseApiController */ protected function getUser() { + if ($this->userSession === null) { + throw new NotLoggedInException(); + } + return $this->userSession->getUser(); } diff --git a/lib/Controller/Controller.php b/lib/Controller/Controller.php index b2b86b007..726ee8e7d 100644 --- a/lib/Controller/Controller.php +++ b/lib/Controller/Controller.php @@ -13,9 +13,11 @@ namespace OCA\News\Controller; +use OCA\News\AppInfo\Application; +use OCA\News\Controller\Exceptions\NotLoggedInException; use OCP\AppFramework\Controller as BaseController; +use \OCP\IUser; use \OCP\IRequest; -use OCP\IUser; use \OCP\IUserSession; /** @@ -26,7 +28,7 @@ use \OCP\IUserSession; class Controller extends BaseController { /** - * @var IUserSession + * @var IUserSession|null */ private $userSession; @@ -35,13 +37,13 @@ class Controller extends BaseController * * Stores the user session to be able to leverage the user in further methods * - * @param string $appName The name of the app - * @param IRequest $request The request - * @param IUserSession $userSession The user session + * @param IRequest $request The request + * @param IUserSession|null $userSession The user session */ - public function __construct(string $appName, IRequest $request, IUserSession $userSession) + public function __construct(IRequest $request, ?IUserSession $userSession) { - parent::__construct($appName, $request); + parent::__construct(Application::NAME, $request); + $this->userSession = $userSession; } @@ -50,6 +52,10 @@ class Controller extends BaseController */ protected function getUser() { + if ($this->userSession === null) { + throw new NotLoggedInException(); + } + return $this->userSession->getUser(); } diff --git a/lib/Controller/Exceptions/NotLoggedInException.php b/lib/Controller/Exceptions/NotLoggedInException.php new file mode 100644 index 000000000..9bdf2475c --- /dev/null +++ b/lib/Controller/Exceptions/NotLoggedInException.php @@ -0,0 +1,14 @@ +feedService = $feedService; $this->folderService = $folderService; $this->opmlService = $opmlService; diff --git a/lib/Controller/FeedApiController.php b/lib/Controller/FeedApiController.php index 65181f8ac..eb6edcad2 100644 --- a/lib/Controller/FeedApiController.php +++ b/lib/Controller/FeedApiController.php @@ -16,6 +16,7 @@ namespace OCA\News\Controller; use Exception; +use OCA\News\AppInfo\Application; use OCA\News\Service\Exceptions\ServiceConflictException; use OCA\News\Service\Exceptions\ServiceNotFoundException; use OCA\News\Service\FeedServiceV2; @@ -61,15 +62,14 @@ class FeedApiController extends ApiController private $serializer; public function __construct( - string $appName, IRequest $request, - IUserSession $userSession, + ?IUserSession $userSession, FeedService $oldFeedService, FeedServiceV2 $feedService, ItemService $oldItemService, LoggerInterface $logger ) { - parent::__construct($appName, $request, $userSession); + parent::__construct($request, $userSession); $this->feedService = $feedService; $this->oldFeedService = $oldFeedService; $this->oldItemService = $oldItemService; diff --git a/lib/Controller/FeedController.php b/lib/Controller/FeedController.php index 5881d7eb9..5abcd3393 100644 --- a/lib/Controller/FeedController.php +++ b/lib/Controller/FeedController.php @@ -44,15 +44,14 @@ class FeedController extends Controller private $settings; public function __construct( - string $appName, IRequest $request, FolderServiceV2 $folderService, FeedService $feedService, ItemService $itemService, IConfig $settings, - IUserSession $userSession + ?IUserSession $userSession ) { - parent::__construct($appName, $request, $userSession); + parent::__construct($request, $userSession); $this->folderService = $folderService; $this->feedService = $feedService; $this->itemService = $itemService; diff --git a/lib/Controller/FolderApiController.php b/lib/Controller/FolderApiController.php index 7f7f97525..8de4b9e69 100644 --- a/lib/Controller/FolderApiController.php +++ b/lib/Controller/FolderApiController.php @@ -35,13 +35,12 @@ class FolderApiController extends ApiController private $itemService; public function __construct( - string $appName, IRequest $request, - IUserSession $userSession, + ?IUserSession $userSession, FolderServiceV2 $folderService, ItemService $itemService ) { - parent::__construct($appName, $request, $userSession); + parent::__construct($request, $userSession); $this->folderService = $folderService; $this->itemService = $itemService; diff --git a/lib/Controller/FolderController.php b/lib/Controller/FolderController.php index c12c7042f..b33f46c54 100644 --- a/lib/Controller/FolderController.php +++ b/lib/Controller/FolderController.php @@ -39,14 +39,13 @@ class FolderController extends Controller private $itemService; public function __construct( - string $appName, IRequest $request, FolderServiceV2 $folderService, FeedService $feedService, ItemService $itemService, - IUserSession $userSession + ?IUserSession $userSession ) { - parent::__construct($appName, $request, $userSession); + parent::__construct($request, $userSession); $this->folderService = $folderService; $this->feedService = $feedService; $this->itemService = $itemService; diff --git a/lib/Controller/ItemApiController.php b/lib/Controller/ItemApiController.php index 3e31d2fdd..442734127 100644 --- a/lib/Controller/ItemApiController.php +++ b/lib/Controller/ItemApiController.php @@ -32,13 +32,12 @@ class ItemApiController extends ApiController private $itemService; public function __construct( - string $appName, IRequest $request, - IUserSession $userSession, + ?IUserSession $userSession, ItemService $oldItemService, ItemServiceV2 $itemService ) { - parent::__construct($appName, $request, $userSession); + parent::__construct($request, $userSession); $this->oldItemService = $oldItemService; $this->itemService = $itemService; diff --git a/lib/Controller/ItemController.php b/lib/Controller/ItemController.php index be4f707c9..7506891d4 100644 --- a/lib/Controller/ItemController.php +++ b/lib/Controller/ItemController.php @@ -32,14 +32,13 @@ class ItemController extends Controller private $settings; public function __construct( - $appName, IRequest $request, FeedService $feedService, ItemService $itemService, IConfig $settings, - IUserSession $userSession + ?IUserSession $userSession ) { - parent::__construct($appName, $request, $userSession); + parent::__construct($request, $userSession); $this->itemService = $itemService; $this->feedService = $feedService; $this->settings = $settings; diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index 8e55e69ee..4b39ba9c2 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -58,16 +58,15 @@ class PageController extends Controller private $statusService; public function __construct( - string $appName, IRequest $request, IConfig $settings, IURLGenerator $urlGenerator, IL10N $l10n, RecommendedSites $recommendedSites, StatusService $statusService, - IUserSession $userSession + ?IUserSession $userSession ) { - parent::__construct($appName, $request, $userSession); + parent::__construct($request, $userSession); $this->settings = $settings; $this->urlGenerator = $urlGenerator; $this->l10n = $l10n; diff --git a/lib/Controller/UserApiController.php b/lib/Controller/UserApiController.php index f5137992d..42f5607fe 100644 --- a/lib/Controller/UserApiController.php +++ b/lib/Controller/UserApiController.php @@ -24,11 +24,10 @@ use \OCP\AppFramework\Http; class UserApiController extends ApiController { public function __construct( - string $appName, IRequest $request, - IUserSession $userSession + ?IUserSession $userSession ) { - parent::__construct($appName, $request, $userSession); + parent::__construct($request, $userSession); } /** diff --git a/lib/Controller/UtilityApiController.php b/lib/Controller/UtilityApiController.php index a2e5ae910..943aa7b15 100644 --- a/lib/Controller/UtilityApiController.php +++ b/lib/Controller/UtilityApiController.php @@ -30,14 +30,13 @@ class UtilityApiController extends ApiController private $statusService; public function __construct( - string $appName, IRequest $request, - IUserSession $userSession, + ?IUserSession $userSession, UpdaterService $updater, IConfig $settings, StatusService $statusService ) { - parent::__construct($appName, $request, $userSession); + parent::__construct($request, $userSession); $this->updaterService = $updater; $this->settings = $settings; $this->statusService = $statusService; diff --git a/lib/Service/Service.php b/lib/Service/Service.php index 970613830..597a99647 100644 --- a/lib/Service/Service.php +++ b/lib/Service/Service.php @@ -40,8 +40,8 @@ abstract class Service /** * Service constructor. * - * @param NewsMapperV2 $mapper - * @param LoggerInterface $logger + * @param NewsMapperV2 $mapper + * @param LoggerInterface $logger */ public function __construct($mapper, LoggerInterface $logger) { @@ -97,7 +97,7 @@ abstract class Service public function find(string $userId, int $id): Entity { try { - return $this->mapper->find($userId, $id); + return $this->mapper->findFromUser($userId, $id); } catch (DoesNotExistException $ex) { throw new ServiceNotFoundException($ex->getMessage()); } catch (MultipleObjectsReturnedException $ex) { -- cgit v1.2.3