Completed
Push — master ( 6135d2...dffc50 )
by Sander
14:38 queued 12:08
created

InternalController::saveSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
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 1
	public function remind($credential_id) {
42 1
		$credential = $this->credentialService->getCredentialById($credential_id, $this->userId);
43 1
		if($credential) {
44
			$credential->setExpireTime(time() + (24 * 60 * 60));
45
			$this->credentialService->upd($credential);
46
47
			$manager = \OC::$server->getNotificationManager();
48
			$notification = $manager->createNotification();
49
			$notification->setApp('passman')
50
				->setObject('credential', $credential_id)
51
				->setUser($this->userId);
52
			$manager->markProcessed($notification);
53
		}
54 1
	}
55
56
	/**
57
	 * @NoAdminRequired
58
	 */
59 1
	public function read($credential_id) {
60
61 1
		$credential = $this->credentialService->getCredentialById($credential_id, $this->userId);
62 1 View Code Duplication
		if($credential) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
63
			$credential->setExpireTime(0);
64
			$this->credentialService->upd($credential);
65
66
			$manager = \OC::$server->getNotificationManager();
67
			$notification = $manager->createNotification();
68
			$notification->setApp('passman')
69
				->setObject('credential', $credential_id)
70
				->setUser($this->userId);
71
			$manager->markProcessed($notification);
72
		}
73 1
	}
74
75
	/**
76
	 * @NoAdminRequired
77
	 * @NoCSRFRequired
78
	 */
79
	public function getAppVersion() {
80
		$AppInstance = new App();
81
		return new JSONResponse(array('version' => $AppInstance->getAppInfo("passman")["version"]));
82
	}
83
84
	/**
85
	 * @NoAdminRequired
86
	 */
87 1
	public function generatePerson() {
88 1
		$random_person = json_decode(file_get_contents('http://api.namefake.com/'));
89 1
		return new JSONResponse($random_person);
90
	}
91
92
	/**
93
	 * @NoAdminRequired
94
	 * @NoCSRFRequired
95
	 */
96 1
	public function getSettings() {
97
		$settings = array(
98 1
			'link_sharing_enabled' => intval($this->config->getAppValue('passman', 'link_sharing_enabled', 1)),
99 1
			'user_sharing_enabled' => intval($this->config->getAppValue('passman', 'user_sharing_enabled', 1)),
100 1
			'vault_key_strength' => intval($this->config->getAppValue('passman', 'vault_key_strength', 3)),
101 1
			'check_version' => intval($this->config->getAppValue('passman', 'check_version', 1)),
102 1
			'https_check' => intval($this->config->getAppValue('passman', 'https_check', 1)),
103 1
			'disable_contextmenu' => intval($this->config->getAppValue('passman', 'disable_contextmenu', 1)),
104
		);
105 1
		return new JSONResponse($settings);
106
	}
107
108
	/**
109
	 * @NoCSRFRequired
110
	 */
111 1
	public function saveSettings($key, $value) {
112 1
		if (is_numeric($value)) {
113
			$value = intval($value);
114
		}
115 1
		$this->config->setAppValue('passman', $key, $value);
116 1
	}
117
118
}