Debian Codegraph @

lib/modules/ieee802device.inc

raw
1 <?php
2 /*
3
4 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
5 Copyright (C) 2004 - 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 * Provides MAC addresses for hosts.
24 *
25 * @package modules
26 * @author Roland Gruber
27 */
28
29 use LAM\TYPES\ConfiguredType;
30
31 /**
32 * Provides MAC addresses for hosts.
33 *
34 * @package modules
35 */
36 class ieee802device extends baseModule {
37
38 /**
39 * These attributes will be ignored by default if a new account is copied from an existing one.
40 */
41 private const ATTRIBUTES_TO_IGNORE_ON_COPY = ['macAddress'];
42
43 /**
44 * {@inheritDoc}
45 */
46 public function can_manage(): bool {
47 return $this->get_scope() === 'host';
48 }
49
50 /**
51 * {@inheritDoc}
52 */
53 public function get_metaData(): array {
54 $return = [];
55 // icon
56 $return['icon'] = 'network.svg';
57 // alias name
58 $return["alias"] = _("MAC address");
59 // module dependencies
60 $return['dependencies'] = ['depends' => [], 'conflicts' => []];
61 // managed object classes
62 $return['objectClasses'] = ['ieee802Device'];
63 // managed attributes
64 $return['attributes'] = ['macAddress'];
65 // help Entries
66 $return['help'] = [
67 'macAddress' => [
68 "Headline" => _("MAC address"), 'attr' => 'macAddress',
69 "Text" => _("This is the MAC address of the network card of the device (e.g. 00:01:02:DE:EF:18).")
70 ],
71 'macList' => [
72 "Headline" => _("MAC address list"), 'attr' => 'macAddress',
73 "Text" => _("This is a comma separated list of MAC addresses.")
74 ]];
75 // upload fields
76 $return['upload_columns'] = [
77 [
78 'name' => 'ieee802Device_mac',
79 'description' => _('MAC address'),
80 'help' => 'macList',
81 'example' => '00:01:02:DE:EF:18'
82 ]
83 ];
84 // available PDF fields
85 $return['PDF_fields'] = [
86 'macAddress' => _('MAC addresses')
87 ];
88 return $return;
89 }
90
91 /**
92 * {@inheritDoc}
93 */
94 protected function load_Messages(): void {
95 $this->messages['macAddress'][0] = ['ERROR', 'MAC address is invalid!']; // third array value is set dynamically
96 $this->messages['macAddress'][1] = ['ERROR', _('Account %s:') . ' ieee802Device_mac', 'MAC address is invalid!'];
97 }
98
99 /**
100 * {@inheritDoc}
101 */
102 public function loadAttributesFromAccountCopy(array $ldapAttributes, array $attributesToIgnore = []): void {
103 parent::loadAttributesFromAccountCopy($ldapAttributes, array_merge(baseModule::ATTRIBUTES_TO_IGNORE_ON_COPY_DEFAULT, self::ATTRIBUTES_TO_IGNORE_ON_COPY));
104 }
105
106 /**
107 * {@inheritDoc}
108 */
109 public function display_html_attributes(): htmlElement|array {
110 $return = new htmlResponsiveRow();
111 $this->addMultiValueInputTextField($return, 'macAddress', _('MAC address'), false, 17);
112 return $return;
113 }
114
115 /**
116 * {@inheritDoc}
117 */
118 public function process_attributes(): array {
119 $errors = [];
120 $this->processMultiValueInputTextField('macAddress', $errors, 'macAddress');
121 // check if MAC is already assigned
122 if (!empty($this->attributes['macAddress'])) {
123 foreach ($this->attributes['macAddress'] as $macAddress) {
124 if (!empty($this->orig['macAddress']) && in_array($macAddress, $this->orig['macAddress'])) {
125 // check only new MAC addresses
126 continue;
127 }
128 $result = searchLDAPByAttribute('macAddress', $macAddress, 'ieee802Device', ['dn'], ['host']);
129 if (!empty($result)) {
130 $errors[] = ['WARN', _('This MAC address is already in use.'), getAbstractDN($result[0]['dn'])];
131 }
132 }
133 }
134 return $errors;
135 }
136
137 /**
138 * {@inheritDoc}
139 */
140 public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
141 $messages = [];
142 for ($i = 0; $i < count($rawAccounts); $i++) {
143 // add object class
144 if (!in_array("ieee802Device", $partialAccounts[$i]['objectClass'])) {
145 $partialAccounts[$i]['objectClass'][] = "ieee802Device";
146 }
147 // add MACs
148 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'ieee802Device_mac', 'macAddress', 'macAddress', $this->messages['macAddress'][1], $messages, '/,[ ]*/');
149 }
150 return $messages;
151 }
152
153 /**
154 * {@inheritDoc}
155 */
156 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
157 $return = [];
158 $this->addSimplePDFField($return, 'macAddress', _('MAC addresses'));
159 return $return;
160 }
161
162 /**
163 * @inheritDoc
164 */
165 public function getListAttributeDescriptions(ConfiguredType $type): array {
166 return [
167 'macaddress' => _('MAC addresses')
168 ];
169 }
170
171 }