createExplicitUsers.php 2.0 KB

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