Debian Codegraph @

lib/modules/nisNetGroupHost.inc

raw
1 <?php
2
3 use LAM\PDF\PDFTable;
4 use LAM\PDF\PDFTableCell;
5 use LAM\PDF\PDFTableRow;
6
7 /*
8
9 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
10 Copyright (C) 2016 - 2026 Roland Gruber
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
26 */
27
28 /**
29 * Manages memberships in NIS net groups.
30 *
31 * @package modules
32 * @author Roland Gruber
33 */
34
35 /** include parent class */
36 include_once(__DIR__ . "/nisNetGroupUser.inc");
37
38 /**
39 * Manages memberships in NIS net groups.
40 *
41 * @package modules
42 */
43 class nisNetGroupHost extends nisNetGroupUser {
44
45 /**
46 * {@inheritDoc}
47 */
48 public function can_manage(): bool {
49 return $this->get_scope() === 'host';
50 }
51
52 /**
53 * {@inheritDoc}
54 */
55 public function get_metaData(): array {
56 $return = parent::get_metaData();
57 // module dependencies
58 $return['dependencies'] = ['depends' => [['account', 'posixAccount']], 'conflicts' => []];
59 // upload columns
60 $return['upload_columns'] = [[
61 'name' => 'nisNetGroup_memberships',
62 'description' => _('Memberships'),
63 'help' => 'memberships_upload',
64 'example' => 'group1#user#domain,group2#user#domain'
65 ]];
66 return $return;
67 }
68
69 /**
70 * {@inheritDoc}
71 */
72 protected function load_Messages(): void {
73 parent::load_Messages();
74 $this->messages['user'][0] = ['ERROR', _('User name'), _('User name contains invalid characters. Valid characters are: a-z, A-Z, 0-9 and .-_ !')];
75 }
76
77 /**
78 * {@inheritDoc}
79 */
80 protected function isMatchingNetGroup($user, $host, $domain, $uid): bool {
81 return $host == $uid;
82 }
83
84 /**
85 * {@inheritDoc}
86 */
87 public function display_html_attributes(): htmlElement|array {
88 $return = new htmlTable();
89 $return->addElement(new htmlOutputText(_('Group')));
90 $return->addElement(new htmlOutputText(_('User name')));
91 $return->addElement(new htmlOutputText(_('Domain name')), true);
92 for ($i = 0; $i < count($this->groups); $i++) {
93 $group = $this->groups[$i];
94 $return->addElement(new htmlOutputText($group['name']));
95 $return->addElement(new htmlInputField('user_' . $i, $group['user']));
96 $return->addElement(new htmlInputField('domain_' . $i, $group['domain']));
97 $delButton = new htmlButton('del_' . $i, 'del.svg', true);
98 $delButton->setTitle(_('Delete'));
99 $return->addElement($delButton, true);
100 }
101 $return->addVerticalSpace('40px');
102
103 // new entry
104 $groupList = [];
105 $groupData = $this->findGroups();
106 if (count($groupData) > 0) {
107 $filterGroup = new htmlGroup();
108 $filterGroup->addElement(new htmlOutputText(_('Filter') . ' '));
109 $filter = new htmlInputField('group_filter');
110 $filter->setFieldSize(5);
111 $filter->filterSelectBox('group_add');
112 $filterGroup->addElement($filter);
113 $return->addElement($filterGroup, true);
114
115 foreach ($groupData as $group) {
116 $groupList[$group['cn'][0]] = $group['cn'][0] . '#+#' . $group['dn'];
117 }
118 $groupSelect = new htmlSelect('group_add', $groupList);
119 $groupSelect->setHasDescriptiveElements(true);
120 $return->addElement($groupSelect);
121 $return->addElement(new htmlInputField('user_add'));
122 $return->addElement(new htmlInputField('domain_add'));
123 $addButton = new htmlButton('addGroup', 'add.svg', true);
124 $addButton->setTitle(_('Add'));
125 $return->addElement($addButton, true);
126 }
127 return $return;
128 }
129
130 /**
131 * {@inheritDoc}
132 */
133 public function process_attributes(): array {
134 $errors = [];
135 // add new entry
136 if (isset($_POST['addGroup'])) {
137 $parts = explode('#+#', $_POST['group_add']);
138 $this->groups[] = [
139 'name' => $parts[0],
140 'dn' => $parts[1],
141 'user' => $_POST['user_add'],
142 'host' => $this->uidOrig,
143 'domain' => $_POST['domain_add']
144 ];
145 if (!empty($_POST['user_add']) && !get_preg($_POST['user_add'], 'username')) {
146 $message = $this->messages['user'][0];
147 $message[2] = $message[2] . '<br><br>' . $_POST['user_add'];
148 $errors[] = $message;
149 }
150 if (!empty($_POST['domain_add']) && !get_preg($_POST['domain_add'], 'DNSname')) {
151 $message = $this->messages['domain'][0];
152 $message[2] = $message[2] . '<br><br>' . $_POST['domain_add'];
153 $errors[] = $message;
154 }
155 }
156 // check existing
157 $counter = 0;
158 while (isset($_POST['user_' . $counter])) {
159 if (isset($_POST['del_' . $counter])) {
160 unset($this->groups[$counter]);
161 }
162 else {
163 $this->groups[$counter]['user'] = $_POST['user_' . $counter];
164 if (!empty($_POST['user_' . $counter]) && !get_preg($_POST['user_' . $counter], 'username')) {
165 $message = $this->messages['user'][0];
166 $message[2] = $message[2] . '<br><br>' . $_POST['user_' . $counter];
167 $errors[] = $message;
168 }
169 $this->groups[$counter]['domain'] = $_POST['domain_' . $counter];
170 if (!empty($_POST['domain_' . $counter]) && !get_preg($_POST['domain_' . $counter], 'DNSname')) {
171 $message = $this->messages['domain'][0];
172 $message[2] = $message[2] . '<br><br>' . $_POST['domain_' . $counter];
173 $errors[] = $message;
174 }
175 }
176 $counter++;
177 }
178 $this->groups = array_values($this->groups);
179 usort($this->groups, [$this, 'sortTriple']);
180 return $errors;
181 }
182
183 /**
184 * {@inheritDoc}
185 */
186 protected function createNetGroupValue(array $group, string $uid): string {
187 return '(' . $uid . ',' . $group['user'] . ',' . $group['domain'] . ')';
188 }
189
190 /**
191 * {@inheritDoc}
192 */
193 public function get_profileOptions(string $typeId): ?htmlResponsiveRow {
194 $groups = $this->findGroups();
195 $groupOptions = ['' => ''];
196 foreach ($groups as $group) {
197 $groupOptions[$group['cn'][0]] = $group['cn'][0] . '#+#' . $group['dn'];
198 }
199 $return = new htmlResponsiveRow();
200 $labels = [_('Group'), _('User name'), _('Domain name')];
201 $data = [];
202 for ($i = 0; $i < 5; $i++) {
203 $select = new htmlSelect('nisNetGroupUser_group' . $i, $groupOptions, ['']);
204 $select->setHasDescriptiveElements(true);
205 $data[$i][] = $select;
206 $data[$i][] = new htmlInputField('nisNetGroupUser_user' . $i);
207 $data[$i][] = new htmlInputField('nisNetGroupUser_domain' . $i);
208 }
209 $return->add(new htmlResponsiveTable($labels, $data));
210 return $return;
211 }
212
213 /**
214 * {@inheritDoc}
215 */
216 public function load_profile(array $profile): void {
217 for ($i = 0; $i < 5; $i++) {
218 if (!empty($profile['nisNetGroupUser_group' . $i][0])) {
219 $parts = explode('#+#', $profile['nisNetGroupUser_group' . $i][0]);
220 $this->groups[] = [
221 'name' => $parts[0],
222 'dn' => $parts[1],
223 'user' => $profile['nisNetGroupUser_user' . $i][0],
224 'host' => $this->uidOrig,
225 'domain' => $profile['nisNetGroupUser_domain' . $i][0],
226 ];
227 }
228 }
229 usort($this->groups, [$this, 'sortTriple']);
230 }
231
232 /**
233 * {@inheritDoc}
234 */
235 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
236 $return = [];
237 $pdfTable = new PDFTable();
238 $pdfRow = new PDFTableRow();
239 $pdfRow->cells[] = new PDFTableCell(_('Group'), '25%', null, true);
240 $pdfRow->cells[] = new PDFTableCell(_('User name'), '25%', null, true);
241 $pdfRow->cells[] = new PDFTableCell(_('Domain name'), '25%', null, true);
242 $pdfTable->rows[] = $pdfRow;
243 foreach ($this->groups as $group) {
244 $pdfRow = new PDFTableRow();
245 $pdfRow->cells[] = new PDFTableCell($group['name'], '25%');
246 $pdfRow->cells[] = new PDFTableCell($group['user'], '25%');
247 $pdfRow->cells[] = new PDFTableCell($group['domain'], '25%');
248 $pdfTable->rows[] = $pdfRow;
249 }
250 $this->addPDFTable($return, 'memberships', $pdfTable);
251 return $return;
252 }
253
254 /**
255 * {@inheritDoc}
256 */
257 protected function buildNetGroupTripleFromUploadValue($value, $uid): string {
258 $user = empty($value[1]) ? '' : $value[1];
259 $domain = empty($value[2]) ? '' : $value[2];
260 return '(' . $uid . ',' . $user . ',' . $domain . ')';
261 }
262
263 }