1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nextcloud - passman |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Sander Brand <[email protected]> |
9
|
|
|
* @copyright Sander Brand 2016 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Passman\Controller; |
13
|
|
|
|
14
|
|
|
use OCP\IConfig; |
15
|
|
|
use OCP\IRequest; |
16
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
17
|
|
|
use OCP\AppFramework\ApiController; |
18
|
|
|
use OCA\Passman\Service\CredentialService; |
19
|
|
|
use \OCP\App; |
20
|
|
|
|
21
|
|
|
class InternalController extends ApiController { |
22
|
|
|
private $userId; |
23
|
|
|
private $credentialService; |
24
|
|
|
private $config; |
25
|
|
|
|
26
|
|
|
public function __construct($AppName, |
27
|
|
|
IRequest $request, |
28
|
|
|
$UserId, |
29
|
|
|
CredentialService $credentialService, |
30
|
|
|
IConfig $config |
31
|
|
|
) { |
32
|
|
|
parent::__construct( |
33
|
|
|
$AppName, |
34
|
|
|
$request, |
35
|
|
|
'GET, POST, DELETE, PUT, PATCH, OPTIONS', |
36
|
|
|
'Authorization, Content-Type, Accept', |
37
|
|
|
86400); |
38
|
|
|
$this->userId = $UserId; |
39
|
|
|
$this->credentialService = $credentialService; |
40
|
|
|
$this->config = $config; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @NoAdminRequired |
45
|
|
|
*/ |
46
|
1 |
|
public function remind($credential_id) { |
47
|
1 |
|
$credential = $this->credentialService->getCredentialById($credential_id, $this->userId); |
48
|
1 |
|
if($credential) { |
49
|
|
|
$credential->setExpireTime(time() + (24 * 60 * 60)); |
50
|
|
|
$this->credentialService->upd($credential); |
51
|
|
|
|
52
|
|
|
$manager = \OC::$server->getNotificationManager(); |
53
|
|
|
$notification = $manager->createNotification(); |
54
|
|
|
$notification->setApp('passman') |
55
|
|
|
->setObject('credential', $credential_id) |
56
|
|
|
->setUser($this->userId); |
57
|
|
|
$manager->markProcessed($notification); |
58
|
|
|
} |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @NoAdminRequired |
63
|
|
|
*/ |
64
|
1 |
|
public function read($credential_id) { |
65
|
|
|
|
66
|
1 |
|
$credential = $this->credentialService->getCredentialById($credential_id, $this->userId); |
67
|
1 |
View Code Duplication |
if($credential) { |
|
|
|
|
68
|
|
|
$credential->setExpireTime(0); |
69
|
|
|
$this->credentialService->upd($credential); |
70
|
|
|
|
71
|
|
|
$manager = \OC::$server->getNotificationManager(); |
72
|
|
|
$notification = $manager->createNotification(); |
73
|
|
|
$notification->setApp('passman') |
74
|
|
|
->setObject('credential', $credential_id) |
75
|
|
|
->setUser($this->userId); |
76
|
|
|
$manager->markProcessed($notification); |
77
|
|
|
} |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @NoAdminRequired |
82
|
|
|
* @NoCSRFRequired |
83
|
|
|
*/ |
84
|
|
|
public function getAppVersion() { |
85
|
|
|
$AppInstance = new App(); |
86
|
|
|
return new JSONResponse(array('version' => $AppInstance->getAppInfo("passman")["version"])); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @NoAdminRequired |
91
|
|
|
*/ |
92
|
|
|
public function generatePerson() { |
93
|
|
|
$context = [ 'http' => [ 'method' => 'GET' ], 'ssl' => [ 'verify_peer' => false, 'allow_self_signed'=> true ] ]; |
94
|
|
|
$context = stream_context_create($context); |
95
|
|
|
$random_person = json_decode(file_get_contents('http://api.namefake.com/', false, $context)); |
96
|
|
|
return new JSONResponse($random_person); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @NoAdminRequired |
101
|
|
|
* @NoCSRFRequired |
102
|
|
|
*/ |
103
|
1 |
|
public function getSettings() { |
104
|
|
|
$settings = array( |
105
|
1 |
|
'link_sharing_enabled' => intval($this->config->getAppValue('passman', 'link_sharing_enabled', 1)), |
106
|
1 |
|
'user_sharing_enabled' => intval($this->config->getAppValue('passman', 'user_sharing_enabled', 1)), |
107
|
1 |
|
'vault_key_strength' => intval($this->config->getAppValue('passman', 'vault_key_strength', 3)), |
108
|
1 |
|
'check_version' => intval($this->config->getAppValue('passman', 'check_version', 1)), |
109
|
1 |
|
'https_check' => intval($this->config->getAppValue('passman', 'https_check', 1)), |
110
|
1 |
|
'disable_contextmenu' => intval($this->config->getAppValue('passman', 'disable_contextmenu', 1)), |
111
|
1 |
|
); |
112
|
1 |
|
return new JSONResponse($settings); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @NoCSRFRequired |
117
|
|
|
*/ |
118
|
1 |
|
public function saveSettings($key, $value) { |
119
|
1 |
|
if (is_numeric($value)) { |
120
|
|
|
$value = intval($value); |
121
|
|
|
} |
122
|
1 |
|
$this->config->setAppValue('passman', $key, $value); |
123
|
1 |
|
} |
124
|
|
|
|
125
|
|
|
} |
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.