1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - maps |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Vinzenz Rosenkranz <[email protected]> |
9
|
|
|
* @copyright Vinzenz Rosenkranz 2015 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Maps\Controller; |
13
|
|
|
|
14
|
|
|
use OCA\Maps\Db\ApiKey; |
15
|
|
|
use OCA\Maps\Db\ApiKeyMapper; |
16
|
|
|
use \OCP\IRequest; |
17
|
|
|
use \OCP\AppFramework\Http\JSONResponse; |
18
|
|
|
use \OCP\AppFramework\ApiController; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class ApiKeyController extends ApiController { |
22
|
|
|
|
23
|
|
|
private $userId; |
24
|
|
|
private $apiKeyMapper; |
25
|
|
|
|
26
|
|
|
public function __construct($appName, IRequest $request, ApiKeyMapper $apiKeyMapper, $userId) { |
27
|
|
|
parent::__construct($appName, $request); |
28
|
|
|
$this->apiKeyMapper = $apiKeyMapper; |
29
|
|
|
$this->userId = $userId; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @NoAdminRequired |
34
|
|
|
* |
35
|
|
|
* @param $key string |
36
|
|
|
* @param $id int |
37
|
|
|
* @return JSONResponse |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function updateKey($key, $id) { |
|
|
|
|
40
|
|
|
|
41
|
|
|
$apikey = new ApiKey(); |
42
|
|
|
$apikey->setId($id); |
43
|
|
|
$apikey->setApiKey($key); |
44
|
|
|
|
45
|
|
|
/* Only save apiKey if it exists in db */ |
46
|
|
|
try { |
47
|
|
|
$this->apiKeyMapper->find($id); |
48
|
|
|
return new JSONResponse($this->apiKeyMapper->update($apikey)); |
49
|
|
|
} catch(\OCP\AppFramework\Db\DoesNotExistException $e) { |
|
|
|
|
50
|
|
|
return new JSONResponse([ |
51
|
|
|
'error' => $e->getMessage() |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @NoAdminRequired |
58
|
|
|
* |
59
|
|
|
* @param $key string |
60
|
|
|
* @return JSONResponse |
61
|
|
|
*/ |
62
|
|
|
public function addKey($key){ |
63
|
|
|
$apikey = new ApiKey(); |
64
|
|
|
$apikey->setApiKey($key); |
65
|
|
|
$apikey->setUserId($this->userId); |
66
|
|
|
|
67
|
|
|
/* @var $apikey ApiKey */ |
68
|
|
|
$apikey = $this->apiKeyMapper->insert($apikey); |
69
|
|
|
|
70
|
|
|
$response = array('id'=> $apikey->getId()); |
71
|
|
|
return new JSONResponse($response); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @NoAdminRequired |
76
|
|
|
* |
77
|
|
|
* @return JSONResponse |
78
|
|
|
*/ |
79
|
|
|
public function getKey(){ |
80
|
|
|
$apikey = new ApiKey(); |
81
|
|
|
try { |
82
|
|
|
$apikey = $this->apiKeyMapper->findByUser($this->userId); |
83
|
|
|
} catch(\OCP\AppFramework\Db\DoesNotExistException $e) { |
|
|
|
|
84
|
|
|
$apikey->setUserId($this->userId); |
85
|
|
|
} |
86
|
|
|
return new JSONResponse($apikey); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @NoAdminRequired |
91
|
|
|
* |
92
|
|
|
* @param $id int |
93
|
|
|
* @return JSONResponse |
94
|
|
|
*/ |
95
|
|
|
public function removeApiKey($id){ |
96
|
|
|
$apikey = $this->apiKeyMapper->find($id); |
97
|
|
|
if($apikey->userId == $this->userId) { |
98
|
|
|
$this->apiKeyMapper->delete($apikey); |
99
|
|
|
} |
100
|
|
|
return new JSONResponse(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.