Debian Codegraph @

lib/modules/hostObject.inc

raw
1 <?php
2 /*
3
4 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
5 Copyright (C) 2011 - 2024 Roland Gruber
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 /**
23 * Manages the hosts to which a user may login.
24 *
25 * @package modules
26 * @author Roland Gruber
27 */
28
29 use LAM\TYPES\ConfiguredType;
30
31 /**
32 * Manages the hosts to which a user may login.
33 *
34 * @package modules
35 */
36 class hostObject extends baseModule {
37
38 /**
39 * Creates a new hostObject object.
40 *
41 * @param string $scope account type (user, group, host)
42 */
43 public function __construct($scope) {
44 // call parent constructor
45 parent::__construct($scope);
46 $this->autoAddObjectClasses = false;
47 }
48
49 /**
50 * {@inheritDoc}
51 */
52 public function can_manage(): bool {
53 return in_array($this->get_scope(), ['user', 'group']);
54 }
55
56 /**
57 * {@inheritDoc}
58 */
59 public function get_metaData(): array {
60 $return = [];
61 // icon
62 $return['icon'] = 'computer.svg';
63 // alias name
64 $return["alias"] = _("Hosts");
65 // module dependencies
66 $return['dependencies'] = ['depends' => [], 'conflicts' => []];
67 // managed object classes
68 $return['objectClasses'] = ['hostObject'];
69 // managed attributes
70 $return['attributes'] = ['host'];
71 // help Entries
72 $return['help'] = [
73 'host' => [
74 "Headline" => _("Hosts"), 'attr' => 'host',
75 "Text" => _("Here you can specify the list of host names where this account has login privileges. The wildcard \"*\" represents all hosts. You may also use \"!\" in front of a host name to deny access to a host.")
76 ],
77 'hostList' => [
78 "Headline" => _("Hosts"), 'attr' => 'host',
79 "Text" => _("Here you can specify the list of host names where this account has login privileges. The wildcard \"*\" represents all hosts. You may also use \"!\" in front of a host name to deny access to a host.")
80 . ' ' . _("Multiple values are separated by comma.")
81 ],
82 'autoAdd' => [
83 "Headline" => _("Automatically add this extension"),
84 "Text" => _("This will enable the extension automatically if this profile is loaded.")
85 ]
86 ];
87 // upload fields
88 $return['upload_columns'] = [
89 [
90 'name' => 'hostObject_hosts',
91 'description' => _('Host list'),
92 'help' => 'hostList',
93 'example' => _('pc01,pc02')
94 ]
95 ];
96 // available PDF fields
97 $return['PDF_fields'] = [
98 'hosts' => _('Host list')
99 ];
100 $profileContainer = new htmlResponsiveRow();
101 $profileContainer->add(new htmlResponsiveInputCheckbox('hostObject_addExt', false, _('Automatically add this extension'), 'autoAdd'));
102 $profileContainer->add(new htmlResponsiveInputField(_('Host'), 'hostObject_host', null, 'hostList'));
103 $return['profile_options'] = $profileContainer;
104 return $return;
105 }
106
107 /**
108 * {@inheritDoc}
109 */
110 protected function load_Messages(): void {
111 $this->messages['host'][0] = ['ERROR', 'Please enter a valid host name.']; // third array value is set dynamically
112 $this->messages['host'][1] = ['ERROR', _('Account %s:') . ' hostObject_hosts', _('Please enter a valid list of host names.')];
113 }
114
115 /**
116 * {@inheritDoc}
117 */
118 public function save_attributes(): array {
119 if (!in_array('hostObject', $this->attributes['objectClass']) && !in_array('hostObject', $this->orig['objectClass'])) {
120 // skip saving if the extension was not added/modified
121 return [];
122 }
123 return parent::save_attributes();
124 }
125
126 /**
127 * {@inheritDoc}
128 */
129 public function display_html_attributes(): htmlElement|array {
130 if (isset($_POST['addObjectClass'])) {
131 $this->attributes['objectClass'][] = 'hostObject';
132 }
133 $return = new htmlResponsiveRow();
134 if (in_array('hostObject', $this->attributes['objectClass'])) {
135 $this->addMultiValueInputTextField($return, 'host', _('Host'));
136 $return->addVerticalSpacer('2rem');
137 $remButton = new htmlButton('remObjectClass', _('Remove host extension'));
138 $remButton->setCSSClasses(['lam-danger']);
139 $return->add($remButton, 12, 12, 12, 'text-center');
140 }
141 else {
142 $return->add(new htmlButton('addObjectClass', _('Add host extension')));
143 }
144 return $return;
145 }
146
147 /**
148 * {@inheritDoc}
149 */
150 public function process_attributes(): array {
151 if (isset($_POST['remObjectClass'])) {
152 $this->attributes['objectClass'] = array_delete(['hostObject'], $this->attributes['objectClass']);
153 if (isset($this->attributes['host'])) {
154 unset($this->attributes['host']);
155 }
156 return [];
157 }
158 if (!in_array('hostObject', $this->attributes['objectClass'])) {
159 return [];
160 }
161 $errors = [];
162 $this->processMultiValueInputTextField('host', $errors, 'hostObject');
163 return $errors;
164 }
165
166
167 /**
168 * {@inheritDoc}
169 */
170 public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
171 $messages = [];
172 for ($i = 0; $i < count($rawAccounts); $i++) {
173 // add object class
174 if (!in_array("hostObject", $partialAccounts[$i]['objectClass'])) {
175 $partialAccounts[$i]['objectClass'][] = "hostObject";
176 }
177 // add hosts
178 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'hostObject_hosts', 'host', 'hostObject', $this->messages['host'][1], $messages, '/,[ ]*/');
179 }
180 return $messages;
181 }
182
183 /**
184 * {@inheritDoc}
185 */
186 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
187 $return = [];
188 $this->addSimplePDFField($return, 'hosts', _('Host list'), 'host');
189 return $return;
190 }
191
192 /**
193 * {@inheritDoc}
194 */
195 public function load_profile(array $profile): void {
196 // profile mappings in meta data
197 parent::load_profile($profile);
198 // add extension
199 if (isset($profile['hostObject_addExt'][0])
200 && ($profile['hostObject_addExt'][0] == "true")
201 && !in_array('hostObject', $this->attributes['objectClass'])) {
202 $this->attributes['objectClass'][] = 'hostObject';
203 }
204 $hostList = preg_split('/,[ ]*/', $profile['hostObject_host'][0]);
205 $this->attributes['host'] = $hostList;
206 }
207
208 /**
209 * @inheritDoc
210 */
211 public function getListAttributeDescriptions(ConfiguredType $type): array {
212 return [
213 'host' => _('Host list')
214 ];
215 }
216
217 }