Completed
Branch master (8ca3ab)
by Sander
03:07
created

InternalController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 5
crap 2
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($AppName, $request);
33
		$this->userId = $UserId;
34
		$this->credentialService = $credentialService;
35
		$this->config = $config;
36
	}
37
38
	/**
39
	 * @NoAdminRequired
40
	 */
41
	public function remind($credential_id) {
42
		$credential = $this->credentialService->getCredentialById($credential_id, $this->userId);
43
		$credential->setExpireTime(time() + (24 * 60 * 60));
44
		$this->credentialService->upd($credential);
45
46
		$manager = \OC::$server->getNotificationManager();
47
		$notification = $manager->createNotification();
48
		$notification->setApp('passman')
49
			->setObject('credential', $credential_id)
50
			->setUser($this->userId);
51
		$manager->markProcessed($notification);
52
	}
53
54
	/**
55
	 * @NoAdminRequired
56
	 */
57
	public function read($credential_id) {
58
59
		$credential = $this->credentialService->getCredentialById($credential_id, $this->userId);
60
		$credential->setExpireTime(0);
61
		$this->credentialService->upd($credential);
62
63
		$manager = \OC::$server->getNotificationManager();
64
		$notification = $manager->createNotification();
65
		$notification->setApp('passman')
66
			->setObject('credential', $credential_id)
67
			->setUser($this->userId);
68
		$manager->markProcessed($notification);
69
	}
70
71
	/**
72
	 * @NoAdminRequired
73
	 * @NoCSRFRequired
74
	 */
75
	public function getAppVersion() {
76
		$AppInstance = new App();
77
		return new JSONResponse(array('version' => $AppInstance->getAppInfo("passman")["version"]));
78
	}
79
80
	/**
81
	 * @NoAdminRequired
82
	 */
83 1
	public function generatePerson() {
84 1
		$random_person = json_decode(file_get_contents('http://api.namefake.com/'));
85 1
		return new JSONResponse($random_person);
86
	}
87
88
	/**
89
	 * @NoAdminRequired
90
	 * @NoCSRFRequired
91
	 */
92
	public function getSettings() {
93
		$settings = array(
94
			'link_sharing_enabled' => intval($this->config->getAppValue('passman', 'link_sharing_enabled', 1)),
95
			'user_sharing_enabled' => intval($this->config->getAppValue('passman', 'user_sharing_enabled', 1)),
96
			'vault_key_strength' => intval($this->config->getAppValue('passman', 'vault_key_strength', 3)),
97
			'check_version' => intval($this->config->getAppValue('passman', 'check_version', 1)),
98
			'https_check' => intval($this->config->getAppValue('passman', 'https_check', 1)),
99
			'disable_contextmenu' => intval($this->config->getAppValue('passman', 'disable_contextmenu', 1)),
100
		);
101
		return new JSONResponse($settings);
102
	}
103
104
	/**
105
	 * @NoCSRFRequired
106
	 */
107
	public function saveSettings($key, $value) {
108
		if (is_numeric($value)) {
109
			$value = intval($value);
110
		}
111
		$this->config->setAppValue('passman', $key, $value);
112
	}
113
114
}