This repository has been archived by the owner on Nov 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
299 additions
and
506 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
/** | ||
* ownCloud - maps | ||
* | ||
* This file is licensed under the Affero General Public License version 3 or | ||
* later. See the COPYING file. | ||
* | ||
* @author Vinzenz Rosenkranz <[email protected]> | ||
* @copyright Vinzenz Rosenkranz 2015 | ||
*/ | ||
|
||
namespace OCA\Maps\Controller; | ||
|
||
use OCA\Maps\Db\ApiKey; | ||
use OCA\Maps\Db\ApiKeyMapper; | ||
use \OCP\IRequest; | ||
use \OCP\AppFramework\Http\JSONResponse; | ||
use \OCP\AppFramework\ApiController; | ||
|
||
|
||
class ApiKeyController extends ApiController { | ||
|
||
private $userId; | ||
private $apiKeyMapper; | ||
|
||
public function __construct($appName, IRequest $request, ApiKeyMapper $apiKeyMapper, $userId) { | ||
parent::__construct($appName, $request); | ||
$this->apiKeyMapper = $apiKeyMapper; | ||
$this->userId = $userId; | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* | ||
* @param $key string | ||
* @param $id int | ||
* @return JSONResponse | ||
*/ | ||
public function updateKey($key, $id) { | ||
|
||
$apikey = new ApiKey(); | ||
$apikey->setId($id); | ||
$apikey->setApiKey($key); | ||
|
||
/* Only save apiKey if it exists in db */ | ||
try { | ||
$this->apiKeyMapper->find($id); | ||
return new JSONResponse($this->apiKeyMapper->update($apikey)); | ||
} catch(\OCP\AppFramework\Db\DoesNotExistException $e) { | ||
return new JSONResponse([ | ||
'error' => $e->getMessage() | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* | ||
* @param $key string | ||
* @return JSONResponse | ||
*/ | ||
public function addKey($key){ | ||
$apikey = new ApiKey(); | ||
$apikey->setApiKey($key); | ||
$apikey->setUserId($this->userId); | ||
|
||
/* @var $apikey ApiKey */ | ||
$apikey = $this->apiKeyMapper->insert($apikey); | ||
|
||
$response = array('id'=> $apikey->getId()); | ||
return new JSONResponse($response); | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* | ||
* @return JSONResponse | ||
*/ | ||
public function getKey(){ | ||
$apikey = new ApiKey(); | ||
try { | ||
$apikey = $this->apiKeyMapper->findByUser($this->userId); | ||
} catch(\OCP\AppFramework\Db\DoesNotExistException $e) { | ||
$apikey->setUserId($this->userId); | ||
} | ||
return new JSONResponse($apikey); | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* | ||
* @param $id int | ||
* @return JSONResponse | ||
*/ | ||
public function removeApiKey($id){ | ||
$apikey = $this->apiKeyMapper->find($id); | ||
if($apikey->userId == $this->userId) { | ||
$this->apiKeyMapper->delete($apikey); | ||
} | ||
return new JSONResponse(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
namespace OCA\Maps\Db; | ||
|
||
use OCP\AppFramework\Db\Entity; | ||
|
||
/** | ||
* @method string getUserId() | ||
* @method void setUserId(string $value) | ||
* @method string getApiKey() | ||
* @method void setApiKey(string $value) | ||
*/ | ||
class ApiKey extends Entity { | ||
public $userId; | ||
public $apiKey; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
namespace OCA\Maps\Db; | ||
|
||
use OCP\AppFramework\Db\Mapper; | ||
use OCP\IDb; | ||
|
||
class ApiKeyMapper extends Mapper { | ||
|
||
public function __construct(IDB $db) { | ||
parent::__construct($db, 'maps_apikeys', '\OCA\Maps\Db\ApiKey'); | ||
} | ||
|
||
/** | ||
* @param int $id | ||
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found | ||
* @return ApiKey | ||
*/ | ||
public function find($id) { | ||
$sql = 'SELECT * FROM `*PREFIX*maps_apikeys` '. | ||
'WHERE `id` = ?'; | ||
return $this->findEntity($sql, [$id]); | ||
} | ||
|
||
/** | ||
* @param string $uid | ||
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found | ||
* @return ApiKey | ||
*/ | ||
public function findByUser($uid) { | ||
$sql = 'SELECT * FROM `*PREFIX*maps_apikeys` '. | ||
'WHERE `user_id` = ?'; | ||
return $this->findEntity($sql, [$uid]); | ||
} | ||
|
||
/** | ||
* @param int $limit | ||
* @param int $offset | ||
* @return ApiKey[] | ||
*/ | ||
public function findAll($limit=null, $offset=null) { | ||
$sql = 'SELECT * FROM `*PREFIX*maps_apikeys`'; | ||
return $this->findEntities($sql, $limit, $offset); | ||
} | ||
} |
Oops, something went wrong.