-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphabricator-sync-ldap.php
executable file
·133 lines (102 loc) · 4.1 KB
/
phabricator-sync-ldap.php
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/php
<?php
assert_options(ASSERT_BAIL, 1);
define("ROOT", dirname(__FILE__));
define("BASENAME", basename(__FILE__, ".php"));
// LOAD PHABRICATOR
define("PHABRICATOR_ROOT", ROOT . "/../phabricator");
require_once PHABRICATOR_ROOT . "/scripts/__init_script__.php";
// LOAD UTILITY FUNCTIONS
require_once ROOT . "/" . BASENAME . ".inc.php";
// LOAD CONFIG
require_once ROOT . "/" . BASENAME . ".cfg";
assert(LDAP_BASE !== null);
assert(LDAP_USER_SUBTREE !== null);
assert(LDAP_GROUP_SUBTREE !== null);
assert(LDAP_USER_FILTER !== null);
assert(LDAP_USER_NAME_ATTR !== null);
assert(LDAP_USER_MAIL_ATTR !== null);
assert(LDAP_USER_GROUP_ATTR !== null);
assert(LDAP_USER_GROUP_TRGT !== null);
assert(LDAP_GROUP_FILTER !== null);
assert(LDAP_GROUP_NAME_ATTR !== null);
assert(LDAP_GROUP_MEMBER_ATTR !== null);
assert(LDAP_GROUP_MEMBER_TRGT !== null);
assert(PHAB_ADMIN_USERNAME !== null);
assert(PHAB_PROJECT_LDAP_DN_FIELD !== null);
assert(PHAB_USER_LDAP_DN_FIELD !== null);
assert(PHAB_USER_ACCOUNT_TYPE !== null);
// With PHP7 this variable can be changed to a constant
assert($PHAB_PROTECTED_USERS !== null);
// LOAD CONNECTION INFO FROM ENVIRONMENT
$ldap_uri = getenv('LDAP_URI');
assert($ldap_uri !== false);
$ldap_binddn = getenv('LDAP_BINDDN');
$ldap_bindpw = getenv('LDAP_BINDPW');
assert(($ldap_binddn === false && $ldap_bindpw !== false) || ($ldap_binddn !== false && $ldap_bindpw !== false));
if (DRY_RUN) {
debug(">>> DRY RUN <<<\n");
}
// LDAP
if ($ldap_binddn === false && $ldap_bindpw === false) {
debug("Attempting anonymous bind ...\n");
$ldap_binddn = NULL;
$ldap_bindpw = NULL;
}
$ld = array(
"connection" => create_ldap_connection($ldap_uri, $ldap_binddn, $ldap_bindpw),
"base_dn" => LDAP_BASE,
"user" => array(
"search_base" => LDAP_BASE ? LDAP_USER_SUBTREE . ',' . LDAP_BASE : LDAP_USER_SUBTREE,
"filter" => LDAP_USER_FILTER,
"name_attr" => LDAP_USER_NAME_ATTR,
),
"group" => array(
"search_base" => LDAP_BASE ? LDAP_GROUP_SUBTREE . ',' . LDAP_BASE : LDAP_GROUP_SUBTREE,
"filter" => LDAP_GROUP_FILTER,
"name_attr" => LDAP_GROUP_NAME_ATTR,
"member_attr" => LDAP_GROUP_MEMBER_ATTR,
),
);
$ldap_users = query_ldap($ld["connection"], $ld["user"]["search_base"], $ld["user"]["filter"]);
$ldap_users = associate_ldap_list_with_dn($ldap_users);
$ldap_groups = query_ldap($ld["connection"], $ld["group"]["search_base"], $ld["group"]["filter"]);
$ldap_groups = associate_ldap_list_with_dn($ldap_groups);
// PHABRICATOR
$phab_admin = id(new PhabricatorPeopleQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withUsernames(array(PHAB_ADMIN_USERNAME))
->executeOne();
assert($phab_admin !== null);
$phab = array(
"admin" => $phab_admin,
"project_ldap_dn_field" => PHAB_PROJECT_LDAP_DN_FIELD,
"user_ldap_dn_field" => PHAB_USER_LDAP_DN_FIELD,
"user_account_type" => PHAB_USER_ACCOUNT_TYPE,
// With PHP7 this variable can be changed to a constant
"protected_users" => $PHAB_PROTECTED_USERS,
);
$phab_projects = id(new PhabricatorProjectQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->needMembers(true)
->execute();
$phab_projects = mpull($phab_projects, null, "getPHID");
$phab_accounts = id(new PhabricatorExternalAccountQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withAccountTypes(array(PHAB_USER_ACCOUNT_TYPE))
->execute();
// FIXME: Useful at least for creating debug output! Should be provided to the update_* functions!
$phab_user_account_map = mpull($phab_accounts, null, "getuserPHID");
$user_map = map_users($phab_accounts, $ld);
$project_map = map_projects($phab_projects, $phab);
update_phab_users($user_map, $ldap_users, $ld, $phab);
update_phab_projects($project_map, $ldap_groups, $ld, $phab);
$phab_projects = id(new PhabricatorProjectQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->needMembers(true)
->execute();
$phab_projects = mpull($phab_projects, null, "getPHID");
// Recreate user/project maps and ignore those missing in LDAP
$user_map = map_users($phab_accounts, $ld, false);
$project_map = map_projects($phab_projects, $phab);
update_phab_project_members($project_map, $user_map, $phab_projects, $ldap_groups, $ld, $phab);