X-Git-Url: https://git.cweiske.de/grauphel.git/blobdiff_plain/3b7dde5d693bbbde491c66b47b0040fcfdc851da..ace226e665fa77d04a61dfd61818f6cce5df6861:/controller/apicontroller.php diff --git a/controller/apicontroller.php b/controller/apicontroller.php index 7f6bc67..688d0b0 100644 --- a/controller/apicontroller.php +++ b/controller/apicontroller.php @@ -16,8 +16,10 @@ namespace OCA\Grauphel\Controller; use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http\JSONResponse; +use \OCA\Grauphel\Lib\Client; use \OCA\Grauphel\Lib\NoteStorage; use \OCA\Grauphel\Lib\OAuth; +use \OCA\Grauphel\Lib\OAuthException; use \OCA\Grauphel\Lib\Dependencies; use \OCA\Grauphel\Lib\Response\ErrorResponse; @@ -34,13 +36,13 @@ use \OCA\Grauphel\Lib\Response\ErrorResponse; */ class ApiController extends Controller { - /** - * constructor of the controller + /** + * constructor of the controller * - * @param string $appName Name of the app - * @param IRequest $request Instance of the request - */ - public function __construct($appName, \OCP\IRequest $request, $user) + * @param string $appName Name of the app + * @param IRequest $request Instance of the request + */ + public function __construct($appName, \OCP\IRequest $request, $user) { parent::__construct($appName, $request); $this->user = $user; @@ -67,7 +69,7 @@ class ApiController extends Controller $urlGen = $deps->urlGen; try { - $provider = new \OAuthProvider(); + $provider = OAuth::getProvider(); $oauth->registerHandler($provider) ->registerAccessTokenHandler($provider); $provider->checkOAuthRequest( @@ -79,7 +81,7 @@ class ApiController extends Controller $token = $deps->tokens->load('access', $provider->token); $username = $token->user; - } catch (\OAuth_Exception $e) { + } catch (OAuthException $e) { return new ErrorResponse($e->getMessage()); } catch (\OAuthException $e) { if ($e->getCode() != OAUTH_PARAMETER_ABSENT) { @@ -104,6 +106,12 @@ class ApiController extends Controller 'api-version' => '1.0', ); + $cl = new Client(); + $client = $cl->getClient(); + if ($client !== false) { + $data['oauth_authorize_url'] .= '?client=' . urlencode($client); + } + if ($authenticated) { $data['user-ref'] = array( 'api-ref' => $urlGen->getAbsoluteURL( @@ -147,7 +155,7 @@ class ApiController extends Controller ) ) ); - $syncdata = $this->notes->loadSyncData($username); + $syncdata = $this->notes->loadSyncData(); $data = array( 'user-name' => $username, @@ -184,8 +192,8 @@ class ApiController extends Controller ) ) ); - $syncdata = $this->notes->loadSyncData($username); - return $this->fetchNotes($username, $syncdata); + $syncdata = $this->notes->loadSyncData(); + return $this->fetchNotes($syncdata); } /** @@ -205,17 +213,17 @@ class ApiController extends Controller ) ) ); - $syncdata = $this->notes->loadSyncData($username); + $syncdata = $this->notes->loadSyncData(); $res = $this->handleNoteSave($username, $syncdata); if ($res instanceof \OCP\AppFramework\Http\Response) { return $res; } - return $this->fetchNotes($username, $syncdata); + return $this->fetchNotes($syncdata); } - protected function fetchNotes($username, $syncdata) + protected function fetchNotes($syncdata) { $since = null; if (isset($_GET['since'])) { @@ -223,9 +231,9 @@ class ApiController extends Controller } if (isset($_GET['include_notes']) && $_GET['include_notes']) { - $notes = $this->notes->loadNotesFull($username, $since); + $notes = $this->notes->loadNotesFull($since); } else { - $notes = $this->notes->loadNotesOverview($username, $since); + $notes = $this->notes->loadNotesOverview($since); } //work around bug https://bugzilla.gnome.org/show_bug.cgi?id=734313 @@ -282,18 +290,18 @@ class ApiController extends Controller //owncloud converts object to array, so we reverse $noteUpdate = (object) $noteUpdate; - $note = $this->notes->load($username, $noteUpdate->guid); + $note = $this->notes->load($noteUpdate->guid); if (isset($noteUpdate->command) && $noteUpdate->command == 'delete') { - $this->notes->delete($username, $noteUpdate->guid); + $this->notes->delete($noteUpdate->guid); } else { $this->notes->update( $note, $noteUpdate, $syncdata->latestSyncRevision ); - $this->notes->save($username, $note); + $this->notes->save($note); } } - $this->notes->saveSyncData($username, $syncdata); + $this->notes->saveSyncData($syncdata); } /** @@ -303,17 +311,19 @@ class ApiController extends Controller * @NoCSRFRequired * @PublicPage */ - public function note() + public function note($username, $guid) { - //FIXME - $deps = Dependencies::get(); - $username = $deps->urlGen->loadUsername(); - $guid = $deps->urlGen->loadGuid(); - $oauth = new \OAuth(); - $oauth->setDeps($deps); - $oauth->verifyOAuthUser($username, $deps->urlGen->note($username, $guid)); + $this->verifyUser( + $username, + $this->deps->urlGen->getAbsoluteURL( + $this->deps->urlGen->linkToRoute( + 'grauphel.api.note', + array('username' => $username, 'guid' => $guid) + ) + ) + ); - $note = $this->notes->load($username, $guid, false); + $note = $this->notes->load($guid, false); if ($note === null) { header('HTTP/1.0 404 Not Found'); header('Content-type: text/plain'); @@ -321,8 +331,7 @@ class ApiController extends Controller exit(1); } - $data = array('note' => array($note)); - $deps->renderer->sendJson($data); + return new JSONResponse($note); } /** @@ -334,13 +343,17 @@ class ApiController extends Controller */ protected function verifyUser($username, $curUrl) { - if ($this->user !== null && $this->user->getUID() == $username) { + if ($this->user !== null && $this->user->getUid() == $username) { + $this->notes->setUsername($username); return true; } $oauth = new OAuth(); $oauth->setDeps($this->deps); $oauth->verifyOAuthUser($username, $curUrl); + + $this->notes->setUsername($username); + return true; } } ?>