Debian Codegraph @

lib/modules/systemQuotas.inc

raw
1 <?php
2
3 use LAM\PDF\PDFTable;
4 use LAM\PDF\PDFTableCell;
5 use LAM\PDF\PDFTableRow;
6 use LAM\TYPES\ConfiguredType;
7
8 /*
9
10 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
11 Copyright (C) 2011 - 2026 Roland Gruber
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28 /**
29 * Manages user quotas with the object class systemQuotas.
30 *
31 * @package modules
32 * @author Roland Gruber
33 */
34
35 /**
36 * Manages user quotas with the object class systemQuotas.
37 *
38 * @package modules
39 */
40 class systemQuotas extends baseModule {
41
42 /**
43 * {@inheritDoc}
44 */
45 public function can_manage(): bool {
46 return $this->get_scope() === 'user';
47 }
48
49 /**
50 * {@inheritDoc}
51 */
52 public function get_metaData(): array {
53 $return = [];
54 // icon
55 $return['icon'] = 'hard-drive.svg';
56 // alias name
57 $return["alias"] = _("Quota");
58 // module dependencies
59 $return['dependencies'] = ['depends' => ['posixAccount'], 'conflicts' => []];
60 // managed object classes
61 $return['objectClasses'] = ['systemQuotas'];
62 // managed attributes
63 $return['attributes'] = ['quota'];
64 // help Entries
65 $return['help'] = [
66 'quota' => [
67 "Headline" => _("Quota"), 'attr' => 'quota',
68 "Text" => _("Please enter the quota settings for this user. The syntax is: {mount point},{soft block limit},{hard block limit},{soft inode limit},{hard inode limit}.")
69 . ' ' . _('Multiple values are separated by semicolon.')
70 ]
71 ];
72 // profile elements
73 $profileContainer = new htmlResponsiveRow();
74 $profileContainer->add(new htmlResponsiveInputField(_('Quota'), 'systemQuotas_quota', null, 'quota'));
75 $return['profile_options'] = $profileContainer;
76 // upload fields
77 $return['upload_columns'] = [
78 [
79 'name' => 'systemQuotas_quota',
80 'description' => _('Quota'),
81 'help' => 'quota',
82 'example' => '/home/smiller,50000,60000,10000,12000',
83 ]
84 ];
85 // available PDF fields
86 $return['PDF_fields'] = [
87 'quota' => _('Quota')
88 ];
89 return $return;
90 }
91
92 /**
93 * {@inheritDoc}
94 */
95 protected function load_Messages(): void {
96 $this->messages['path'][0] = ['ERROR', _('Mountpoint'), _('Mountpoint contains invalid characters.')];
97 $this->messages['path'][1] = ['ERROR', _('Account %s:'), _('Mountpoint contains invalid characters.')];
98 $this->messages['softblock'][0] = ['ERROR', _('Block soft quota'), _('Block soft quota contains invalid characters. Only natural numbers are allowed.')];
99 $this->messages['softblock'][1] = ['ERROR', _('Account %s:'), _('Block soft quota contains invalid characters. Only natural numbers are allowed.')];
100 $this->messages['hardblock'][0] = ['ERROR', _('Block hard quota'), _('Block hard quota contains invalid characters. Only natural numbers are allowed.')];
101 $this->messages['hardblock'][1] = ['ERROR', _('Account %s:'), _('Block hard quota contains invalid characters. Only natural numbers are allowed.')];
102 $this->messages['softinode'][0] = ['ERROR', _('Inode soft quota'), _('Inode soft quota contains invalid characters. Only natural numbers are allowed.')];
103 $this->messages['softinode'][1] = ['ERROR', _('Account %s:'), _('Inode soft quota contains invalid characters. Only natural numbers are allowed.')];
104 $this->messages['hardinode'][0] = ['ERROR', _('Inode hard quota'), _('Inode hard quota contains invalid characters. Only natural numbers are allowed.')];
105 $this->messages['hardinode'][1] = ['ERROR', _('Account %s:'), _('Inode hard quota contains invalid characters. Only natural numbers are allowed.')];
106 $this->messages['block_cmp'][0] = ['ERROR', _('Block quota'), _('Block soft quota must be smaller than block hard quota.')];
107 $this->messages['block_cmp'][1] = ['ERROR', _('Account %s:'), _('Block soft quota must be smaller than block hard quota.')];
108 $this->messages['inode_cmp'][0] = ['ERROR', _('Inode quota'), _('Inode soft quota must be smaller than inode hard quota.')];
109 $this->messages['inode_cmp'][1] = ['ERROR', _('Account %s:'), _('Inode soft quota must be smaller than inode hard quota.')];
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 public function display_html_attributes(): htmlElement|array {
116 $container = new htmlTable();
117 $spacer = new htmlSpacer('10px', null);
118 // caption
119 $container->addElement(new htmlOutputText(_('Mountpoint')));
120 $container->addElement($spacer);
121 $container->addElement(new htmlOutputText(_('Soft block limit')));
122 $container->addElement($spacer);
123 $container->addElement(new htmlOutputText(_('Hard block limit')));
124 $container->addElement($spacer);
125 $container->addElement(new htmlOutputText(_('Soft inode limit')));
126 $container->addElement($spacer);
127 $container->addElement(new htmlOutputText(_('Hard inode limit')), true);
128 // existing entries
129 if (isset($this->attributes['quota'][0])) {
130 natcasesort($this->attributes['quota']);
131 $this->attributes['quota'] = array_values($this->attributes['quota']);
132 for ($i = 0; $i < count($this->attributes['quota']); $i++) {
133 $parts = explode(',', $this->attributes['quota'][$i]);
134 $container->addElement(new htmlInputField('path_' . $i, $parts[0], 20));
135 $container->addElement($spacer);
136 $softBlockInput = new htmlInputField('softBlock_' . $i, $parts[1], 10);
137 $container->addElement($softBlockInput);
138 $container->addElement($spacer);
139 $hardBlockInput = new htmlInputField('hardBlock_' . $i, $parts[2], 10);
140 $container->addElement($hardBlockInput);
141 $container->addElement($spacer);
142 $softInodeInput = new htmlInputField('softInode_' . $i, $parts[3], 10);
143 $container->addElement($softInodeInput);
144 $container->addElement($spacer);
145 $hardInodeInput = new htmlInputField('hardInode_' . $i, $parts[4], 10);
146 $container->addElement($hardInodeInput);
147 $container->addElement(new htmlButton('del_' . $i, 'del.svg', true), true);
148 }
149 }
150 // new entry
151 $container->addElement(new htmlInputField('path', null, 20));
152 $container->addElement($spacer);
153 $newSoftBlockInput = new htmlInputField('softBlock', '0', 10);
154 $container->addElement($newSoftBlockInput);
155 $container->addElement($spacer);
156 $newHardBlockInput = new htmlInputField('hardBlock', '0', 10);
157 $container->addElement($newHardBlockInput);
158 $container->addElement($spacer);
159 $newSoftInodeInput = new htmlInputField('softInode', '0', 10);
160 $container->addElement($newSoftInodeInput);
161 $container->addElement($spacer);
162 $newHardInodeInput = new htmlInputField('hardInode', '0', 10);
163 $container->addElement($newHardInodeInput);
164 $container->addElement(new htmlButton('add', 'add.svg', true));
165 return $container;
166 }
167
168 /**
169 * {@inheritDoc}
170 */
171 public function process_attributes(): array {
172 $return = [];
173 if (!isset($this->attributes['quota'][0])) {
174 $this->attributes['quota'] = [];
175 }
176 // check existing entries
177 for ($i = 0; $i < count($this->attributes['quota']); $i++) {
178 if (isset($_POST['del_' . $i])) {
179 unset($this->attributes['quota'][$i]);
180 $this->attributes['quota'] = array_values($this->attributes['quota']);
181 $i--;
182 continue;
183 }
184 $path = $_POST['path_' . $i];
185 $softBlock = $_POST['softBlock_' . $i];
186 if ($softBlock == '') {
187 $softBlock = '0';
188 }
189 $hardBlock = $_POST['hardBlock_' . $i];
190 if ($hardBlock == '') {
191 $hardBlock = '0';
192 }
193 $softInode = $_POST['softInode_' . $i];
194 if ($softInode == '') {
195 $softInode = '0';
196 }
197 $hardInode = $_POST['hardInode_' . $i];
198 if ($hardInode == '') {
199 $hardInode = '0';
200 }
201 $this->attributes['quota'][$i] = $path . ',' . $softBlock . ',' . $hardBlock . ',' .
202 $softInode . ',' . $hardInode;
203 $return = array_merge($return, $this->checkQuota($path, $softBlock, $hardBlock, $softInode, $hardInode));
204 }
205 // check for new entry
206 if (isset($_POST['add'])) {
207 $path = $_POST['path'];
208 $softBlock = $_POST['softBlock'];
209 if ($softBlock == '') {
210 $softBlock = '0';
211 }
212 $hardBlock = $_POST['hardBlock'];
213 if ($hardBlock == '') {
214 $hardBlock = '0';
215 }
216 $softInode = $_POST['softInode'];
217 if ($softInode == '') {
218 $softInode = '0';
219 }
220 $hardInode = $_POST['hardInode'];
221 if ($hardInode == '') {
222 $hardInode = '0';
223 }
224 $this->attributes['quota'][] = $path . ',' . $softBlock . ',' . $hardBlock . ',' .
225 $softInode . ',' . $hardInode;
226 $return = array_merge($return, $this->checkQuota($path, $softBlock, $hardBlock, $softInode, $hardInode));
227 }
228 $this->attributes['quota'] = array_unique($this->attributes['quota']);
229 return $return;
230 }
231
232 /**
233 * Checks if the quota parameters are valid.
234 *
235 * @param string $path mountpoint
236 * @param string $softBlock soft block limit
237 * @param string $hardBlock hard block limit
238 * @param string $softInode soft inode limit
239 * @param string $hardInode hard inode limit
240 * @param int $uploadIndex position in upload table
241 * @return string[][] array where error messages are returned
242 */
243 private function checkQuota($path, $softBlock, $hardBlock, $softInode, $hardInode, $uploadIndex = null): array {
244 $return = [];
245 if (!get_preg($path, 'filePath')) {
246 if ($uploadIndex == null) {
247 $return[] = $this->messages['path'][0];
248 }
249 else {
250 $error = $this->messages['path'][1];
251 $error[] = [$uploadIndex];
252 $return[] = $error;
253 }
254 }
255 if (!get_preg($softBlock, 'quotaNumber')) {
256 if ($uploadIndex == null) {
257 $return[] = $this->messages['softblock'][0];
258 }
259 else {
260 $error = $this->messages['softblock'][1];
261 $error[] = [$uploadIndex];
262 $return[] = $error;
263 }
264 }
265 if (!get_preg($hardBlock, 'quotaNumber')) {
266 if ($uploadIndex == null) {
267 $return[] = $this->messages['hardblock'][0];
268 }
269 else {
270 $error = $this->messages['hardblock'][1];
271 $error[] = [$uploadIndex];
272 $return[] = $error;
273 }
274 }
275 if (!get_preg($softInode, 'quotaNumber')) {
276 if ($uploadIndex == null) {
277 $return[] = $this->messages['softinode'][0];
278 }
279 else {
280 $error = $this->messages['softinode'][1];
281 $error[] = [$uploadIndex];
282 $return[] = $error;
283 }
284 }
285 if (!get_preg($hardInode, 'quotaNumber')) {
286 if ($uploadIndex == null) {
287 $return[] = $this->messages['hardinode'][0];
288 }
289 else {
290 $error = $this->messages['hardinode'][1];
291 $error[] = [$uploadIndex];
292 $return[] = $error;
293 }
294 }
295 if ($softBlock > $hardBlock) {
296 if ($uploadIndex == null) {
297 $return[] = $this->messages['block_cmp'][0];
298 }
299 else {
300 $error = $this->messages['block_cmp'][1];
301 $error[] = [$uploadIndex];
302 $return[] = $error;
303 }
304 }
305 if ($softInode > $hardInode) {
306 if ($uploadIndex == null) {
307 $return[] = $this->messages['inode_cmp'][0];
308 }
309 else {
310 $error = $this->messages['inode_cmp'][1];
311 $error[] = [$uploadIndex];
312 $return[] = $error;
313 }
314 }
315 return $return;
316 }
317
318 /**
319 * {@inheritDoc}
320 */
321 public function check_profileOptions(array $options, string $typeId): array {
322 $messages = parent::check_profileOptions($options, $typeId);
323 $quotas = explode(';', $options['systemQuotas_quota'][0]);
324 for ($q = 0; $q < count($quotas); $q++) {
325 if ($quotas[$q] === '') {
326 continue;
327 }
328 $parts = explode(',', $quotas[$q]);
329 $messages = array_merge($messages, $this->checkQuota($parts[0], $parts[1], $parts[2], $parts[3], $parts[4]));
330 }
331 return $messages;
332 }
333
334 /**
335 * {@inheritDoc}
336 */
337 public function load_profile(array $profile): void {
338 // profile mappings in meta data
339 parent::load_profile($profile);
340 if (isset($profile['systemQuotas_quota'][0]) && ($profile['systemQuotas_quota'][0] != '')) {
341 $this->attributes['quota'] = explode(';', $profile['systemQuotas_quota'][0]);
342 }
343 }
344
345 /**
346 * {@inheritDoc}
347 */
348 public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
349 $messages = [];
350 for ($i = 0; $i < count($rawAccounts); $i++) {
351 // add object class
352 if (!in_array('systemQuotas', $partialAccounts[$i]['objectClass'])) {
353 $partialAccounts[$i]['objectClass'][] = 'systemQuotas';
354 }
355 // add quota
356 if (isset($rawAccounts[$i][$ids['systemQuotas_quota']]) && ($rawAccounts[$i][$ids['systemQuotas_quota']] != '')) {
357 $quotas = explode(';', $rawAccounts[$i][$ids['systemQuotas_quota']]);
358 for ($q = 0; $q < count($quotas); $q++) {
359 $parts = explode(',', $quotas[$q]);
360 $messages = array_merge($messages, $this->checkQuota($parts[0], $parts[1], $parts[2], $parts[3], $parts[4], $i));
361 $partialAccounts[$i]['quota'][] = $quotas[$q];
362 }
363 }
364 }
365 return $messages;
366 }
367
368 /**
369 * {@inheritDoc}
370 */
371 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
372 $return = [];
373 if (isset($this->attributes['quota'][0])) {
374 $pdfTable = new PDFTable();
375 $pdfRow = new PDFTableRow();
376 $pdfRow->cells[] = new PDFTableCell(_('Mountpoint'), '28%', null, true);
377 $pdfRow->cells[] = new PDFTableCell(_('Soft block'), '18%', null, true);
378 $pdfRow->cells[] = new PDFTableCell(_('Hard block'), '18%', null, true);
379 $pdfRow->cells[] = new PDFTableCell(_('Soft inode'), '18%', null, true);
380 $pdfRow->cells[] = new PDFTableCell(_('Hard inode'), '18%', null, true);
381 $pdfTable->rows[] = $pdfRow;
382 for ($i = 0; $i < count($this->attributes['quota']); $i++) {
383 $parts = explode(',', $this->attributes['quota'][$i]);
384 $pdfRow = new PDFTableRow();
385 $pdfRow->cells[] = new PDFTableCell($parts[0], '28%');
386 $pdfRow->cells[] = new PDFTableCell($parts[1], '18%');
387 $pdfRow->cells[] = new PDFTableCell($parts[2], '18%');
388 $pdfRow->cells[] = new PDFTableCell($parts[3], '18%');
389 $pdfRow->cells[] = new PDFTableCell($parts[4], '18%');
390 $pdfTable->rows[] = $pdfRow;
391 }
392 $this->addPDFTable($return, 'quota', $pdfTable);
393 }
394 return $return;
395 }
396
397 }