createExplicitUsers.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 = 'Users';
  35. $ouDN = 'ou=' . $ouName . ',' . $bdn;
  36. //creates on 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. $users = ['alice', 'boris', 'cynthia', 'derek', 'evelina', 'fatima', 'gregor'];
  48. foreach ($users as $uid) {
  49. $newDN = 'uid=' . $uid . ',' . $ouDN;
  50. $fn = ucfirst($uid);
  51. $sn = ucfirst(str_shuffle($uid)); // not so explicit but it's OK.
  52. $entry = [];
  53. $entry['cn'] = $fn . ' ' . $sn;
  54. $entry['objectclass'][] = 'inetOrgPerson';
  55. $entry['objectclass'][] = 'person';
  56. $entry['sn'] = $sn;
  57. $entry['userPassword'] = $uid;
  58. $entry['displayName'] = $sn . ', ' . $fn;
  59. $entry['mail'] = $fn . '@example.com';
  60. $ok = ldap_add($cr, $newDN, $entry);
  61. if ($ok) {
  62. echo('created user ' . ': ' . $entry['cn'] . PHP_EOL);
  63. } else {
  64. die(ldap_error($cr));
  65. }
  66. }