aboutsummaryrefslogtreecommitdiff
path: root/linaro/import_ldap_user.php
blob: 9f9e411c35ad1d09fedaf77174c8ae288697463b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env php
<?php

$root = dirname(dirname(dirname(__FILE__)));
require_once $root.'/scripts/__init_script__.php';

if ($argc !== 5) {
  echo pht(
    "Usage: %s\n",
    'add_user.php <username> <email> <realname> <admin_user>');
  exit(1);
}

$username = $argv[1];
$email = $argv[2];
$realname = $argv[3];
$admin = $argv[4];

$admin = id(new PhabricatorUser())->loadOneWhere(
  'username = %s',
  $argv[4]);
if (!$admin) {
  throw new Exception(
    pht(
      'Admin user must be the username of a valid Phabricator account'));
}

$existing_user = id(new PhabricatorUser())->loadOneWhere(
  'username = %s',
  $username);
if ($existing_user) {
  throw new Exception(
    pht(
      "There is already a user with the username '%s'!",
      $username));
}

$existing_email = id(new PhabricatorUserEmail())->loadOneWhere(
  'address = %s',
  $email);
if ($existing_email) {
  throw new Exception(
    pht(
      "There is already a user with the email '%s'!",
      $email));
}

$user = new PhabricatorUser();
$user->setUsername($username);
$user->setRealname($realname);
$user->setIsApproved(1);

$email_object = id(new PhabricatorUserEmail())
  ->setAddress($email)
  ->setIsVerified(1);

id(new PhabricatorUserEditor())
  ->setActor($admin)
  ->createNewUser($user, $email_object);

id(new PhabricatorExternalAccount())
  ->setUserPHID($user->getPHID())
  ->setAccountType('ldap')
  ->setAccountDomain('self')
  ->setAccountID($username)
  ->save();


echo pht(
  "Created ldap user '%s' (realname='%s', email='%s').\n",
  $username,
  $realname,
  $email);