createExplicitGroupsDifferentOU.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. if(php_sapi_name() !== 'cli') {
  24. print('Only via CLI, please.');
  25. exit(1);
  26. }
  27. include __DIR__ . '/config.php';
  28. $cr = ldap_connect($host, $port);
  29. ldap_set_option($cr, LDAP_OPT_PROTOCOL_VERSION, 3);
  30. $ok = ldap_bind($cr, $adn, $apwd);
  31. if (!$ok) {
  32. die(ldap_error($cr));
  33. }
  34. $ouName = 'SpecialGroups';
  35. $ouDN = 'ou=' . $ouName . ',' . $bdn;
  36. //creates an OU
  37. if (true) {
  38. $entry = [];
  39. $entry['objectclass'][] = 'top';
  40. $entry['objectclass'][] = 'organizationalunit';
  41. $entry['ou'] = $ouName;
  42. $b = ldap_add($cr, $ouDN, $entry);
  43. if (!$b) {
  44. die(ldap_error($cr));
  45. }
  46. }
  47. $groups = ['SquareGroup', 'CircleGroup', 'TriangleGroup', 'SquaredCircleGroup'];
  48. // groupOfNames requires groups to have at least one member
  49. // the member used is created by createExplicitUsers.php script
  50. $omniMember = 'uid=alice,ou=Users,' . $bdn;
  51. foreach ($groups as $cn) {
  52. $newDN = 'cn=' . $cn . ',' . $ouDN;
  53. $entry = [];
  54. $entry['cn'] = $cn;
  55. $entry['objectclass'][] = 'groupOfNames';
  56. $entry['member'][] = $omniMember;
  57. $ok = ldap_add($cr, $newDN, $entry);
  58. if ($ok) {
  59. echo('created group ' . ': ' . $entry['cn'] . PHP_EOL);
  60. } else {
  61. die(ldap_error($cr));
  62. }
  63. }