1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Collection of functions related to removing user data.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
*
|
10
|
*/
|
11
|
function ldap_severs_user_data_setup_batch($consumer_type = NULL) {
|
12
|
|
13
|
$max_uid = db_query("SELECT max(uid) FROM {users}")->fetchField();
|
14
|
$step = 100;
|
15
|
$operations = [];
|
16
|
for ($uid = 2; $uid <= $max_uid; $uid += $step) {
|
17
|
$operations[] = ["ldap_servers_empty_user_data", [$uid, $step, $consumer_type]];
|
18
|
}
|
19
|
|
20
|
// Put all that information into our batch array.
|
21
|
return [
|
22
|
'operations' => $operations,
|
23
|
'title' => t('Empty LDAP Authorization Data in user->data[ldap_authorizations][%consumer_id]', ['%consumer_id' => $consumer_type]),
|
24
|
'init_message' => t('Initializing'),
|
25
|
'error_message' => t('An error occurred'),
|
26
|
'finished' => t('Finished.'),
|
27
|
];
|
28
|
|
29
|
}
|
30
|
|
31
|
/**
|
32
|
* Function to remove $user->data['ldap_authorizations'] on uninstall
|
33
|
* which is called from ldap_authorization uninstall batches.
|
34
|
*/
|
35
|
function ldap_servers_empty_user_data($start, $step, $consumer_type, &$context) {
|
36
|
|
37
|
$query = new EntityFieldQuery();
|
38
|
$query->entityCondition('entity_type', 'user')
|
39
|
->entityCondition('entity_id', [$start, $start + $step - 1], 'BETWEEN');
|
40
|
$results = $query->execute();
|
41
|
|
42
|
if (isset($results['user'])) {
|
43
|
foreach ($results['user'] as $uid => $entity_data) {
|
44
|
if ($uid > 1 && $account = user_load($uid, TRUE)) {
|
45
|
// Remove all authorization data.
|
46
|
if ($consumer_type == NULL && isset($account->data['ldap_authorizations'])) {
|
47
|
$names[] = $account->name;
|
48
|
unset($account->data['ldap_authorizations']);
|
49
|
$updated_account = user_save($account, ['data' => $account->data]);
|
50
|
}
|
51
|
// Remove only a particular consumers authorization data.
|
52
|
elseif ($consumer_type != NULL && isset($account->data['ldap_authorizations'][$consumer_type])) {
|
53
|
unset($account->data['ldap_authorizations'][$consumer_type]);
|
54
|
$updated_account = user_save($account, ['data' => $account->data]);
|
55
|
}
|
56
|
}
|
57
|
}
|
58
|
}
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
*
|
63
|
*/
|
64
|
function ldap_authorization_generate_users() {
|
65
|
$response = "";
|
66
|
for ($i = 1; $i < 1000; $i++) {
|
67
|
$name = "user" . $i;
|
68
|
if ($account = user_load_by_name($name)) {
|
69
|
user_delete($account->uid);
|
70
|
}
|
71
|
$account = new stdClass();
|
72
|
$account->is_new = TRUE;
|
73
|
$account->name = "user" . $i;
|
74
|
$user_edit = [
|
75
|
'data' => ['ldap_authorizations' => ['og_group' => 7, 'drupal_role' => 8]],
|
76
|
];
|
77
|
|
78
|
$user_response = user_save($account, $user_edit);
|
79
|
$response .= $user_response->name . "<br/>";
|
80
|
}
|
81
|
return $response;
|
82
|
|
83
|
}
|