Completed
Push — master ( 9b8394...483b13 )
by Sander
02:27
created

SettingsService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
/**
3
 * Nextcloud - passman
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Passman\Service;
25
26
use OCP\IConfig;
27
28
29
class SettingsService {
30
31
	private $userId;
32
	private $config;
33
	private $appName;
34
	public $settings;
35
36
	private $numeric_settings = array(
37
		'link_sharing_enabled',
38
		'user_sharing_enabled',
39
		'vault_key_strength',
40
		'check_version',
41
		'https_check',
42
		'disable_contextmenu'
43
	);
44
45
	public function __construct($UserId, IConfig $config, $AppName) {
46
		$this->userId = $UserId;
47
		$this->config = $config;
48
		$this->appName = $AppName;
49
	}
50
51
	/**
52
	 * Get all app settings
53
	 *
54
	 * @return array
55
	 */
56
	public function getAppSettings() {
57
		$this->settings = array(
58
			'link_sharing_enabled' => intval($this->config->getAppValue('passman', 'link_sharing_enabled', 1)),
59
			'user_sharing_enabled' => intval($this->config->getAppValue('passman', 'user_sharing_enabled', 1)),
60
			'vault_key_strength' => intval($this->config->getAppValue('passman', 'vault_key_strength', 3)),
61
			'check_version' => intval($this->config->getAppValue('passman', 'check_version', 1)),
62
			'https_check' => intval($this->config->getAppValue('passman', 'https_check', 1)),
63
			'disable_contextmenu' => intval($this->config->getAppValue('passman', 'disable_contextmenu', 1)),
64
		);
65
		return $this->settings;
66
	}
67
68
	/**
69
	 * Get a app setting
70
	 *
71
	 * @param $key string
72
	 * @param null $default_value The default value if key does not exist
73
	 * @return mixed
74
	 */
75
	public function getAppSetting($key, $default_value = null) {
76
		$value = $this->config->getAppValue('passman', $key, $default_value);
77
		if (in_array($key, $this->numeric_settings)) {
78
			$value = intval($value);
79
		}
80
		return $value;
81
	}
82
83
	/**
84
	 * Set a app setting
85
	 *
86
	 * @param $key string Setting name
87
	 * @param $value mixed Value of the setting
88
	 */
89
	public function setAppSetting($key, $value) {
90
		$this->config->setAppValue('passman', $key, $value);
91
	}
92
93
	/**
94
	 * Set a user setting
95
	 *
96
	 * @param $key string Setting name
97
	 * @param $value mixed Value of the setting
98
	 */
99
100
	public function setUserSetting($key, $value){
101
		return $this->config->setUserValue($this->userId, $this->appName, $key, $value);
102
	}
103
104
	/**
105
	 * Check if an setting is enabled (value of 1)
106
	 *
107
	 * @param $setting
108
	 * @return bool
109
	 */
110
	public function isEnabled($setting){
111
		$value = intval($this->config->getAppValue('passman', $setting, false));
112
		return ($value === 1);
113
	}
114
}