Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LDAPService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LDAPService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | use Injectable; |
||
| 43 | use Extensible; |
||
| 44 | use Configurable; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private static $dependencies = [ |
||
|
|
|||
| 50 | 'gateway' => '%$' . LDAPGateway::class |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * If configured, only user objects within these locations will be exposed to this service. |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | * @config |
||
| 58 | */ |
||
| 59 | private static $users_search_locations = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * If configured, only group objects within these locations will be exposed to this service. |
||
| 63 | * @var array |
||
| 64 | * |
||
| 65 | * @config |
||
| 66 | */ |
||
| 67 | private static $groups_search_locations = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Location to create new users in (distinguished name). |
||
| 71 | * @var string |
||
| 72 | * |
||
| 73 | * @config |
||
| 74 | */ |
||
| 75 | private static $new_users_dn; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Location to create new groups in (distinguished name). |
||
| 79 | * @var string |
||
| 80 | * |
||
| 81 | * @config |
||
| 82 | */ |
||
| 83 | private static $new_groups_dn; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | private static $_cache_nested_groups = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * If this is configured to a "Code" value of a {@link Group} in SilverStripe, the user will always |
||
| 92 | * be added to this group's membership when imported, regardless of any sort of group mappings. |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | * @config |
||
| 96 | */ |
||
| 97 | private static $default_group; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * For samba4 directory, there is no way to enforce password history on password resets. |
||
| 101 | * This only happens with changePassword (which requires the old password). |
||
| 102 | * This works around it by making the old password up and setting it administratively. |
||
| 103 | * |
||
| 104 | * A cleaner fix would be to use the LDAP_SERVER_POLICY_HINTS_OID connection flag, |
||
| 105 | * but it's not implemented in samba https://bugzilla.samba.org/show_bug.cgi?id=12020 |
||
| 106 | * |
||
| 107 | * @var bool |
||
| 108 | */ |
||
| 109 | private static $password_history_workaround = false; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get the cache object used for LDAP results. Note that the default lifetime set here |
||
| 113 | * is 8 hours, but you can change that by adding configuration: |
||
| 114 | * |
||
| 115 | * <code> |
||
| 116 | * SilverStripe\Core\Injector\Injector: |
||
| 117 | * Psr\SimpleCache\CacheInterface.ldap: |
||
| 118 | * constructor: |
||
| 119 | * defaultLifetime: 3600 # time in seconds |
||
| 120 | * </code> |
||
| 121 | * |
||
| 122 | * @return Psr\SimpleCache\CacheInterface |
||
| 123 | */ |
||
| 124 | public static function get_cache() |
||
| 125 | { |
||
| 126 | return Injector::inst()->get(CacheInterface::class . '.ldap'); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Flushes out the LDAP results cache when flush=1 is called. |
||
| 131 | */ |
||
| 132 | public static function flush() |
||
| 133 | { |
||
| 134 | /** @var CacheInterface $cache */ |
||
| 135 | $cache = self::get_cache(); |
||
| 136 | $cache->clear(); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var LDAPGateway |
||
| 141 | */ |
||
| 142 | public $gateway; |
||
| 143 | |||
| 144 | public function __construct() |
||
| 145 | { |
||
| 146 | $this->constructExtensions(); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Setter for gateway. Useful for overriding the gateway with a fake for testing. |
||
| 151 | * @var LDAPGateway |
||
| 152 | */ |
||
| 153 | public function setGateway($gateway) |
||
| 154 | { |
||
| 155 | $this->gateway = $gateway; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Checkes whether or not the service is enabled. |
||
| 160 | * |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public function enabled() |
||
| 164 | { |
||
| 165 | $options = Config::inst()->get(LDAPGateway::class, 'options'); |
||
| 166 | return !empty($options); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Authenticate the given username and password with LDAP. |
||
| 171 | * |
||
| 172 | * @param string $username |
||
| 173 | * @param string $password |
||
| 174 | * |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | public function authenticate($username, $password) |
||
| 178 | { |
||
| 179 | $result = $this->gateway->authenticate($username, $password); |
||
| 180 | $messages = $result->getMessages(); |
||
| 181 | |||
| 182 | // all messages beyond the first one are for debugging and |
||
| 183 | // not suitable to display to the user. |
||
| 184 | foreach ($messages as $i => $message) { |
||
| 185 | if ($i > 0) { |
||
| 186 | $this->getLogger()->debug(str_replace("\n", "\n ", $message)); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $message = $messages[0]; // first message is user readable, suitable for showing on login form |
||
| 191 | |||
| 192 | // show better errors than the defaults for various status codes returned by LDAP |
||
| 193 | View Code Duplication | if (!empty($messages[1]) && strpos($messages[1], 'NT_STATUS_ACCOUNT_LOCKED_OUT') !== false) { |
|
| 194 | $message = _t( |
||
| 195 | __CLASS__ . '.ACCOUNTLOCKEDOUT', |
||
| 196 | 'Your account has been temporarily locked because of too many failed login attempts. ' . |
||
| 197 | 'Please try again later.' |
||
| 198 | ); |
||
| 199 | } |
||
| 200 | View Code Duplication | if (!empty($messages[1]) && strpos($messages[1], 'NT_STATUS_LOGON_FAILURE') !== false) { |
|
| 201 | $message = _t( |
||
| 202 | __CLASS__ . '.INVALIDCREDENTIALS', |
||
| 203 | 'The provided details don\'t seem to be correct. Please try again.' |
||
| 204 | ); |
||
| 205 | } |
||
| 206 | |||
| 207 | return [ |
||
| 208 | 'success' => $result->getCode() === 1, |
||
| 209 | 'identity' => $result->getIdentity(), |
||
| 210 | 'message' => $message, |
||
| 211 | 'code' => $result->getCode() |
||
| 212 | ]; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Return all nodes (organizational units, containers, and domains) within the current base DN. |
||
| 217 | * |
||
| 218 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
| 219 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | public function getNodes($cached = true, $attributes = []) |
||
| 223 | { |
||
| 224 | $cache = self::get_cache(); |
||
| 225 | $cacheKey = 'nodes' . md5(implode('', $attributes)); |
||
| 226 | $results = $cache->has($cacheKey); |
||
| 227 | |||
| 228 | if (!$results || !$cached) { |
||
| 229 | $results = []; |
||
| 230 | $records = $this->gateway->getNodes(null, Ldap::SEARCH_SCOPE_SUB, $attributes); |
||
| 231 | foreach ($records as $record) { |
||
| 232 | $results[$record['dn']] = $record; |
||
| 233 | } |
||
| 234 | |||
| 235 | $cache->set($cacheKey, $results); |
||
| 236 | } |
||
| 237 | |||
| 238 | return $results; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Return all AD groups in configured search locations, including all nested groups. |
||
| 243 | * Uses groups_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
| 244 | * to use the default baseDn defined in the connection. |
||
| 245 | * |
||
| 246 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
| 247 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 248 | * @param string $indexBy Attribute to use as an index. |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | public function getGroups($cached = true, $attributes = [], $indexBy = 'dn') |
||
| 252 | { |
||
| 253 | $searchLocations = $this->config()->groups_search_locations ?: [null]; |
||
| 254 | $cache = self::get_cache(); |
||
| 255 | $cacheKey = 'groups' . md5(implode('', array_merge($searchLocations, $attributes))); |
||
| 256 | $results = $cache->has($cacheKey); |
||
| 257 | |||
| 258 | if (!$results || !$cached) { |
||
| 259 | $results = []; |
||
| 260 | View Code Duplication | foreach ($searchLocations as $searchLocation) { |
|
| 261 | $records = $this->gateway->getGroups($searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes); |
||
| 262 | if (!$records) { |
||
| 263 | continue; |
||
| 264 | } |
||
| 265 | |||
| 266 | foreach ($records as $record) { |
||
| 267 | $results[$record[$indexBy]] = $record; |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | $cache->set($cacheKey, $results); |
||
| 272 | } |
||
| 273 | |||
| 274 | if ($cached && $results === true) { |
||
| 275 | $results = $cache->get($cacheKey); |
||
| 276 | } |
||
| 277 | |||
| 278 | return $results; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Return all member groups (and members of those, recursively) underneath a specific group DN. |
||
| 283 | * Note that these get cached in-memory per-request for performance to avoid re-querying for the same results. |
||
| 284 | * |
||
| 285 | * @param string $dn |
||
| 286 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public function getNestedGroups($dn, $attributes = []) |
||
| 290 | { |
||
| 291 | if (isset(self::$_cache_nested_groups[$dn])) { |
||
| 292 | return self::$_cache_nested_groups[$dn]; |
||
| 293 | } |
||
| 294 | |||
| 295 | $searchLocations = $this->config()->groups_search_locations ?: [null]; |
||
| 296 | $results = []; |
||
| 297 | View Code Duplication | foreach ($searchLocations as $searchLocation) { |
|
| 298 | $records = $this->gateway->getNestedGroups($dn, $searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes); |
||
| 299 | foreach ($records as $record) { |
||
| 300 | $results[$record['dn']] = $record; |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | self::$_cache_nested_groups[$dn] = $results; |
||
| 305 | return $results; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get a particular AD group's data given a GUID. |
||
| 310 | * |
||
| 311 | * @param string $guid |
||
| 312 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | View Code Duplication | public function getGroupByGUID($guid, $attributes = []) |
|
| 316 | { |
||
| 317 | $searchLocations = $this->config()->groups_search_locations ?: [null]; |
||
| 318 | foreach ($searchLocations as $searchLocation) { |
||
| 319 | $records = $this->gateway->getGroupByGUID($guid, $searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes); |
||
| 320 | if ($records) { |
||
| 321 | return $records[0]; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Get a particular AD group's data given a DN. |
||
| 328 | * |
||
| 329 | * @param string $dn |
||
| 330 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 331 | * @return array |
||
| 332 | */ |
||
| 333 | View Code Duplication | public function getGroupByDN($dn, $attributes = []) |
|
| 334 | { |
||
| 335 | $searchLocations = $this->config()->groups_search_locations ?: [null]; |
||
| 336 | foreach ($searchLocations as $searchLocation) { |
||
| 337 | $records = $this->gateway->getGroupByDN($dn, $searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes); |
||
| 338 | if ($records) { |
||
| 339 | return $records[0]; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Return all AD users in configured search locations, including all users in nested groups. |
||
| 346 | * Uses users_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
| 347 | * to use the default baseDn defined in the connection. |
||
| 348 | * |
||
| 349 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | public function getUsers($attributes = []) |
||
| 353 | { |
||
| 354 | $searchLocations = $this->config()->users_search_locations ?: [null]; |
||
| 355 | $results = []; |
||
| 356 | |||
| 357 | View Code Duplication | foreach ($searchLocations as $searchLocation) { |
|
| 358 | $records = $this->gateway->getUsers($searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes); |
||
| 359 | if (!$records) { |
||
| 360 | continue; |
||
| 361 | } |
||
| 362 | |||
| 363 | foreach ($records as $record) { |
||
| 364 | $results[$record['objectguid']] = $record; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | return $results; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get a specific AD user's data given a GUID. |
||
| 373 | * |
||
| 374 | * @param string $guid |
||
| 375 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | View Code Duplication | public function getUserByGUID($guid, $attributes = []) |
|
| 379 | { |
||
| 380 | $searchLocations = $this->config()->users_search_locations ?: [null]; |
||
| 381 | foreach ($searchLocations as $searchLocation) { |
||
| 382 | $records = $this->gateway->getUserByGUID($guid, $searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes); |
||
| 383 | if ($records) { |
||
| 384 | return $records[0]; |
||
| 385 | } |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get a specific AD user's data given a DN. |
||
| 391 | * |
||
| 392 | * @param string $dn |
||
| 393 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function getUserByDN($dn, $attributes = []) |
|
| 398 | { |
||
| 399 | $searchLocations = $this->config()->users_search_locations ?: [null]; |
||
| 400 | foreach ($searchLocations as $searchLocation) { |
||
| 401 | $records = $this->gateway->getUserByDN($dn, $searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes); |
||
| 402 | if ($records) { |
||
| 403 | return $records[0]; |
||
| 404 | } |
||
| 405 | } |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Get a specific user's data given an email. |
||
| 410 | * |
||
| 411 | * @param string $email |
||
| 412 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 413 | * @return array |
||
| 414 | */ |
||
| 415 | View Code Duplication | public function getUserByEmail($email, $attributes = []) |
|
| 416 | { |
||
| 417 | $searchLocations = $this->config()->users_search_locations ?: [null]; |
||
| 418 | foreach ($searchLocations as $searchLocation) { |
||
| 419 | $records = $this->gateway->getUserByEmail($email, $searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes); |
||
| 420 | if ($records) { |
||
| 421 | return $records[0]; |
||
| 422 | } |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Get a specific user's data given a username. |
||
| 428 | * |
||
| 429 | * @param string $username |
||
| 430 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | View Code Duplication | public function getUserByUsername($username, $attributes = []) |
|
| 434 | { |
||
| 435 | $searchLocations = $this->config()->users_search_locations ?: [null]; |
||
| 436 | foreach ($searchLocations as $searchLocation) { |
||
| 437 | $records = $this->gateway->getUserByUsername( |
||
| 438 | $username, |
||
| 439 | $searchLocation, |
||
| 440 | Ldap::SEARCH_SCOPE_SUB, |
||
| 441 | $attributes |
||
| 442 | ); |
||
| 443 | if ($records) { |
||
| 444 | return $records[0]; |
||
| 445 | } |
||
| 446 | } |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Get a username for an email. |
||
| 451 | * |
||
| 452 | * @param string $email |
||
| 453 | * @return string|null |
||
| 454 | */ |
||
| 455 | public function getUsernameByEmail($email) |
||
| 456 | { |
||
| 457 | $data = $this->getUserByEmail($email); |
||
| 458 | if (empty($data)) { |
||
| 459 | return null; |
||
| 460 | } |
||
| 461 | |||
| 462 | return $this->gateway->getCanonicalUsername($data); |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Given a group DN, get the group membership data in LDAP. |
||
| 467 | * |
||
| 468 | * @param string $dn |
||
| 469 | * @return array |
||
| 470 | */ |
||
| 471 | public function getLDAPGroupMembers($dn) |
||
| 472 | { |
||
| 473 | $groupObj = Group::get()->filter('DN', $dn)->first(); |
||
| 474 | $groupData = $this->getGroupByGUID($groupObj->GUID); |
||
| 475 | $members = !empty($groupData['member']) ? $groupData['member'] : []; |
||
| 476 | // If a user belongs to a single group, this comes through as a string. |
||
| 477 | // Normalise to a array so it's consistent. |
||
| 478 | if ($members && is_string($members)) { |
||
| 479 | $members = [$members]; |
||
| 480 | } |
||
| 481 | |||
| 482 | return $members; |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Update the current Member record with data from LDAP. |
||
| 487 | * |
||
| 488 | * It's allowed to pass an unwritten Member record here, because it's not always possible to satisfy |
||
| 489 | * field constraints without importing data from LDAP (for example if the application requires Username |
||
| 490 | * through a Validator). Even though unwritten, it still must have the GUID set. |
||
| 491 | * |
||
| 492 | * Constraints: |
||
| 493 | * - GUID of the member must have already been set, for integrity reasons we don't allow it to change here. |
||
| 494 | * |
||
| 495 | * @param Member $member |
||
| 496 | * @param array|null $data If passed, this is pre-existing AD attribute data to update the Member with. |
||
| 497 | * If not given, the data will be looked up by the user's GUID. |
||
| 498 | * @param bool $updateGroups controls whether to run the resource-intensive group update function as well. This is |
||
| 499 | * skipped during login to reduce load. |
||
| 500 | * @return bool |
||
| 501 | * @internal param $Member |
||
| 502 | */ |
||
| 503 | public function updateMemberFromLDAP(Member $member, $data = null, $updateGroups = true) |
||
| 504 | { |
||
| 505 | if (!$this->enabled()) { |
||
| 506 | return false; |
||
| 507 | } |
||
| 508 | |||
| 509 | if (!$member->GUID) { |
||
| 510 | $this->getLogger()->warning(sprintf('Cannot update Member ID %s, GUID not set', $member->ID)); |
||
| 511 | return false; |
||
| 512 | } |
||
| 513 | |||
| 514 | if (!$data) { |
||
| 515 | $data = $this->getUserByGUID($member->GUID); |
||
| 516 | if (!$data) { |
||
| 517 | $this->getLogger()->warning(sprintf('Could not retrieve data for user. GUID: %s', $member->GUID)); |
||
| 518 | return false; |
||
| 519 | } |
||
| 520 | } |
||
| 521 | |||
| 522 | $member->IsExpired = ($data['useraccountcontrol'] & 2) == 2; |
||
| 523 | $member->LastSynced = (string)DBDatetime::now(); |
||
| 524 | |||
| 525 | foreach ($member->config()->ldap_field_mappings as $attribute => $field) { |
||
| 526 | if (!isset($data[$attribute])) { |
||
| 527 | $this->getLogger()->notice( |
||
| 528 | sprintf( |
||
| 529 | 'Attribute %s configured in Member.ldap_field_mappings, ' . |
||
| 530 | 'but no available attribute in AD data (GUID: %s, Member ID: %s)', |
||
| 531 | $attribute, |
||
| 532 | $data['objectguid'], |
||
| 533 | $member->ID |
||
| 534 | ) |
||
| 535 | ); |
||
| 536 | |||
| 537 | continue; |
||
| 538 | } |
||
| 539 | |||
| 540 | if ($attribute == 'thumbnailphoto') { |
||
| 541 | $imageClass = $member->getRelationClass($field); |
||
| 542 | if ($imageClass !== Image::class |
||
| 543 | && !is_subclass_of($imageClass, Image::class) |
||
| 544 | ) { |
||
| 545 | $this->getLogger()->warning( |
||
| 546 | sprintf( |
||
| 547 | 'Member field %s configured for thumbnailphoto AD attribute, but it isn\'t a ' . |
||
| 548 | 'valid relation to an Image class', |
||
| 549 | $field |
||
| 550 | ) |
||
| 551 | ); |
||
| 552 | |||
| 553 | continue; |
||
| 554 | } |
||
| 555 | |||
| 556 | $filename = sprintf('thumbnailphoto-%s.jpg', $data['samaccountname']); |
||
| 557 | $path = ASSETS_DIR . '/' . $member->config()->ldap_thumbnail_path; |
||
| 558 | $absPath = BASE_PATH . '/' . $path; |
||
| 559 | if (!file_exists($absPath)) { |
||
| 560 | Filesystem::makeFolder($absPath); |
||
| 561 | } |
||
| 562 | |||
| 563 | // remove existing record if it exists |
||
| 564 | $existingObj = $member->getComponent($field); |
||
| 565 | if ($existingObj && $existingObj->exists()) { |
||
| 566 | $existingObj->delete(); |
||
| 567 | } |
||
| 568 | |||
| 569 | // The image data is provided in raw binary. |
||
| 570 | file_put_contents($absPath . '/' . $filename, $data[$attribute]); |
||
| 571 | $record = new $imageClass(); |
||
| 572 | $record->Name = $filename; |
||
| 573 | $record->Filename = $path . '/' . $filename; |
||
| 574 | $record->write(); |
||
| 575 | |||
| 576 | $relationField = $field . 'ID'; |
||
| 577 | $member->{$relationField} = $record->ID; |
||
| 578 | } else { |
||
| 579 | $member->$field = $data[$attribute]; |
||
| 580 | } |
||
| 581 | } |
||
| 582 | |||
| 583 | // if a default group was configured, ensure the user is in that group |
||
| 584 | if ($this->config()->default_group) { |
||
| 585 | $group = Group::get()->filter('Code', $this->config()->default_group)->limit(1)->first(); |
||
| 586 | if (!($group && $group->exists())) { |
||
| 587 | $this->getLogger()->warning( |
||
| 588 | sprintf( |
||
| 589 | 'LDAPService.default_group misconfiguration! There is no such group with Code = \'%s\'', |
||
| 590 | $this->config()->default_group |
||
| 591 | ) |
||
| 592 | ); |
||
| 593 | } else { |
||
| 594 | $group->Members()->add($member, [ |
||
| 595 | 'IsImportedFromLDAP' => '1' |
||
| 596 | ]); |
||
| 597 | } |
||
| 598 | } |
||
| 599 | |||
| 600 | // this is to keep track of which groups the user gets mapped to |
||
| 601 | // and we'll use that later to remove them from any groups that they're no longer mapped to |
||
| 602 | $mappedGroupIDs = []; |
||
| 603 | |||
| 604 | // Member must have an ID before manipulating Groups, otherwise they will not be added correctly. |
||
| 605 | // However we cannot do a full ->write before the groups are associated, because this will upsync |
||
| 606 | // the Member, in effect deleting all their LDAP group associations! |
||
| 607 | $member->writeWithoutSync(); |
||
| 608 | |||
| 609 | if ($updateGroups) { |
||
| 610 | $this->updateMemberGroups($data, $member); |
||
| 611 | } |
||
| 612 | |||
| 613 | // This will throw an exception if there are two distinct GUIDs with the same email address. |
||
| 614 | // We are happy with a raw 500 here at this stage. |
||
| 615 | $member->write(); |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Ensure the user is mapped to any applicable groups. |
||
| 620 | * @param array $data |
||
| 621 | * @param Member $member |
||
| 622 | */ |
||
| 623 | public function updateMemberGroups($data, Member $member) |
||
| 624 | { |
||
| 625 | if (isset($data['memberof'])) { |
||
| 626 | $ldapGroups = is_array($data['memberof']) ? $data['memberof'] : [$data['memberof']]; |
||
| 627 | foreach ($ldapGroups as $groupDN) { |
||
| 628 | foreach (LDAPGroupMapping::get() as $mapping) { |
||
| 629 | if (!$mapping->DN) { |
||
| 630 | $this->getLogger()->warning( |
||
| 631 | sprintf( |
||
| 632 | 'LDAPGroupMapping ID %s is missing DN field. Skipping', |
||
| 633 | $mapping->ID |
||
| 634 | ) |
||
| 635 | ); |
||
| 636 | continue; |
||
| 637 | } |
||
| 638 | |||
| 639 | // the user is a direct member of group with a mapping, add them to the SS group. |
||
| 640 | View Code Duplication | if ($mapping->DN == $groupDN) { |
|
| 641 | $group = $mapping->Group(); |
||
| 642 | if ($group && $group->exists()) { |
||
| 643 | $group->Members()->add($member, [ |
||
| 644 | 'IsImportedFromLDAP' => '1' |
||
| 645 | ]); |
||
| 646 | $mappedGroupIDs[] = $mapping->GroupID; |
||
| 647 | } |
||
| 648 | } |
||
| 649 | |||
| 650 | // the user *might* be a member of a nested group provided the scope of the mapping |
||
| 651 | // is to include the entire subtree. Check all those mappings and find the LDAP child groups |
||
| 652 | // to see if they are a member of one of those. If they are, add them to the SS group |
||
| 653 | if ($mapping->Scope == 'Subtree') { |
||
| 654 | $childGroups = $this->getNestedGroups($mapping->DN, ['dn']); |
||
| 655 | if (!$childGroups) { |
||
| 656 | continue; |
||
| 657 | } |
||
| 658 | |||
| 659 | foreach ($childGroups as $childGroupDN => $childGroupRecord) { |
||
| 660 | View Code Duplication | if ($childGroupDN == $groupDN) { |
|
| 661 | $group = $mapping->Group(); |
||
| 662 | if ($group && $group->exists()) { |
||
| 663 | $group->Members()->add($member, [ |
||
| 664 | 'IsImportedFromLDAP' => '1' |
||
| 665 | ]); |
||
| 666 | $mappedGroupIDs[] = $mapping->GroupID; |
||
| 667 | } |
||
| 668 | } |
||
| 669 | } |
||
| 670 | } |
||
| 671 | } |
||
| 672 | } |
||
| 673 | } |
||
| 674 | |||
| 675 | // remove the user from any previously mapped groups, where the mapping has since been removed |
||
| 676 | $groupRecords = DB::query( |
||
| 677 | sprintf( |
||
| 678 | 'SELECT "GroupID" FROM "Group_Members" WHERE "IsImportedFromLDAP" = 1 AND "MemberID" = %s', |
||
| 679 | $member->ID |
||
| 680 | ) |
||
| 681 | ); |
||
| 682 | |||
| 683 | if (!empty($mappedGroupIDs)) { |
||
| 684 | foreach ($groupRecords as $groupRecord) { |
||
| 685 | if (!in_array($groupRecord['GroupID'], $mappedGroupIDs)) { |
||
| 686 | $group = Group::get()->byId($groupRecord['GroupID']); |
||
| 687 | // Some groups may no longer exist. SilverStripe does not clean up join tables. |
||
| 688 | if ($group) { |
||
| 689 | $group->Members()->remove($member); |
||
| 690 | } |
||
| 691 | } |
||
| 692 | } |
||
| 693 | } |
||
| 694 | } |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Sync a specific Group by updating it with LDAP data. |
||
| 698 | * |
||
| 699 | * @param Group $group An existing Group or a new Group object |
||
| 700 | * @param array $data LDAP group object data |
||
| 701 | * |
||
| 702 | * @return bool |
||
| 703 | */ |
||
| 704 | public function updateGroupFromLDAP(Group $group, $data) |
||
| 705 | { |
||
| 706 | if (!$this->enabled()) { |
||
| 707 | return false; |
||
| 708 | } |
||
| 709 | |||
| 710 | // Synchronise specific guaranteed fields. |
||
| 711 | $group->Code = $data['samaccountname']; |
||
| 712 | $group->Title = $data['samaccountname']; |
||
| 713 | if (!empty($data['description'])) { |
||
| 714 | $group->Description = $data['description']; |
||
| 715 | } |
||
| 716 | $group->DN = $data['dn']; |
||
| 717 | $group->LastSynced = (string)DBDatetime::now(); |
||
| 718 | $group->write(); |
||
| 719 | |||
| 720 | // Mappings on this group are automatically maintained to contain just the group's DN. |
||
| 721 | // First, scan through existing mappings and remove ones that are not matching (in case the group moved). |
||
| 722 | $hasCorrectMapping = false; |
||
| 723 | foreach ($group->LDAPGroupMappings() as $mapping) { |
||
| 724 | if ($mapping->DN === $data['dn']) { |
||
| 725 | // This is the correct mapping we want to retain. |
||
| 726 | $hasCorrectMapping = true; |
||
| 727 | } else { |
||
| 728 | $mapping->delete(); |
||
| 729 | } |
||
| 730 | } |
||
| 731 | |||
| 732 | // Second, if the main mapping was not found, add it in. |
||
| 733 | if (!$hasCorrectMapping) { |
||
| 734 | $mapping = new LDAPGroupMapping(); |
||
| 735 | $mapping->DN = $data['dn']; |
||
| 736 | $mapping->write(); |
||
| 737 | $group->LDAPGroupMappings()->add($mapping); |
||
| 738 | } |
||
| 739 | } |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Creates a new LDAP user from the passed Member record. |
||
| 743 | * Note that the Member record must have a non-empty Username field for this to work. |
||
| 744 | * |
||
| 745 | * @param Member $member |
||
| 746 | * @throws ValidationException |
||
| 747 | * @throws Exception |
||
| 748 | */ |
||
| 749 | public function createLDAPUser(Member $member) |
||
| 750 | { |
||
| 751 | if (!$this->enabled()) { |
||
| 752 | return; |
||
| 753 | } |
||
| 754 | if (empty($member->Username)) { |
||
| 755 | throw new ValidationException('Member missing Username. Cannot create LDAP user'); |
||
| 756 | } |
||
| 757 | if (!$this->config()->new_users_dn) { |
||
| 758 | throw new Exception('LDAPService::new_users_dn must be configured to create LDAP users'); |
||
| 759 | } |
||
| 760 | |||
| 761 | // Normalise username to lowercase to ensure we don't have duplicates of different cases |
||
| 762 | $member->Username = strtolower($member->Username); |
||
| 763 | |||
| 764 | // Create user in LDAP using available information. |
||
| 765 | $dn = sprintf('CN=%s,%s', $member->Username, $this->config()->new_users_dn); |
||
| 766 | |||
| 767 | try { |
||
| 768 | $this->add($dn, [ |
||
| 769 | 'objectclass' => 'user', |
||
| 770 | 'cn' => $member->Username, |
||
| 771 | 'accountexpires' => '9223372036854775807', |
||
| 772 | 'useraccountcontrol' => '66048', |
||
| 773 | 'userprincipalname' => sprintf( |
||
| 774 | '%s@%s', |
||
| 775 | $member->Username, |
||
| 776 | $this->gateway->config()->options['accountDomainName'] |
||
| 777 | ), |
||
| 778 | ]); |
||
| 779 | } catch (Exception $e) { |
||
| 780 | throw new ValidationException('LDAP synchronisation failure: ' . $e->getMessage()); |
||
| 781 | } |
||
| 782 | |||
| 783 | $user = $this->getUserByUsername($member->Username); |
||
| 784 | if (empty($user['objectguid'])) { |
||
| 785 | throw new ValidationException('LDAP synchronisation failure: user missing GUID'); |
||
| 786 | } |
||
| 787 | |||
| 788 | // Creation was successful, mark the user as LDAP managed by setting the GUID. |
||
| 789 | $member->GUID = $user['objectguid']; |
||
| 790 | } |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Creates a new LDAP group from the passed Group record. |
||
| 794 | * |
||
| 795 | * @param Group $group |
||
| 796 | * @throws ValidationException |
||
| 797 | */ |
||
| 798 | public function createLDAPGroup(Group $group) |
||
| 799 | { |
||
| 800 | if (!$this->enabled()) { |
||
| 801 | return; |
||
| 802 | } |
||
| 803 | if (empty($group->Title)) { |
||
| 804 | throw new ValidationException('Group missing Title. Cannot create LDAP group'); |
||
| 805 | } |
||
| 806 | if (!$this->config()->new_groups_dn) { |
||
| 807 | throw new Exception('LDAPService::new_groups_dn must be configured to create LDAP groups'); |
||
| 808 | } |
||
| 809 | |||
| 810 | // LDAP isn't really meant to distinguish between a Title and Code. Squash them. |
||
| 811 | $group->Code = $group->Title; |
||
| 812 | |||
| 813 | $dn = sprintf('CN=%s,%s', $group->Title, $this->config()->new_groups_dn); |
||
| 814 | try { |
||
| 815 | $this->add($dn, [ |
||
| 816 | 'objectclass' => 'group', |
||
| 817 | 'cn' => $group->Title, |
||
| 818 | 'name' => $group->Title, |
||
| 819 | 'samaccountname' => $group->Title, |
||
| 820 | 'description' => $group->Description, |
||
| 821 | 'distinguishedname' => $dn |
||
| 822 | ]); |
||
| 823 | } catch (Exception $e) { |
||
| 824 | throw new ValidationException('LDAP group creation failure: ' . $e->getMessage()); |
||
| 825 | } |
||
| 826 | |||
| 827 | $data = $this->getGroupByDN($dn); |
||
| 828 | if (empty($data['objectguid'])) { |
||
| 829 | throw new ValidationException( |
||
| 830 | new ValidationResult( |
||
| 831 | false, |
||
| 832 | 'LDAP group creation failure: group might have been created in LDAP. GUID is missing.' |
||
| 833 | ) |
||
| 834 | ); |
||
| 835 | } |
||
| 836 | |||
| 837 | // Creation was successful, mark the group as LDAP managed by setting the GUID. |
||
| 838 | $group->GUID = $data['objectguid']; |
||
| 839 | $group->DN = $data['dn']; |
||
| 840 | } |
||
| 841 | |||
| 842 | /** |
||
| 843 | * Update the Member data back to the corresponding LDAP user object. |
||
| 844 | * |
||
| 845 | * @param Member $member |
||
| 846 | * @throws ValidationException |
||
| 847 | */ |
||
| 848 | public function updateLDAPFromMember(Member $member) |
||
| 849 | { |
||
| 850 | if (!$this->enabled()) { |
||
| 851 | return; |
||
| 852 | } |
||
| 853 | if (!$member->GUID) { |
||
| 854 | throw new ValidationException('Member missing GUID. Cannot update LDAP user'); |
||
| 855 | } |
||
| 856 | |||
| 857 | $data = $this->getUserByGUID($member->GUID); |
||
| 858 | if (empty($data['objectguid'])) { |
||
| 859 | throw new ValidationException('LDAP synchronisation failure: user missing GUID'); |
||
| 860 | } |
||
| 861 | |||
| 862 | if (empty($member->Username)) { |
||
| 863 | throw new ValidationException('Member missing Username. Cannot update LDAP user'); |
||
| 864 | } |
||
| 865 | |||
| 866 | $dn = $data['distinguishedname']; |
||
| 867 | |||
| 868 | // Normalise username to lowercase to ensure we don't have duplicates of different cases |
||
| 869 | $member->Username = strtolower($member->Username); |
||
| 870 | |||
| 871 | try { |
||
| 872 | // If the common name (cn) has changed, we need to ensure they've been moved |
||
| 873 | // to the new DN, to avoid any clashes between user objects. |
||
| 874 | if ($data['cn'] != $member->Username) { |
||
| 875 | $newDn = sprintf('CN=%s,%s', $member->Username, preg_replace('/^CN=(.+?),/', '', $dn)); |
||
| 876 | $this->move($dn, $newDn); |
||
| 877 | $dn = $newDn; |
||
| 878 | } |
||
| 879 | } catch (Exception $e) { |
||
| 880 | throw new ValidationException('LDAP move failure: '.$e->getMessage()); |
||
| 881 | } |
||
| 882 | |||
| 883 | try { |
||
| 884 | $attributes = [ |
||
| 885 | 'displayname' => sprintf('%s %s', $member->FirstName, $member->Surname), |
||
| 886 | 'name' => sprintf('%s %s', $member->FirstName, $member->Surname), |
||
| 887 | 'userprincipalname' => sprintf( |
||
| 888 | '%s@%s', |
||
| 889 | $member->Username, |
||
| 890 | $this->gateway->config()->options['accountDomainName'] |
||
| 891 | ), |
||
| 892 | ]; |
||
| 893 | foreach ($member->config()->ldap_field_mappings as $attribute => $field) { |
||
| 894 | $relationClass = $member->getRelationClass($field); |
||
| 895 | if ($relationClass) { |
||
| 896 | // todo no support for writing back relations yet. |
||
| 897 | } else { |
||
| 898 | $attributes[$attribute] = $member->$field; |
||
| 899 | } |
||
| 900 | } |
||
| 901 | |||
| 902 | $this->update($dn, $attributes); |
||
| 903 | } catch (Exception $e) { |
||
| 904 | throw new ValidationException('LDAP synchronisation failure: '.$e->getMessage()); |
||
| 905 | } |
||
| 906 | } |
||
| 907 | |||
| 908 | /** |
||
| 909 | * Ensure the user belongs to the correct groups in LDAP from their membership |
||
| 910 | * to local LDAP mapped SilverStripe groups. |
||
| 911 | * |
||
| 912 | * This also removes them from LDAP groups if they've been taken out of one. |
||
| 913 | * It will not affect group membership of non-mapped groups, so it will |
||
| 914 | * not touch such internal AD groups like "Domain Users". |
||
| 915 | * |
||
| 916 | * @param Member $member |
||
| 917 | * @throws ValidationException |
||
| 918 | */ |
||
| 919 | public function updateLDAPGroupsForMember(Member $member) |
||
| 920 | { |
||
| 921 | if (!$this->enabled()) { |
||
| 922 | return; |
||
| 923 | } |
||
| 924 | if (!$member->GUID) { |
||
| 925 | throw new ValidationException('Member missing GUID. Cannot update LDAP user'); |
||
| 926 | } |
||
| 927 | |||
| 928 | $addGroups = []; |
||
| 929 | $removeGroups = []; |
||
| 930 | |||
| 931 | $user = $this->getUserByGUID($member->GUID); |
||
| 932 | if (empty($user['objectguid'])) { |
||
| 933 | throw new ValidationException('LDAP update failure: user missing GUID'); |
||
| 934 | } |
||
| 935 | |||
| 936 | // If a user belongs to a single group, this comes through as a string. |
||
| 937 | // Normalise to a array so it's consistent. |
||
| 938 | $existingGroups = !empty($user['memberof']) ? $user['memberof'] : []; |
||
| 939 | if ($existingGroups && is_string($existingGroups)) { |
||
| 940 | $existingGroups = [$existingGroups]; |
||
| 941 | } |
||
| 942 | |||
| 943 | foreach ($member->Groups() as $group) { |
||
| 944 | if (!$group->GUID) { |
||
| 945 | continue; |
||
| 946 | } |
||
| 947 | |||
| 948 | // mark this group as something we need to ensure the user belongs to in LDAP. |
||
| 949 | $addGroups[] = $group->DN; |
||
| 950 | } |
||
| 951 | |||
| 952 | // Which existing LDAP groups are not in the add groups? We'll check these groups to |
||
| 953 | // see if the user should be removed from any of them. |
||
| 954 | $remainingGroups = array_diff($existingGroups, $addGroups); |
||
| 955 | |||
| 956 | foreach ($remainingGroups as $groupDn) { |
||
| 957 | // We only want to be removing groups we have a local Group mapped to. Removing |
||
| 958 | // membership for anything else would be bad! |
||
| 959 | $group = Group::get()->filter('DN', $groupDn)->first(); |
||
| 960 | if (!$group || !$group->exists()) { |
||
| 961 | continue; |
||
| 962 | } |
||
| 963 | |||
| 964 | // this group should be removed from the user's memberof attribute, as it's been removed. |
||
| 965 | $removeGroups[] = $groupDn; |
||
| 966 | } |
||
| 967 | |||
| 968 | // go through the groups we want the user to be in and ensure they're in them. |
||
| 969 | foreach ($addGroups as $groupDn) { |
||
| 970 | $this->addLDAPUserToGroup($user['distinguishedname'], $groupDn); |
||
| 971 | } |
||
| 972 | |||
| 973 | // go through the groups we _don't_ want the user to be in and ensure they're taken out of them. |
||
| 974 | foreach ($removeGroups as $groupDn) { |
||
| 975 | $members = $this->getLDAPGroupMembers($groupDn); |
||
| 976 | |||
| 977 | // remove the user from the members data. |
||
| 978 | if (in_array($user['distinguishedname'], $members)) { |
||
| 979 | foreach ($members as $i => $dn) { |
||
| 980 | if ($dn == $user['distinguishedname']) { |
||
| 981 | unset($members[$i]); |
||
| 982 | } |
||
| 983 | } |
||
| 984 | } |
||
| 985 | |||
| 986 | try { |
||
| 987 | $this->update($groupDn, ['member' => $members]); |
||
| 988 | } catch (Exception $e) { |
||
| 989 | throw new ValidationException('LDAP group membership remove failure: ' . $e->getMessage()); |
||
| 990 | } |
||
| 991 | } |
||
| 992 | } |
||
| 993 | |||
| 994 | /** |
||
| 995 | * Add LDAP user by DN to LDAP group. |
||
| 996 | * |
||
| 997 | * @param string $userDn |
||
| 998 | * @param string $groupDn |
||
| 999 | * @throws Exception |
||
| 1000 | */ |
||
| 1001 | public function addLDAPUserToGroup($userDn, $groupDn) |
||
| 1002 | { |
||
| 1003 | $members = $this->getLDAPGroupMembers($groupDn); |
||
| 1004 | |||
| 1005 | // this user is already in the group, no need to do anything. |
||
| 1006 | if (in_array($userDn, $members)) { |
||
| 1007 | return; |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | $members[] = $userDn; |
||
| 1011 | |||
| 1012 | try { |
||
| 1013 | $this->update($groupDn, ['member' => $members]); |
||
| 1014 | } catch (Exception $e) { |
||
| 1015 | throw new ValidationException('LDAP group membership add failure: ' . $e->getMessage()); |
||
| 1016 | } |
||
| 1017 | } |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * Change a members password on the AD. Works with ActiveDirectory compatible services that saves the |
||
| 1021 | * password in the `unicodePwd` attribute. |
||
| 1022 | * |
||
| 1023 | * @todo Use the Zend\Ldap\Attribute::setPassword functionality to create a password in |
||
| 1024 | * an abstract way, so it works on other LDAP directories, not just Active Directory. |
||
| 1025 | * |
||
| 1026 | * Ensure that the LDAP bind:ed user can change passwords and that the connection is secure. |
||
| 1027 | * |
||
| 1028 | * @param Member $member |
||
| 1029 | * @param string $password |
||
| 1030 | * @param string|null $oldPassword Supply old password to perform a password change (as opposed to password reset) |
||
| 1031 | * @return ValidationResult |
||
| 1032 | */ |
||
| 1033 | public function setPassword(Member $member, $password, $oldPassword = null) |
||
| 1034 | { |
||
| 1035 | $validationResult = ValidationResult::create(); |
||
| 1036 | |||
| 1037 | $this->extend('onBeforeSetPassword', $member, $password, $validationResult); |
||
| 1038 | |||
| 1039 | if (!$member->GUID) { |
||
| 1040 | $this->getLogger()->warning(sprintf('Cannot update Member ID %s, GUID not set', $member->ID)); |
||
| 1041 | $validationResult->addError( |
||
| 1042 | _t( |
||
| 1043 | 'SilverStripe\\LDAP\\Authenticators\\LDAPAuthenticator.NOUSER', |
||
| 1044 | 'Your account hasn\'t been setup properly, please contact an administrator.' |
||
| 1045 | ) |
||
| 1046 | ); |
||
| 1047 | return $validationResult; |
||
| 1048 | } |
||
| 1049 | |||
| 1050 | $userData = $this->getUserByGUID($member->GUID); |
||
| 1051 | if (empty($userData['distinguishedname'])) { |
||
| 1052 | $validationResult->addError( |
||
| 1053 | _t( |
||
| 1054 | 'SilverStripe\\LDAP\\Authenticators\\LDAPAuthenticator.NOUSER', |
||
| 1055 | 'Your account hasn\'t been setup properly, please contact an administrator.' |
||
| 1056 | ) |
||
| 1057 | ); |
||
| 1058 | return $validationResult; |
||
| 1059 | } |
||
| 1060 | |||
| 1061 | try { |
||
| 1062 | if (!empty($oldPassword)) { |
||
| 1063 | $this->gateway->changePassword($userData['distinguishedname'], $password, $oldPassword); |
||
| 1064 | } elseif ($this->config()->password_history_workaround) { |
||
| 1065 | $this->passwordHistoryWorkaround($userData['distinguishedname'], $password); |
||
| 1066 | } else { |
||
| 1067 | $this->gateway->resetPassword($userData['distinguishedname'], $password); |
||
| 1068 | } |
||
| 1069 | $this->extend('onAfterSetPassword', $member, $password, $validationResult); |
||
| 1070 | } catch (Exception $e) { |
||
| 1071 | $validationResult->addError($e->getMessage()); |
||
| 1072 | } |
||
| 1073 | |||
| 1074 | return $validationResult; |
||
| 1075 | } |
||
| 1076 | |||
| 1077 | /** |
||
| 1078 | * Delete an LDAP user mapped to the Member record |
||
| 1079 | * @param Member $member |
||
| 1080 | * @throws ValidationException |
||
| 1081 | */ |
||
| 1082 | public function deleteLDAPMember(Member $member) |
||
| 1083 | { |
||
| 1084 | if (!$this->enabled()) { |
||
| 1085 | return; |
||
| 1086 | } |
||
| 1087 | if (!$member->GUID) { |
||
| 1088 | throw new ValidationException('Member missing GUID. Cannot delete LDAP user'); |
||
| 1089 | } |
||
| 1090 | $data = $this->getUserByGUID($member->GUID); |
||
| 1091 | if (empty($data['distinguishedname'])) { |
||
| 1092 | throw new ValidationException('LDAP delete failure: could not find distinguishedname attribute'); |
||
| 1093 | } |
||
| 1094 | |||
| 1095 | try { |
||
| 1096 | $this->delete($data['distinguishedname']); |
||
| 1097 | } catch (Exception $e) { |
||
| 1098 | throw new ValidationException('LDAP delete user failed: ' . $e->getMessage()); |
||
| 1099 | } |
||
| 1100 | } |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * A simple proxy to LDAP update operation. |
||
| 1104 | * |
||
| 1105 | * @param string $dn Location to add the entry at. |
||
| 1106 | * @param array $attributes A simple associative array of attributes. |
||
| 1107 | */ |
||
| 1108 | public function update($dn, array $attributes) |
||
| 1109 | { |
||
| 1110 | $this->gateway->update($dn, $attributes); |
||
| 1111 | } |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * A simple proxy to LDAP delete operation. |
||
| 1115 | * |
||
| 1116 | * @param string $dn Location of object to delete |
||
| 1117 | * @param bool $recursively Recursively delete nested objects? |
||
| 1118 | */ |
||
| 1119 | public function delete($dn, $recursively = false) |
||
| 1120 | { |
||
| 1121 | $this->gateway->delete($dn, $recursively); |
||
| 1122 | } |
||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * A simple proxy to LDAP copy/delete operation. |
||
| 1126 | * |
||
| 1127 | * @param string $fromDn |
||
| 1128 | * @param string $toDn |
||
| 1129 | * @param bool $recursively Recursively move nested objects? |
||
| 1130 | */ |
||
| 1131 | public function move($fromDn, $toDn, $recursively = false) |
||
| 1132 | { |
||
| 1133 | $this->gateway->move($fromDn, $toDn, $recursively); |
||
| 1134 | } |
||
| 1135 | |||
| 1136 | /** |
||
| 1137 | * A simple proxy to LDAP add operation. |
||
| 1138 | * |
||
| 1139 | * @param string $dn Location to add the entry at. |
||
| 1140 | * @param array $attributes A simple associative array of attributes. |
||
| 1141 | */ |
||
| 1142 | public function add($dn, array $attributes) |
||
| 1143 | { |
||
| 1144 | $this->gateway->add($dn, $attributes); |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | /** |
||
| 1148 | * @param string $dn Distinguished name of the user |
||
| 1149 | * @param string $password New password. |
||
| 1150 | * @throws Exception |
||
| 1151 | */ |
||
| 1152 | private function passwordHistoryWorkaround($dn, $password) |
||
| 1159 | } |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * Get a logger |
||
| 1163 | * |
||
| 1164 | * @return LoggerInterface |
||
| 1165 | */ |
||
| 1166 | public function getLogger() |
||
| 1169 | } |
||
| 1170 | } |
||
| 1171 |
This check marks private properties in classes that are never used. Those properties can be removed.