summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorDavid Guillot <david@guillot.me>2018-06-29 22:28:12 +0200
committerDavid Guillot <david@guillot.me>2018-07-01 23:01:54 +0200
commita84e80131a891184e234ddeee1ba0606ea898d7b (patch)
tree493d7a609192c86cb3c5b79a5e2ab8d193386f90 /lib
parentec3cc602f1bd363efe776ce4914319034d35c18e (diff)
feat(api): support new OC core login flow
* Base ApiController now needs IUserSession and extracts/serves IUser from it * All other API controllers now inherit from ApiController
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/ApiController.php37
-rw-r--r--lib/Controller/FeedApiController.php29
-rw-r--r--lib/Controller/FolderApiController.php23
-rw-r--r--lib/Controller/ItemApiController.php25
-rw-r--r--lib/Controller/UserApiController.php7
-rw-r--r--lib/Controller/UtilityApiController.php6
6 files changed, 80 insertions, 47 deletions
diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php
index f5052b49a..d2a787a28 100644
--- a/lib/Controller/ApiController.php
+++ b/lib/Controller/ApiController.php
@@ -7,14 +7,17 @@
*
* @author Alessandro Cosentino <cosenal@gmail.com>
* @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @author David Guillot <david@guillot.me>
* @copyright 2012 Alessandro Cosentino
* @copyright 2012-2014 Bernhard Posselt
+ * @copyright 2018 David Guillot
*/
namespace OCA\News\Controller;
-use OCP\IRequest;
-use OCP\AppFramework\ApiController as BaseApiController;
+use \OCP\IRequest;
+use \OCP\IUserSession;
+use \OCP\AppFramework\ApiController as BaseApiController;
/**
* Class ApiController
@@ -24,14 +27,36 @@ use OCP\AppFramework\ApiController as BaseApiController;
class ApiController extends BaseApiController
{
/**
+ * @var IUserSession
+ */
+ private $userSession;
+
+ /**
* ApiController constructor.
*
- * @param string $appName The name of the app
- * @param IRequest $request The request
+ * 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
*/
- public function __construct($appName, IRequest $request)
- {
+ public function __construct($appName, IRequest $request, IUserSession $userSession) {
parent::__construct($appName, $request);
+ $this->userSession = $userSession;
+ }
+
+ /**
+ * @return IUser
+ */
+ protected function getUser() {
+ return $this->userSession->getUser();
+ }
+
+ /**
+ * @return string
+ */
+ protected function getUserId() {
+ return $this->getUser()->getUID();
}
/**
diff --git a/lib/Controller/FeedApiController.php b/lib/Controller/FeedApiController.php
index 344d72bd0..2e4a85eef 100644
--- a/lib/Controller/FeedApiController.php
+++ b/lib/Controller/FeedApiController.php
@@ -7,14 +7,17 @@
*
* @author Alessandro Cosentino <cosenal@gmail.com>
* @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @author David Guillot <david@guillot.me>
* @copyright 2012 Alessandro Cosentino
* @copyright 2012-2014 Bernhard Posselt
+ * @copyright 2018 David Guillot
*/
namespace OCA\News\Controller;
use \OCP\IRequest;
use \OCP\ILogger;
+use \OCP\IUserSession;
use \OCP\AppFramework\Http;
use \OCA\News\Service\FeedService;
@@ -30,23 +33,21 @@ class FeedApiController extends ApiController
private $itemService;
private $feedService;
- private $userId;
private $logger;
private $loggerParams;
private $serializer;
public function __construct($appName,
IRequest $request,
+ IUserSession $userSession,
FeedService $feedService,
ItemService $itemService,
ILogger $logger,
- $UserId,
$LoggerParameters
) {
- parent::__construct($appName, $request);
+ parent::__construct($appName, $request, $userSession);
$this->feedService = $feedService;
$this->itemService = $itemService;
- $this->userId = $UserId;
$this->logger = $logger;
$this->loggerParams = $LoggerParameters;
$this->serializer = new EntityApiSerializer('feeds');
@@ -62,14 +63,14 @@ class FeedApiController extends ApiController
{
$result = [
- 'starredCount' => $this->itemService->starredCount($this->userId),
- 'feeds' => $this->feedService->findAll($this->userId)
+ 'starredCount' => $this->itemService->starredCount($this->getUserId()),
+ 'feeds' => $this->feedService->findAll($this->getUserId())
];
try {
$result['newestItemId'] =
- $this->itemService->getNewestItemId($this->userId);
+ $this->itemService->getNewestItemId($this->getUserId());
// in case there are no items, ignore
} catch(ServiceNotFoundException $ex) {
@@ -91,14 +92,14 @@ class FeedApiController extends ApiController
public function create($url, $folderId=0)
{
try {
- $this->feedService->purgeDeleted($this->userId, false);
+ $this->feedService->purgeDeleted($this->getUserId(), false);
- $feed = $this->feedService->create($url, $folderId, $this->userId);
+ $feed = $this->feedService->create($url, $folderId, $this->getUserId());
$result = ['feeds' => [$feed]];
try {
$result['newestItemId'] =
- $this->itemService->getNewestItemId($this->userId);
+ $this->itemService->getNewestItemId($this->getUserId());
// in case there are no items, ignore
} catch(ServiceNotFoundException $ex) {
@@ -125,7 +126,7 @@ class FeedApiController extends ApiController
public function delete($feedId)
{
try {
- $this->feedService->delete($feedId, $this->userId);
+ $this->feedService->delete($feedId, $this->getUserId());
} catch(ServiceNotFoundException $ex) {
return $this->error($ex, Http::STATUS_NOT_FOUND);
}
@@ -144,7 +145,7 @@ class FeedApiController extends ApiController
*/
public function read($feedId, $newestItemId)
{
- $this->itemService->readFeed($feedId, $newestItemId, $this->userId);
+ $this->itemService->readFeed($feedId, $newestItemId, $this->getUserId());
}
@@ -161,7 +162,7 @@ class FeedApiController extends ApiController
{
try {
$this->feedService->patch(
- $feedId, $this->userId, ['folderId' => $folderId]
+ $feedId, $this->getUserId(), ['folderId' => $folderId]
);
} catch(ServiceNotFoundException $ex) {
return $this->error($ex, Http::STATUS_NOT_FOUND);
@@ -184,7 +185,7 @@ class FeedApiController extends ApiController
{
try {
$this->feedService->patch(
- $feedId, $this->userId, ['title' => $feedTitle]
+ $feedId, $this->getUserId(), ['title' => $feedTitle]
);
} catch(ServiceNotFoundException $ex) {
return $this->error($ex, Http::STATUS_NOT_FOUND);
diff --git a/lib/Controller/FolderApiController.php b/lib/Controller/FolderApiController.php
index b24ae9acb..348fefda6 100644
--- a/lib/Controller/FolderApiController.php
+++ b/lib/Controller/FolderApiController.php
@@ -7,13 +7,16 @@
*
* @author Alessandro Cosentino <cosenal@gmail.com>
* @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @author David Guillot <david@guillot.me>
* @copyright 2012 Alessandro Cosentino
* @copyright 2012-2014 Bernhard Posselt
+ * @copyright 2018 David Guillot
*/
namespace OCA\News\Controller;
use \OCP\IRequest;
+use \OCP\IUserSession;
use \OCP\AppFramework\Http;
use \OCA\News\Service\FolderService;
@@ -30,19 +33,17 @@ class FolderApiController extends ApiController
private $folderService;
private $itemService;
- private $userId;
private $serializer;
public function __construct($appName,
IRequest $request,
+ IUserSession $userSession,
FolderService $folderService,
- ItemService $itemService,
- $UserId
+ ItemService $itemService
) {
- parent::__construct($appName, $request);
+ parent::__construct($appName, $request, $userSession);
$this->folderService = $folderService;
$this->itemService = $itemService;
- $this->userId = $UserId;
$this->serializer = new EntityApiSerializer('folders');
}
@@ -55,7 +56,7 @@ class FolderApiController extends ApiController
public function index()
{
return $this->serializer->serialize(
- $this->folderService->findAll($this->userId)
+ $this->folderService->findAll($this->getUserId())
);
}
@@ -71,9 +72,9 @@ class FolderApiController extends ApiController
public function create($name)
{
try {
- $this->folderService->purgeDeleted($this->userId, false);
+ $this->folderService->purgeDeleted($this->getUserId(), false);
return $this->serializer->serialize(
- $this->folderService->create($name, $this->userId)
+ $this->folderService->create($name, $this->getUserId())
);
} catch(ServiceValidationException $ex) {
return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
@@ -94,7 +95,7 @@ class FolderApiController extends ApiController
public function delete($folderId)
{
try {
- $this->folderService->delete($folderId, $this->userId);
+ $this->folderService->delete($folderId, $this->getUserId());
} catch(ServiceNotFoundException $ex) {
return $this->error($ex, Http::STATUS_NOT_FOUND);
}
@@ -114,7 +115,7 @@ class FolderApiController extends ApiController
public function update($folderId, $name)
{
try {
- $this->folderService->rename($folderId, $name, $this->userId);
+ $this->folderService->rename($folderId, $name, $this->getUserId());
} catch(ServiceValidationException $ex) {
return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
@@ -138,7 +139,7 @@ class FolderApiController extends ApiController
*/
public function read($folderId, $newestItemId)
{
- $this->itemService->readFolder($folderId, $newestItemId, $this->userId);
+ $this->itemService->readFolder($folderId, $newestItemId, $this->getUserId());
}
diff --git a/lib/Controller/ItemApiController.php b/lib/Controller/ItemApiController.php
index ae523a5f1..601a3b409 100644
--- a/lib/Controller/ItemApiController.php
+++ b/lib/Controller/ItemApiController.php
@@ -7,13 +7,16 @@
*
* @author Alessandro Cosentino <cosenal@gmail.com>
* @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @author David Guillot <david@guillot.me>
* @copyright 2012 Alessandro Cosentino
* @copyright 2012-2014 Bernhard Posselt
+ * @copyright 2018 David Guillot
*/
namespace OCA\News\Controller;
use \OCP\IRequest;
+use \OCP\IUserSession;
use \OCP\AppFramework\Http;
use \OCA\News\Service\ItemService;
@@ -25,17 +28,15 @@ class ItemApiController extends ApiController
use JSONHttpError;
private $itemService;
- private $userId;
private $serializer;
public function __construct($appName,
IRequest $request,
- ItemService $itemService,
- $UserId
+ IUserSession $userSession,
+ ItemService $itemService
) {
- parent::__construct($appName, $request);
+ parent::__construct($appName, $request, $userSession);
$this->itemService = $itemService;
- $this->userId = $UserId;
$this->serializer = new EntityApiSerializer('items');
}
@@ -59,7 +60,7 @@ class ItemApiController extends ApiController
return $this->serializer->serialize(
$this->itemService->findAll(
$id, $type, $batchSize, $offset, $getRead, $oldestFirst,
- $this->userId
+ $this->getUserId()
)
);
}
@@ -86,7 +87,7 @@ class ItemApiController extends ApiController
return $this->serializer->serialize(
$this->itemService->findAllNew(
$id, $type, $paddedLastModified,
- true, $this->userId
+ true, $this->getUserId()
)
);
}
@@ -95,7 +96,7 @@ class ItemApiController extends ApiController
private function setRead($isRead, $itemId)
{
try {
- $this->itemService->read($itemId, $isRead, $this->userId);
+ $this->itemService->read($itemId, $isRead, $this->getUserId());
} catch(ServiceNotFoundException $ex){
return $this->error($ex, Http::STATUS_NOT_FOUND);
}
@@ -136,7 +137,7 @@ class ItemApiController extends ApiController
{
try {
$this->itemService->star(
- $feedId, $guidHash, $isStarred, $this->userId
+ $feedId, $guidHash, $isStarred, $this->getUserId()
);
} catch(ServiceNotFoundException $ex){
return $this->error($ex, Http::STATUS_NOT_FOUND);
@@ -185,7 +186,7 @@ class ItemApiController extends ApiController
*/
public function readAll($newestItemId)
{
- $this->itemService->readAll($newestItemId, $this->userId);
+ $this->itemService->readAll($newestItemId, $this->getUserId());
}
@@ -193,7 +194,7 @@ class ItemApiController extends ApiController
{
foreach($items as $id) {
try {
- $this->itemService->read($id, $isRead, $this->userId);
+ $this->itemService->read($id, $isRead, $this->getUserId());
} catch(ServiceNotFoundException $ex) {
continue;
}
@@ -233,7 +234,7 @@ class ItemApiController extends ApiController
try {
$this->itemService->star(
$item['feedId'], $item['guidHash'],
- $isStarred, $this->userId
+ $isStarred, $this->getUserId()
);
} catch(ServiceNotFoundException $ex) {
continue;
diff --git a/lib/Controller/UserApiController.php b/lib/Controller/UserApiController.php
index cb3b8b419..2e0b04b5c 100644
--- a/lib/Controller/UserApiController.php
+++ b/lib/Controller/UserApiController.php
@@ -7,8 +7,10 @@
*
* @author Alessandro Cosentino <cosenal@gmail.com>
* @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @author David Guillot <david@guillot.me>
* @copyright 2012 Alessandro Cosentino
* @copyright 2012-2014 Bernhard Posselt
+ * @copyright 2018 David Guillot
*/
namespace OCA\News\Controller;
@@ -30,8 +32,7 @@ class UserApiController extends ApiController
IUserSession $userSession,
IRootFolder $rootFolder
) {
- parent::__construct($appName, $request);
- $this->userSession = $userSession;
+ parent::__construct($appName, $request, $userSession);
$this->rootFolder = $rootFolder;
}
@@ -42,7 +43,7 @@ class UserApiController extends ApiController
*/
public function index()
{
- $user = $this->userSession->getUser();
+ $user = $this->getUser();
// find the avatar
$jpgAvatar = '/' . $user->getUID() . '/avatar.jpg';
diff --git a/lib/Controller/UtilityApiController.php b/lib/Controller/UtilityApiController.php
index 7fc6403c8..f88230c3b 100644
--- a/lib/Controller/UtilityApiController.php
+++ b/lib/Controller/UtilityApiController.php
@@ -7,14 +7,17 @@
*
* @author Alessandro Cosentino <cosenal@gmail.com>
* @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @author David Guillot <david@guillot.me>
* @copyright 2012 Alessandro Cosentino
* @copyright 2012-2014 Bernhard Posselt
+ * @copyright 2018 David Guillot
*/
namespace OCA\News\Controller;
use \OCP\IRequest;
use \OCP\IConfig;
+use \OCP\IUserSession;
use \OCP\AppFramework\Http;
use \OCA\News\Utility\Updater;
@@ -30,11 +33,12 @@ class UtilityApiController extends ApiController
public function __construct($appName,
IRequest $request,
+ IUserSession $userSession,
Updater $updater,
IConfig $settings,
StatusService $statusService
) {
- parent::__construct($appName, $request);
+ parent::__construct($appName, $request, $userSession);
$this->updater = $updater;
$this->settings = $settings;
$this->statusService = $statusService;