createExplicitGroupsDifferentOU.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. if (php_sapi_name() !== 'cli') {
  8. print('Only via CLI, please.');
  9. exit(1);
  10. }
  11. include __DIR__ . '/config.php';
  12. $cr = ldap_connect($host, $port);
  13. ldap_set_option($cr, LDAP_OPT_PROTOCOL_VERSION, 3);
  14. $ok = ldap_bind($cr, $adn, $apwd);
  15. if (!$ok) {
  16. die(ldap_error($cr));
  17. }
  18. $ouName = 'SpecialGroups';
  19. $ouDN = 'ou=' . $ouName . ',' . $bdn;
  20. //creates an OU
  21. if (true) {
  22. $entry = [];
  23. $entry['objectclass'][] = 'top';
  24. $entry['objectclass'][] = 'organizationalunit';
  25. $entry['ou'] = $ouName;
  26. $b = ldap_add($cr, $ouDN, $entry);
  27. if (!$b) {
  28. die(ldap_error($cr));
  29. }
  30. }
  31. $groups = ['SquareGroup', 'CircleGroup', 'TriangleGroup', 'SquaredCircleGroup'];
  32. // groupOfNames requires groups to have at least one member
  33. // the member used is created by createExplicitUsers.php script
  34. $omniMember = 'uid=alice,ou=Users,' . $bdn;
  35. foreach ($groups as $cn) {
  36. $newDN = 'cn=' . $cn . ',' . $ouDN;
  37. $entry = [];
  38. $entry['cn'] = $cn;
  39. $entry['objectclass'][] = 'groupOfNames';
  40. $entry['member'][] = $omniMember;
  41. $ok = ldap_add($cr, $newDN, $entry);
  42. if ($ok) {
  43. echo('created group ' . ': ' . $entry['cn'] . PHP_EOL);
  44. } else {
  45. die(ldap_error($cr));
  46. }
  47. }