Completed
Push — master ( 7a29a1...33d37f )
by Thomas
01:49
created

SecurityConfig::setIsSpecialCharactersEnforced()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * @author Semih Serhat Karakaya <[email protected]>
4
 * @copyright Copyright (c) 2017, ownCloud GmbH.
5
 * @license AGPL-3.0
6
 *
7
 * This code is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License, version 3,
9
 * as published by the Free Software Foundation.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License, version 3,
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
18
 */
19
20
namespace OCA\Security;
21
22
use OCP\IConfig;
23
/**
24
 * Class Config
25
 *
26
 * read/write config of the security policies
27
 *
28
 * @package OCA\Security
29
 */
30
class SecurityConfig {
31
32
	/** @var IConfig */
33
	private $config;
34
35
	/**
36
	 * Config constructor.
37
	 *
38
	 * @param IConfig $config
39
	 */
40
	public function __construct(IConfig $config) {
41
		$this->config = $config;
42
	}
43
44
	/**
45
	 * is brute force protection enabled
46
	 *
47
	 * @return array
48
	 */
49
	public function getAllSecurityConfigs() {
50
		return [
51
			'isBruteForceProtectionEnabled' => $this->getIsBruteForceProtectionEnabled(),
52
			'minPasswordLength' => $this->getMinPasswordLength(),
53
			'isUpperLowerCaseEnforced' => $this->getIsUpperLowerCaseEnforced(),
54
			'isNumericalCharsEnforced' => $this->getIsNumericCharactersEnforced(),
55
			'isSpecialCharsEnforced' => $this->getIsSpecialCharactersEnforced()
56
		];
57
	}
58
59
	/**
60
	 * is brute force protection enabled
61
	 *
62
	 * @return bool
63
	 */
64
	public function getIsBruteForceProtectionEnabled() {
65
		$enableBruteForceProtection = $this->config->getAppValue(
66
			'security',
67
			'enable_brute_force_protection',
68
			'0'
69
		);
70
		return boolval($enableBruteForceProtection);
71
	}
72
73
	/**
74
	 * get the enforced minimum length of passwords
75
	 *
76
	 * @return int
77
	 */
78
	public function getMinPasswordLength() {
79
		$minLength = $this->config->getAppValue('security', 'min_password_length', '8');
80
		return intval($minLength);
81
	}
82
	/**
83
	 * does the password need to contain upper and lower case characters
84
	 *
85
	 * @return bool
86
	 */
87
	public function getIsUpperLowerCaseEnforced() {
88
		$enforceUpperLowerCase = $this->config->getAppValue(
89
			'security',
90
			'enforce_upper_lower_case',
91
			'0'
92
		);
93
		return boolval($enforceUpperLowerCase);
94
	}
95
	/**
96
	 * does the password need to contain numeric characters
97
	 *
98
	 * @return bool
99
	 */
100
	public function getIsNumericCharactersEnforced() {
101
		$enforceNumericCharacters = $this->config->getAppValue(
102
			'security',
103
			'enforce_numeric_characters',
104
			'0'
105
		);
106
		return boolval($enforceNumericCharacters);
107
	}
108
	/**
109
	 * does the password need to contain special characters
110
	 *
111
	 * @return bool
112
	 */
113
	public function getIsSpecialCharactersEnforced() {
114
		$enforceSpecialCharacters = $this->config->getAppValue(
115
			'security',
116
			'enforce_special_characters',
117
			'0'
118
		);
119
		return boolval($enforceSpecialCharacters);
120
	}
121
	/**
122
	 * enable brute force protection
123
	 *
124
	 * @param bool $enableBruteForceProtection
125
	 */
126
	public function setIsBruteForceProtectionEnabled($enableBruteForceProtection) {
127
		$value = $enableBruteForceProtection === true ? '1' : '0';
128
		$this->config->setAppValue('security', 'enable_brute_force_protection', $value);
129
	}
130
	/**
131
	 * set minimal length of passwords
132
	 *
133
	 * @param int $minLength
134
	 */
135
	public function setMinPasswordLength($minLength) {
136
		$this->config->setAppValue('security', 'min_password_length', $minLength);
137
	}
138
	/**
139
	 * enforce upper and lower characters case
140
	 *
141
	 * @param bool $enforceUpperLowerCase
142
	 */
143
	public function setIsUpperLowerCaseEnforced($enforceUpperLowerCase) {
144
		$value = $enforceUpperLowerCase === true ? '1' : '0';
145
		$this->config->setAppValue('security', 'enforce_upper_lower_case', $value);
146
	}
147
	/**
148
	 * enforce numeric characters
149
	 *
150
	 * @param bool $enforceNumericCharacters
151
	 */
152
	public function setIsNumericCharactersEnforced($enforceNumericCharacters) {
153
		$value = $enforceNumericCharacters === true ? '1' : '0';
154
		$this->config->setAppValue('security', 'enforce_numeric_characters', $value);
155
	}
156
	/**
157
	 * enforce special characters
158
	 *
159
	 * @param bool $enforceSpecialCharacters
160
	 */
161
	public function setIsSpecialCharactersEnforced($enforceSpecialCharacters) {
162
		$value = $enforceSpecialCharacters === true ? '1' : '0';
163
		$this->config->setAppValue('security', 'enforce_special_characters', $value);
164
	}
165
}