function ldap_user_ws in Lightweight Directory Access Protocol (LDAP) 7.2
Same name and namespace in other branches
- 8.2 ldap_user/ldap_user.ws.inc \ldap_user_ws()
File
- ldap_user/
ldap_user.ws.inc, line 36 - Functions related to REST webservices for LDAP User module.
Code
function ldap_user_ws($action, $direction_tag, $drupal_user_name_or_dn, $key) {
$action = check_plain($action);
$key = urldecode($key);
if (!$ldap_user_conf->wsEnabled) {
return ldap_user_ws_out([
0,
t('Webservice Not Enabled'),
]);
}
elseif ($key != $ldap_user_conf->wsKey) {
return ldap_user_ws_out([
0,
t('Bad Webservice Key'),
]);
}
elseif (!in_array($_SERVER['REMOTE_ADDR'], array_values($ldap_user_conf->wsUserIps))) {
return ldap_user_ws_out([
0,
t('Request from non-allowed IP Address'),
]);
}
if ($direction_tag == 'todrupal') {
$direction = LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER;
$sid = $ldap_user_conf->drupalAcctProvisionServer;
$ldap_server = ldap_servers_get_servers($sid, NULL, TRUE);
}
if ($direction_tag == 'toldap') {
$direction = LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY;
$sid = $ldap_user_conf->ldapEntryProvisionServer;
$ldap_server = ldap_servers_get_servers($sid, NULL, TRUE);
}
else {
$direction = LDAP_USER_PROV_DIRECTION_NONE;
$sid = LDAP_USER_NO_SERVER_SID;
$ldap_server = FALSE;
}
if (strpos($drupal_user_name_or_dn, '=') === FALSE) {
$drupal_user_name = check_plain($drupal_user_name_or_dn);
}
else {
$drupal_user_name = $ldap_server ? $ldap_server
->userUsernameFromDn($drupal_user_name_or_dn) : FALSE;
}
ldap_servers_module_load_include('php', 'ldap_user', 'LdapUserConfAdmin.class');
$ldap_user_conf = new LdapUserConf();
$drupal_user = $action == 'create' || $drupal_user_name === FALSE ? FALSE : user_load_by_name($drupal_user_name);
$user_edit = [];
$account = [];
switch ($action) {
case 'create':
if ($direction = LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER) {
$user_edit['name'] = $drupal_user_name;
$new_account = $ldap_user_conf
->provisionDrupalAccount($account, $user_edit, $ldap_user, TRUE);
// @todo return boolean on first line, not human readable message
$text = $new_account ? 'Created Account ' . $drupal_user_name : 'Fails to Create Account ' . $drupal_user_name;
return ldap_user_ws_out([
(bool) $new_account,
$text,
]);
}
elseif ($direction = LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY) {
// No need for ldap_user_ldap_provision_semaphore call with webservice since not tied to single user like logon process.
$provision_result = $ldap_user_conf
->provisionLdapEntry($drupal_user_name);
// @todo turn result array into response
}
break;
case 'synch':
if ($direction = LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER) {
$saved_account = $ldap_user_conf
->synchToDrupalAccount($drupal_user, $user_edit, LDAP_USER_EVENT_SYNCH_TO_DRUPAL_USER, $ldap_user, TRUE);
$text = $saved_account ? 'Updated Account ' . $drupal_user_name : 'Failed to Update Account ' . $drupal_user_name;
return ldap_user_ws_out([
(bool) $saved_account,
$text,
]);
}
elseif ($direction = LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY) {
$boolean_result = $ldap_user_conf
->synchToLdapEntry($drupal_user_name);
// @todo turn result array into response
}
break;
case 'disable':
if ($direction = LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER) {
$drupal_user->status = 0;
$edit = [
'status' => 0,
];
$saved_account = user_save($drupal_user, [
'status' => 0,
]);
return ldap_user_ws_out([
(bool) $saved_account,
'Disabled Account ' . $drupal_user_name,
]);
}
break;
case 'delete':
if ($direction = LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER) {
user_delete($drupal_user->uid);
return ldap_user_ws_out([
1,
'Deleted Account ' . $drupal_user_name,
]);
}
elseif ($direction = LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY) {
// @todo implement delete ldap record and call ldap_user event handler for delete account
}
break;
}
return $out;
}