createExplicitUsers.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-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 = 'Users';
  19. $ouDN = 'ou=' . $ouName . ',' . $bdn;
  20. //creates on 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. $users = ['alice', 'boris', 'cynthia', 'derek', 'evelina', 'fatima', 'gregor'];
  32. foreach ($users as $uid) {
  33. $newDN = 'uid=' . $uid . ',' . $ouDN;
  34. $fn = ucfirst($uid);
  35. $sn = ucfirst(str_shuffle($uid)); // not so explicit but it's OK.
  36. $entry = [];
  37. $entry['cn'] = $fn . ' ' . $sn;
  38. $entry['objectclass'][] = 'inetOrgPerson';
  39. $entry['objectclass'][] = 'person';
  40. $entry['sn'] = $sn;
  41. $entry['userPassword'] = $uid;
  42. $entry['displayName'] = $sn . ', ' . $fn;
  43. $entry['mail'] = $fn . '@example.com';
  44. $ok = ldap_add($cr, $newDN, $entry);
  45. if ($ok) {
  46. echo('created user ' . ': ' . $entry['cn'] . PHP_EOL);
  47. } else {
  48. die(ldap_error($cr));
  49. }
  50. }