| 1 |
<?php |
| 2 |
/* |
| 3 |
|
| 4 |
This code is not yet part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 5 |
Copyright (C) 2011 J de Jong |
| 6 |
2012 - 2026 Roland Gruber |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 2 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program; if not, write to the Free Software |
| 20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 |
|
| 22 |
*/ |
| 23 |
|
| 24 |
|
| 25 |
/** |
| 26 |
* Provides Authorized Service for accounts. |
| 27 |
* |
| 28 |
* @package modules |
| 29 |
* @author J de Jong |
| 30 |
* @author Roland Gruber |
| 31 |
*/ |
| 32 |
|
| 33 |
use LAM\TYPES\ConfiguredType; |
| 34 |
|
| 35 |
/** |
| 36 |
* Provides Authorized Service for accounts. |
| 37 |
* |
| 38 |
* @package modules |
| 39 |
*/ |
| 40 |
class authorizedServiceObject extends baseModule { |
| 41 |
|
| 42 |
/** |
| 43 |
* Creates a new authorizedServiceObject object. |
| 44 |
* |
| 45 |
* @param string $scope account type (user, group, host) |
| 46 |
*/ |
| 47 |
public function __construct($scope) { |
| 48 |
// call parent constructor |
| 49 |
parent::__construct($scope); |
| 50 |
$this->autoAddObjectClasses = false; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* {@inheritDoc} |
| 56 |
*/ |
| 57 |
public function can_manage(): bool { |
| 58 |
return $this->get_scope() === 'user'; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* {@inheritDoc} |
| 63 |
*/ |
| 64 |
public function get_metaData(): array { |
| 65 |
$return = []; |
| 66 |
// icon |
| 67 |
$return['icon'] = 'gears-color.svg'; |
| 68 |
// alias name |
| 69 |
$return["alias"] = _("Authorized Services"); |
| 70 |
// module dependencies |
| 71 |
$return['dependencies'] = ['depends' => [], 'conflicts' => []]; |
| 72 |
// managed object classes |
| 73 |
$return['objectClasses'] = ['authorizedServiceObject']; |
| 74 |
// managed attributes |
| 75 |
$return['attributes'] = ['authorizedservice']; |
| 76 |
// help Entries |
| 77 |
$return['help'] = [ |
| 78 |
'authorizedservice' => [ |
| 79 |
"Headline" => _("Authorized Services"), 'attr' => 'authorizedService', |
| 80 |
"Text" => _("Service name (e.g. sshd, imap, ftp). Enter one service per entry.") . ' ' . _("Use * for all services.") |
| 81 |
], |
| 82 |
'authorizedservices' => [ |
| 83 |
"Headline" => _("Authorized Services"), 'attr' => 'authorizedService', |
| 84 |
"Text" => _("Comma separated list of services (e.g. sshd, imap, ftp).") . ' ' . _("Use * for all services.") |
| 85 |
], |
| 86 |
'autoAdd' => [ |
| 87 |
"Headline" => _("Automatically add this extension"), |
| 88 |
"Text" => _("This will enable the extension automatically if this profile is loaded.") |
| 89 |
], |
| 90 |
'predefinedServices' => [ |
| 91 |
"Headline" => _("Predefined services"), |
| 92 |
"Text" => _("These services will show up as hint if you enter a new service.") |
| 93 |
] |
| 94 |
]; |
| 95 |
// upload fields |
| 96 |
$return['upload_columns'] = [ |
| 97 |
[ |
| 98 |
'name' => 'authorizedService', |
| 99 |
'description' => _('Authorized Services'), |
| 100 |
'help' => 'authorizedServices', |
| 101 |
'example' => 'sshd, imap' |
| 102 |
] |
| 103 |
]; |
| 104 |
// available PDF fields |
| 105 |
$return['PDF_fields'] = [ |
| 106 |
'authorizedService' => _('Authorized Services') |
| 107 |
]; |
| 108 |
// profile options |
| 109 |
$profileContainer = new htmlResponsiveRow(); |
| 110 |
$profileContainer->add(new htmlResponsiveInputField(_('Authorized Services'), 'authorizedServiceObject_services', null, 'authorizedservices')); |
| 111 |
$profileContainer->add(new htmlResponsiveInputCheckbox('authorizedServiceObject_addExt', false, _('Automatically add this extension'), 'autoAdd')); |
| 112 |
$return['profile_options'] = $profileContainer; |
| 113 |
// profile checks |
| 114 |
$return['profile_checks']['authorizedServiceObject_services'] = ['type' => 'ext_preg', 'regex' => 'ascii', |
| 115 |
'error_message' => $this->messages['authorizedservice'][0]]; |
| 116 |
return $return; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* {@inheritDoc} |
| 121 |
*/ |
| 122 |
protected function load_Messages(): void { |
| 123 |
$this->messages['authorizedservice'][0] = ['ERROR', _('Authorized services are invalid.')]; // third array value is set dynamically |
| 124 |
$this->messages['authorizedservice'][1] = ['ERROR', _('Account %s:') . ' authorizedService', _('Please enter a valid list of service names.')]; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* {@inheritDoc} |
| 129 |
*/ |
| 130 |
public function save_attributes(): array { |
| 131 |
if (!in_array('authorizedServiceObject', $this->attributes['objectClass']) && !in_array('authorizedServiceObject', $this->orig['objectClass'])) { |
| 132 |
// skip saving if the extension was not added/modified |
| 133 |
return []; |
| 134 |
} |
| 135 |
return parent::save_attributes(); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* {@inheritDoc} |
| 140 |
*/ |
| 141 |
public function display_html_attributes(): htmlElement|array { |
| 142 |
if (isset($_POST['form_subpage_authorizedServiceObject_attributes_addObjectClass'])) { |
| 143 |
$this->attributes['objectClass'][] = 'authorizedServiceObject'; |
| 144 |
} |
| 145 |
$return = new htmlResponsiveRow(); |
| 146 |
if (in_array('authorizedServiceObject', $this->attributes['objectClass'])) { |
| 147 |
$autocompleteValues = []; |
| 148 |
if (isset($this->moduleSettings['authorizedServiceObject_services'])) { |
| 149 |
$autocompleteValues = $this->moduleSettings['authorizedServiceObject_services']; |
| 150 |
} |
| 151 |
$this->addMultiValueInputTextField($return, 'authorizedservice', _('Authorized Services'), false, null, false, $autocompleteValues); |
| 152 |
$return->addVerticalSpacer('2rem'); |
| 153 |
$remButton = new htmlAccountPageButton('authorizedServiceObject', 'attributes', 'remObjectClass', _('Remove Authorized Service extension')); |
| 154 |
$remButton->setCSSClasses(['lam-danger']); |
| 155 |
$return->add($remButton, 12, 12, 12, 'text-center'); |
| 156 |
} |
| 157 |
else { |
| 158 |
$return->add(new htmlAccountPageButton('authorizedServiceObject', 'attributes', 'addObjectClass', _('Add Authorized Service extension'))); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
return $return; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* {@inheritDoc} |
| 167 |
*/ |
| 168 |
public function process_attributes(): array { |
| 169 |
if (isset($_POST['form_subpage_authorizedServiceObject_attributes_remObjectClass'])) { |
| 170 |
$this->attributes['objectClass'] = array_delete(['authorizedServiceObject'], $this->attributes['objectClass']); |
| 171 |
if (isset($this->attributes['authorizedService'])) { |
| 172 |
unset($this->attributes['authorizedService']); |
| 173 |
} |
| 174 |
return []; |
| 175 |
} |
| 176 |
if (!in_array('authorizedServiceObject', $this->attributes['objectClass'])) { |
| 177 |
return []; |
| 178 |
} |
| 179 |
|
| 180 |
$errors = []; |
| 181 |
$this->processMultiValueInputTextField('authorizedservice', $errors, 'ascii'); |
| 182 |
return $errors; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* {@inheritDoc} |
| 187 |
*/ |
| 188 |
public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array { |
| 189 |
$messages = []; |
| 190 |
for ($i = 0; $i < count($rawAccounts); $i++) { |
| 191 |
// add object class |
| 192 |
if (!in_array("authorizedServiceObject", $partialAccounts[$i]['objectClass'])) { |
| 193 |
$partialAccounts[$i]['objectClass'][] = "authorizedServiceObject"; |
| 194 |
} |
| 195 |
// add ASs |
| 196 |
$this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'authorizedService', 'authorizedservice', 'ascii', $this->messages['authorizedservice'][1], $messages, '/,[ ]*/'); |
| 197 |
} |
| 198 |
return $messages; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* {@inheritDoc} |
| 203 |
*/ |
| 204 |
public function get_pdfEntries(array $pdfKeys, string $typeId): array { |
| 205 |
$return = []; |
| 206 |
if (in_array('authorizedServiceObject', $this->attributes['objectClass'])) { |
| 207 |
$this->addSimplePDFField($return, 'authorizedService', _('Authorized Services'), 'authorizedservice'); |
| 208 |
} |
| 209 |
return $return; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* {@inheritDoc} |
| 214 |
*/ |
| 215 |
public function load_profile(array $profile): void { |
| 216 |
// profile mappings in meta data |
| 217 |
parent::load_profile($profile); |
| 218 |
// add extension |
| 219 |
if (isset($profile['authorizedServiceObject_addExt'][0]) |
| 220 |
&& ($profile['authorizedServiceObject_addExt'][0] == "true") |
| 221 |
&& !in_array('authorizedServiceObject', $this->attributes['objectClass'])) { |
| 222 |
$this->attributes['objectClass'][] = 'authorizedServiceObject'; |
| 223 |
} |
| 224 |
// add ASs |
| 225 |
if (isset ($profile['authorizedServiceObject_services'][0]) && ($profile['authorizedServiceObject_services'][0] != "")) { |
| 226 |
$services = explode(',', $profile['authorizedServiceObject_services'][0]); |
| 227 |
for ($m = 0; $m < count($services); $m++) { |
| 228 |
if (get_preg($services[$m], 'ascii')) { |
| 229 |
$this->attributes['authorizedservice'][] = trim($services[$m]); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* {@inheritDoc} |
| 237 |
*/ |
| 238 |
public function get_configOptions(array $scopes, array $allScopes): htmlElement|array { |
| 239 |
$configContainer = new htmlResponsiveRow(); |
| 240 |
$configContainer->add(new htmlResponsiveInputTextarea('authorizedServiceObject_services', "sshd\r\nimap", 30, 5, _('Predefined services'), 'predefinedServices')); |
| 241 |
return $configContainer; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* @inheritDoc |
| 246 |
*/ |
| 247 |
public function getListAttributeDescriptions(ConfiguredType $type): array { |
| 248 |
return [ |
| 249 |
'authorizedservice' => _('Authorized Services') |
| 250 |
]; |
| 251 |
} |
| 252 |
|
| 253 |
} |