Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
small fixes and updates
Browse files Browse the repository at this point in the history
  • Loading branch information
v1r0x committed Feb 3, 2016
1 parent aae6daf commit d9b4e31
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 28 deletions.
10 changes: 9 additions & 1 deletion appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use \OCA\Maps\Db\DeviceMapper;
use \OCA\Maps\Db\LocationMapper;
use \OCA\Maps\Db\FavoriteMapper;
use \OCA\Maps\Db\ApiKeyMapper;
use \OCA\Maps\Controller\PageController;
use \OCA\Maps\Controller\LocationController;
use \OCA\Maps\Controller\FavoriteController;
Expand All @@ -41,7 +42,8 @@ public function __construct (array $urlParams=array()) {
$c->query('Request'),
$c->query('UserId'),
$c->query('CacheManager'),
$c->query('DeviceMapper')
$c->query('DeviceMapper'),
$c->query('ApiKeyMapper')
);
});
$container->registerService('LocationController', function($c) {
Expand Down Expand Up @@ -89,6 +91,12 @@ public function __construct (array $urlParams=array()) {
$server->getDb()
);
});
$container->registerService('ApiKeyMapper', function($c) use ($server) {
/** @var SimpleContainer $c */
return new ApiKeyMapper(
$server->getDb()
);
});

}

Expand Down
23 changes: 23 additions & 0 deletions appinfo/database.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,27 @@
</field>
</declaration>
</table>
<table>
<name>*dbprefix*maps_apikeys</name>
<declaration>
<field>
<name>id</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<autoincrement>1</autoincrement>
<length>41</length>
</field>
<field>
<name>user_id</name>
<type>text</type>
<length>64</length>
</field>
<field>
<name>api_key</name>
<type>text</type>
<length>64</length>
</field>
</declaration>
</table>
</database>
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
<author>Sander Brand</author>
<version>0.0.6</version>
<requiremin>7</requiremin>
</info>
</info>
3 changes: 3 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@
array('name' => 'favorite#update_favorite', 'url' => '/api/1.0/favorite/updateFavorite', 'verb' => 'POST'),
array('name' => 'favorite#get_favorites_by_name', 'url' => '/api/1.0/favorite/getFavoritesByName', 'verb' => 'POST'),

array('name' => 'apikey#get_key', 'url' => '/api/1.0/apikey/getKey', 'verb' => 'POST'),
array('name' => 'apikey#add_key', 'url' => '/api/1.0/apikey/addKey', 'verb' => 'POST'),

)));
103 changes: 103 additions & 0 deletions controller/apikeycontroller.php
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();
}

}
29 changes: 23 additions & 6 deletions controller/pagecontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace OCA\Maps\Controller;

use \OCA\Maps\Db\ApiKey;
use \OCA\Maps\Db\DeviceMapper;
use \OCA\Maps\Db\ApiKeyMapper;
use \OCP\IRequest;
use \OCP\AppFramework\Http\TemplateResponse;
use \OCP\AppFramework\Controller;
Expand All @@ -22,13 +24,16 @@ class PageController extends Controller {
private $userId;
private $cacheManager;
private $deviceMapper;
private $apiKeyMapper;
public function __construct($appName, IRequest $request, $userId,
CacheManager $cacheManager,
DeviceMapper $deviceMapper) {
DeviceMapper $deviceMapper,
ApiKeyMapper $apiKeyMapper) {
parent::__construct($appName, $request);
$this -> userId = $userId;
$this -> cacheManager = $cacheManager;
$this -> deviceMapper = $deviceMapper;
$this -> apiKeyMapper = $apiKeyMapper;
}

/**
Expand All @@ -52,11 +57,23 @@ public function index() {
// marker icons
$csp->addAllowedImageDomain('https://api.tiles.mapbox.com');
// inline images
$csp->addAllowedScriptDomain('data:');
// nominatim geocoder
$csp->addAllowedScriptDomain('http://nominatim.openstreetmap.org/search?q=*');
$csp->addAllowedScriptDomain('http://nominatim.openstreetmap.org/reverse');
$csp->addAllowedConnectDomain('http://router.project-osrm.org');
$csp->addAllowedImageDomain('data:');
$tmpkey = new ApiKey();
try {
$tmpkey = $this->apiKeyMapper->findByUser($this->userId);
} catch(\OCP\AppFramework\Db\DoesNotExistException $e) {
$tmpkey->setUserId($this->userId);
}
if($tmpkey->apiKey != null && strlen($tmpkey->apiKey) > 0) {
// mapzen geocoder
$csp->addAllowedConnectDomain('http://search.mapzen.com/v1/search?');
$csp->addAllowedConnectDomain('http://search.mapzen.com/v1/reverse?');
} else {
// nominatim geocoder
$csp->addAllowedScriptDomain('http://nominatim.openstreetmap.org/search?q=*');
$csp->addAllowedScriptDomain('http://nominatim.openstreetmap.org/reverse');
$csp->addAllowedConnectDomain('http://router.project-osrm.org');
}
$response->setContentSecurityPolicy($csp);
}
return $response;
Expand Down
1 change: 1 addition & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ ul.geocoder-list {
background: white;
width: 400px;
font-size: 10px;
position: absolute;
}

li.geocoder-list-item {
Expand Down
15 changes: 15 additions & 0 deletions db/apikey.php
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;
}
44 changes: 44 additions & 0 deletions db/apikeymapper.php
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);
}
}
Loading

0 comments on commit d9b4e31

Please sign in to comment.