-
Notifications
You must be signed in to change notification settings - Fork 0
/
civimrf_user_sync.module
281 lines (256 loc) · 9.05 KB
/
civimrf_user_sync.module
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* Implements hook_menu().
*/
function civimrf_user_sync_menu() {
$items['admin/config/civimrf/civimrf_user_sync'] = array(
'title' => 'CiviCRM User Synchronisation',
'description' => 'Edit the synchronisation settings for CiviCRM User Synchronisation.',
'page callback' => 'drupal_get_form',
'page arguments' => array('civimrf_user_sync_admin_form'),
'access arguments' => array('administer cmrf'),
'file' => 'civimrf_user_sync.admin.inc',
'weight' => 0,
);
return $items;
}
function civimrf_user_sync_cron() {
$settings = civimrf_user_sync_get_settings();
$queue = DrupalQueue::get('civimrf_user_sync');
if ($queue->numberOfItems() > 0) {
return; // dont add new items if queue is still running
}
$parameters = json_decode($settings['api_parameters'], true);
$options = array();
$options['limit'] = 0;
$call = civimrf_user_sync_send_call($settings['api_entity'], $settings['api_count_action'], $parameters, $options, $settings['profile']);
$userCount = $call->getReply();
if (isset($userCount['is_error']) && $userCount['is_error']) {
watchdog('error', 'Could not synchronize users');
return;
}
// Count could be stored ether in result or in count
$count = 0;
if (isset($userCount['result'])) {
$count = $userCount['result'];
} elseif (isset($userCount['count'])) {
$count = $userCount['count'];
}
for($i=0; $i<$count; $i = $i + $settings['api_limit']) {
$queue->createItem(array(
'offset' => $i,
'limit' => $settings['api_limit'],
));
}
}
function civimrf_user_sync_cron_queue_info() {
$queues['civimrf_user_sync'] = array(
'worker callback' => 'civimrf_user_sync_queue_callback',
'time' => 30,
'civimrf user sync finish callback' => 'civimrf_user_sync_finished'
);
return $queues;
}
/**
* Execute the API to retrieve a list of users and synchronize those.
*/
function civimrf_user_sync_queue_callback($data) {
$queue = DrupalQueue::get('civimrf_user_sync');
$settings = civimrf_user_sync_get_settings();
$parameters = json_decode($settings['api_parameters'], true);
$options = array();
if ($settings['cache']) {
$options['cache'] = $settings['cache'];
}
$options['offset'] = $data['offset'];
$options['limit'] = $data['limit'];
$call = civimrf_user_sync_send_call($settings['api_entity'], $settings['api_action'], $parameters, $options, $settings['profile']);
$contacts = $call->getReply();
if (isset($contacts['is_error']) && $contacts['is_error']) {
watchdog('error', 'Could not synchronize users');
return;
}
if (!isset($contacts['values']) || !is_array($contacts['values'])) {
watchdog('error', 'Could not synchronize users');
return;
}
$saved_uids = array();
foreach($contacts['values'] as $contact) {
$user = civimrf_user_sync_syncuser($contact);
if ($user) {
$saved_uids[] = $user->field_civimrf_user_sync_uid['und'][0]['value'];
}
}
$queue->addProcessedUIDs($saved_uids);
}
/**
* Syncs a user to an existing or new user.
*/
function civimrf_user_sync_syncuser($contact) {
$settings = civimrf_user_sync_get_settings();
$uid = $contact[$settings['uid_attribute']];
$username = $contact[$settings['username_attribute']];
$email = $contact[$settings['email_attribute']];
if (empty($username) && empty($email)) {
return; // both username and email are empty so no information to create an account.
} elseif (empty($username) && !empty($email)) {
$username = $email;
}
$account = civimrf_user_sync_load_user($uid, $username, $email);
if ($account) {
$edit = array();
$edit['name'] = $username;
$edit['mail'] = $email;
$edit['status'] = 1;
$edit['field_civimrf_user_sync_uid']['und'][0]['value'] = $uid;
$edit['roles'] = $settings['roles'];
drupal_alter('civimrf_user_update', $edit, $contact);
return user_save($account, $edit);
} else {
$edit = array();
$edit['name'] = $username;
$edit['mail'] = $email;
$edit['status'] = 1;
$edit['field_civimrf_user_sync_uid']['und'][0]['value'] = $uid;
$edit['init'] = $email;
$edit['pass'] = user_password();
$edit['roles'] = $settings['roles'];
drupal_alter('civimrf_user_new', $edit, $contact);
$account = user_save(null, $edit);
$params['account'] = $account;
$language = user_preferred_language($account);
drupal_mail('user', 'register_admin_created', $account->mail, $language, $params);
return $account;
}
return false;
}
/**
* Callback for when the synchronisation is finished.
* We delete all users who arent synchronised.
*/
function civimrf_user_sync_finished($uids) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'user', '=');
$query->fieldCondition('field_civimrf_user_sync_uid', 'value', 0, '>');
if (count($uids)) {
$query->fieldCondition('field_civimrf_user_sync_uid', 'value', $uids, 'NOT IN');
}
$result = $query->execute();
if (isset($result['user'])) {
foreach($result['user'] as $user) {
if ($user->uid == 1) {
continue; // Don't delete user 1 as that one is an admin user.
}
$account = user_load($user->uid);
user_delete($account->uid);
$params['account'] = $account;
$language = user_preferred_language($account);
drupal_mail('user', 'status_canceled', $account->mail, $language, $params);
}
}
}
/**
* Load a user by its uid or its username.
*/
function civimrf_user_sync_load_user($uid, $username, $email) {
// First lookup the user by the uid.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'user', '=')
->fieldCondition('field_civimrf_user_sync_uid', 'value', $uid, '=');
$result = $query->execute();
if (isset($result['user'])) {
foreach($result['user'] as $user) {
return user_load($user->uid);
}
}
// If the user is not found see whether we can find it by its username.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'user', '=')
->propertyCondition('name', $username);
$result = $query->execute();
if (isset($result['user'])) {
foreach($result['user'] as $user) {
return user_load($user->uid);
}
}
// If the user is not found see whether we can find it by its email.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'user', '=')
->propertyCondition('mail', $email);
$result = $query->execute();
if (isset($result['user'])) {
foreach($result['user'] as $user) {
return user_load($user->uid);
}
}
// User not found.
return null;
}
function civimrf_user_sync_get_connector_name($profile) {
return 'civimrf_user_sync_connector:'.$profile;
}
function civimrf_user_sync_get_connector_id($profile=null) {
$connector_name = civimrf_user_sync_get_connector_name($profile);
$core = cmrf_core_get_core();
$connector_id = variable_get($connector_name);
if (empty($connector_id)) {
$connector_id = $core->registerConnector($connector_name, $profile);
variable_set($connector_name, $connector_id);
}
return $connector_id;
}
/**
* Send a CiviMRF call
*/
function civimrf_user_sync_send_call($entity, $action, $parameters, $options, $profile) {
$connector_id = civimrf_user_sync_get_connector_id($profile);
$core = cmrf_core_get_core();
$call = $core->createCall($connector_id, $entity, $action, $parameters, $options);
$core->executeCall($call);
return $call;
}
function civimrf_user_sync_get_settings() {
return array(
'profile' => variable_get('civimrf_user_sync_profile', null),
'cache' => variable_get('civimrf_user_sync_cache', '60 seconds'),
'api_entity' => variable_get('civimrf_user_sync_api_entity', 'Contact'),
'api_action' => variable_get('civimrf_user_sync_api_action', 'get'),
'api_count_action' => variable_get('civimrf_user_sync_api_count_action', 'getcount'),
'api_parameters' => variable_get('civimrf_user_sync_api_parameters', '{"contact_sub_type": "Student", "is_deleted": "0", "is_deceased": "0"}'),
'api_limit' => variable_get('civimrf_user_sync_api_limit', '100'),
'uid_attribute' => variable_get('civimrf_user_sync_uid_attribute', 'id'),
'username_attribute' => variable_get('civimrf_user_sync_username_attribute', 'email'),
'email_attribute' => variable_get('civimrf_user_sync_email_attribute', 'email'),
'hide_username_field' => variable_get('civimrf_hide_username_field', true),
'hide_email_field' => variable_get('civimrf_hide_email_field', true),
'roles' => variable_get('civimrf_user_sync_roles', array(DRUPAL_AUTHENTICATED_RID)),
);
}
/**
* Hide the uid field from the form.
*/
function civimrf_user_sync_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
$settings = civimrf_user_sync_get_settings();
$current_user = user_uid_optional_load();
if (!in_array('administrator', $current_user->roles)) {
$form['field_civimrf_user_sync_uid']['#access'] = false;
}
if ($settings['hide_username_field']) {
$form['account']['name']['#access'] = false;
}
if ($settings['hide_email_field']) {
$form['account']['mail']['#access'] = false;
}
}
/**
* Hide the uid field from the user profile
*/
function civimrf_user_sync_preprocess_user_profile(&$vars) {
$current_user = user_uid_optional_load();
if (!in_array('administrator', $current_user->roles)) {
unset($vars['user_profile']['field_civimrf_user_sync_uid']);
}
}