createExplicitGroupsDifferentOU.php 2.0 KB

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