1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Contains FeedsUserProcessor.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Option to block users not found in the feed.
|
10
|
*
|
11
|
* @var string
|
12
|
*/
|
13
|
define('FEEDS_BLOCK_NON_EXISTENT', 'block');
|
14
|
|
15
|
/**
|
16
|
* Feeds processor plugin. Create users from feed items.
|
17
|
*/
|
18
|
class FeedsUserProcessor extends FeedsProcessor {
|
19
|
/**
|
20
|
* Search by role name.
|
21
|
*/
|
22
|
const ROLE_SEARCH_NAME = 'name';
|
23
|
|
24
|
/**
|
25
|
* Search by role id.
|
26
|
*/
|
27
|
const ROLE_SEARCH_RID = 'rid';
|
28
|
|
29
|
/**
|
30
|
* Unencrypted password.
|
31
|
*/
|
32
|
const PASS_UNENCRYPTED = 'none';
|
33
|
|
34
|
/**
|
35
|
* MD5 encrypted password.
|
36
|
*/
|
37
|
const PASS_MD5 = 'md5';
|
38
|
|
39
|
/**
|
40
|
* SHA512 encrypted password.
|
41
|
*/
|
42
|
const PASS_SHA512 = 'sha512';
|
43
|
|
44
|
/**
|
45
|
* Define entity type.
|
46
|
*/
|
47
|
public function entityType() {
|
48
|
return 'user';
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* Implements parent::entityInfo().
|
53
|
*/
|
54
|
protected function entityInfo() {
|
55
|
$info = parent::entityInfo();
|
56
|
$info['label plural'] = t('Users');
|
57
|
return $info;
|
58
|
}
|
59
|
|
60
|
/**
|
61
|
* Creates a new user account in memory and returns it.
|
62
|
*/
|
63
|
protected function newEntity(FeedsSource $source) {
|
64
|
$account = parent::newEntity($source);
|
65
|
$account->uid = 0;
|
66
|
$account->roles = array_filter($this->config['roles']);
|
67
|
$account->status = $this->config['status'];
|
68
|
$account->is_new = TRUE;
|
69
|
|
70
|
return $account;
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* Loads an existing user.
|
75
|
*/
|
76
|
protected function entityLoad(FeedsSource $source, $uid) {
|
77
|
$user = parent::entityLoad($source, $uid);
|
78
|
|
79
|
// Copy the password so that we can compare it again at save.
|
80
|
$user->feeds_original_pass = $user->pass;
|
81
|
|
82
|
// Reset roles and status when an user is replaced.
|
83
|
if ($this->config['update_existing'] == FEEDS_REPLACE_EXISTING) {
|
84
|
$user->roles = array_filter($this->config['roles']);
|
85
|
$user->status = $this->config['status'];
|
86
|
|
87
|
// Unserialize user data if it is still serialized.
|
88
|
if (!empty($user->data) && @unserialize($user->data)) {
|
89
|
$user->data = unserialize($user->data);
|
90
|
}
|
91
|
else {
|
92
|
$user->data = array();
|
93
|
}
|
94
|
}
|
95
|
|
96
|
return $user;
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* Validates a user account.
|
101
|
*/
|
102
|
protected function entityValidate($account, FeedsSource $source = NULL) {
|
103
|
parent::entityValidate($account);
|
104
|
|
105
|
if (empty($account->name) || empty($account->mail) || !valid_email_address($account->mail)) {
|
106
|
throw new FeedsValidationException(t('User name missing or email not valid.'));
|
107
|
}
|
108
|
|
109
|
// Check when an user ID gets set or changed during processing if that user
|
110
|
// ID is not already in use.
|
111
|
$uid_is_set = FALSE;
|
112
|
if (!empty($account->uid)) {
|
113
|
$is_new = !empty($account->feeds_item->is_new);
|
114
|
$different = !empty($account->feeds_item->entity_id) && $account->feeds_item->entity_id != $account->uid;
|
115
|
if ($is_new || $different) {
|
116
|
// Flag that an user ID is explicitly set. This will skip the
|
117
|
// validations for existing user name and mail address defined below.
|
118
|
$uid_is_set = TRUE;
|
119
|
|
120
|
// Check if the given user ID already exists.
|
121
|
$exists = entity_load_unchanged('user', $account->uid);
|
122
|
if ($exists) {
|
123
|
throw new FeedsValidationException(t('Could not update user ID to @uid since that ID is already in use.', array(
|
124
|
'@uid' => $account->uid,
|
125
|
)));
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
|
130
|
// Look for other validation constraints.
|
131
|
$errors = array();
|
132
|
|
133
|
// Validate the user name.
|
134
|
if ($error = user_validate_name($account->name)) {
|
135
|
$errors[] = $error;
|
136
|
}
|
137
|
|
138
|
// Check for an existing user name, unless an user ID is explicitly set.
|
139
|
if (!$uid_is_set && (bool) db_select('users')->fields('users', array('uid'))->condition('uid', $account->uid, '<>')->condition('name', db_like($account->name), 'LIKE')->range(0, 1)->execute()->fetchField()) {
|
140
|
$errors[] = t("The name '@name' is already taken.", array('@name' => $account->name));
|
141
|
}
|
142
|
|
143
|
// Check for an existing mail address, unless an user ID is explicitly set.
|
144
|
if (!$uid_is_set && (bool) db_select('users')->fields('users', array('uid'))->condition('uid', $account->uid, '<>')->condition('mail', db_like($account->mail), 'LIKE')->range(0, 1)->execute()->fetchField()) {
|
145
|
$errors[] = t("The e-mail address '@email' is already taken.", array('@email' => $account->mail));
|
146
|
}
|
147
|
|
148
|
// Timezone validation.
|
149
|
if (!empty($account->timezone) && !array_key_exists($account->timezone, system_time_zones())) {
|
150
|
$errors[] = t("Failed importing '@name'. User's timezone is not valid.", array('@name' => $account->name));
|
151
|
}
|
152
|
|
153
|
if (!empty($errors)) {
|
154
|
throw new FeedsValidationException(implode("\n", $errors));
|
155
|
}
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* Save a user account.
|
160
|
*/
|
161
|
protected function entitySave($account) {
|
162
|
if ($this->config['defuse_mail']) {
|
163
|
$account->mail = $account->mail . '_test';
|
164
|
}
|
165
|
|
166
|
$edit = (array) $account;
|
167
|
|
168
|
// Remove pass from $edit if the password is unchanged.
|
169
|
if (isset($account->feeds_original_pass) && $account->pass == $account->feeds_original_pass) {
|
170
|
unset($edit['pass']);
|
171
|
}
|
172
|
|
173
|
// Check if the user ID changed when updating users.
|
174
|
if (!empty($account->feeds_item->entity_id) && $account->feeds_item->entity_id != $account->uid) {
|
175
|
// The user ID of the existing user is different. Try to update the user ID.
|
176
|
db_update('users')
|
177
|
->fields(array(
|
178
|
'uid' => $account->uid,
|
179
|
))
|
180
|
->condition('uid', $account->feeds_item->entity_id)
|
181
|
->execute();
|
182
|
}
|
183
|
|
184
|
user_save($account, $edit);
|
185
|
|
186
|
// If an encrypted password was given, directly set this in the database.
|
187
|
if ($account->uid && !empty($account->pass_crypted)) {
|
188
|
db_update('users')
|
189
|
->fields(array('pass' => $account->pass_crypted))
|
190
|
->condition('uid', $account->uid)
|
191
|
->execute();
|
192
|
}
|
193
|
|
194
|
if ($account->uid && !empty($account->openid)) {
|
195
|
$authmap = array(
|
196
|
'uid' => $account->uid,
|
197
|
'module' => 'openid',
|
198
|
'authname' => $account->openid,
|
199
|
);
|
200
|
if (SAVED_UPDATED != drupal_write_record('authmap', $authmap, array('uid', 'module'))) {
|
201
|
drupal_write_record('authmap', $authmap);
|
202
|
}
|
203
|
}
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* Delete multiple user accounts.
|
208
|
*/
|
209
|
protected function entityDeleteMultiple($uids) {
|
210
|
// Prevent user 1 from being deleted.
|
211
|
if (in_array(1, $uids)) {
|
212
|
$uids = array_diff($uids, array(1));
|
213
|
|
214
|
// But do delete the associated feeds item.
|
215
|
db_delete('feeds_item')
|
216
|
->condition('entity_type', $this->entityType())
|
217
|
->condition('entity_id', 1)
|
218
|
->execute();
|
219
|
}
|
220
|
user_delete_multiple($uids);
|
221
|
}
|
222
|
|
223
|
/**
|
224
|
* Override parent::configDefaults().
|
225
|
*/
|
226
|
public function configDefaults() {
|
227
|
return array(
|
228
|
'roles' => array(),
|
229
|
'status' => 1,
|
230
|
'defuse_mail' => FALSE,
|
231
|
) + parent::configDefaults();
|
232
|
}
|
233
|
|
234
|
/**
|
235
|
* Override parent::configForm().
|
236
|
*/
|
237
|
public function configForm(&$form_state) {
|
238
|
$form = parent::configForm($form_state);
|
239
|
$form['status'] = array(
|
240
|
'#type' => 'radios',
|
241
|
'#title' => t('Status'),
|
242
|
'#description' => t('Select whether users should be imported active or blocked.'),
|
243
|
'#options' => array(0 => t('Blocked'), 1 => t('Active')),
|
244
|
'#default_value' => $this->config['status'],
|
245
|
);
|
246
|
|
247
|
$roles = user_roles(TRUE);
|
248
|
unset($roles[2]);
|
249
|
if (count($roles)) {
|
250
|
$form['roles'] = array(
|
251
|
'#type' => 'checkboxes',
|
252
|
'#title' => t('Additional roles'),
|
253
|
'#description' => t('Every user is assigned the "authenticated user" role. Select additional roles here.'),
|
254
|
'#default_value' => $this->config['roles'],
|
255
|
'#options' => $roles,
|
256
|
);
|
257
|
}
|
258
|
$form['defuse_mail'] = array(
|
259
|
'#type' => 'checkbox',
|
260
|
'#title' => t('Defuse e-mail addresses'),
|
261
|
'#description' => t('This appends _test to all imported e-mail addresses to ensure they cannot be used as recipients.'),
|
262
|
'#default_value' => $this->config['defuse_mail'],
|
263
|
);
|
264
|
$form['update_non_existent']['#options'][FEEDS_BLOCK_NON_EXISTENT] = t('Block non-existent users');
|
265
|
return $form;
|
266
|
}
|
267
|
|
268
|
/**
|
269
|
* Overrides FeedsProcessor::map().
|
270
|
*
|
271
|
* Ensures that the user is assigned additional roles that are configured on
|
272
|
* the settings. The roles could have been revoked when there was mapped to
|
273
|
* the "roles_list" target.
|
274
|
*/
|
275
|
protected function map(FeedsSource $source, FeedsParserResult $result, $target_item = NULL) {
|
276
|
$target_item = parent::map($source, $result, $target_item);
|
277
|
|
278
|
// Assign additional roles as configured.
|
279
|
$roles = array_filter($this->config['roles']);
|
280
|
foreach ($roles as $rid) {
|
281
|
$target_item->roles[$rid] = $rid;
|
282
|
}
|
283
|
|
284
|
return $target_item;
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* Overrides setTargetElement() to operate on a target item that is an user.
|
289
|
*/
|
290
|
public function setTargetElement(FeedsSource $source, $target_user, $target_element, $value, array $mapping = array()) {
|
291
|
switch ($target_element) {
|
292
|
case 'pass':
|
293
|
$this->setPassTarget($source, $target_user, $target_element, $value, $mapping);
|
294
|
break;
|
295
|
|
296
|
case 'created':
|
297
|
$target_user->created = feeds_to_unixtime($value, REQUEST_TIME);
|
298
|
break;
|
299
|
|
300
|
case 'language':
|
301
|
$target_user->language = strtolower($value);
|
302
|
break;
|
303
|
|
304
|
case 'roles_list':
|
305
|
// Ensure that the role list is an array.
|
306
|
$value = (array) $value;
|
307
|
$this->rolesListSetTarget($source, $target_user, $target_element, $value, $mapping);
|
308
|
break;
|
309
|
|
310
|
case 'timezone':
|
311
|
$target_user->timezone = $value;
|
312
|
break;
|
313
|
|
314
|
default:
|
315
|
parent::setTargetElement($source, $target_user, $target_element, $value);
|
316
|
break;
|
317
|
}
|
318
|
}
|
319
|
|
320
|
/**
|
321
|
* Return available mapping targets.
|
322
|
*/
|
323
|
public function getMappingTargets() {
|
324
|
$targets = parent::getMappingTargets();
|
325
|
$targets += array(
|
326
|
'uid' => array(
|
327
|
'name' => t('User ID'),
|
328
|
'description' => t('The uid of the user. NOTE: use this feature with care, user ids are usually assigned by Drupal.'),
|
329
|
'optional_unique' => TRUE,
|
330
|
),
|
331
|
'name' => array(
|
332
|
'name' => t('User name'),
|
333
|
'description' => t('Name of the user.'),
|
334
|
'optional_unique' => TRUE,
|
335
|
),
|
336
|
'mail' => array(
|
337
|
'name' => t('Email address'),
|
338
|
'description' => t('Email address of the user.'),
|
339
|
'optional_unique' => TRUE,
|
340
|
),
|
341
|
'created' => array(
|
342
|
'name' => t('Created date'),
|
343
|
'description' => t('The created (e. g. joined) data of the user.'),
|
344
|
),
|
345
|
'pass' => array(
|
346
|
'name' => t('Password'),
|
347
|
'description' => t('The user password.'),
|
348
|
'summary_callbacks' => array(array($this, 'passSummaryCallback')),
|
349
|
'form_callbacks' => array(array($this, 'passFormCallback')),
|
350
|
),
|
351
|
'status' => array(
|
352
|
'name' => t('Account status'),
|
353
|
'description' => t('Whether a user is active or not. 1 stands for active, 0 for blocked.'),
|
354
|
),
|
355
|
'language' => array(
|
356
|
'name' => t('User language'),
|
357
|
'description' => t('Default language for the user.'),
|
358
|
),
|
359
|
'timezone' => array(
|
360
|
'name' => t('Timezone'),
|
361
|
'description' => t('The timezone identifier, like UTC or Europe/Lisbon.'),
|
362
|
),
|
363
|
'roles_list' => array(
|
364
|
'name' => t('User roles'),
|
365
|
'description' => t('User roles provided as role names in comma separated list.'),
|
366
|
'summary_callbacks' => array(array($this, 'rolesListSummaryCallback')),
|
367
|
'form_callbacks' => array(array($this, 'rolesListFormCallback')),
|
368
|
),
|
369
|
);
|
370
|
if (module_exists('openid')) {
|
371
|
$targets['openid'] = array(
|
372
|
'name' => t('OpenID identifier'),
|
373
|
'description' => t('The OpenID identifier of the user. <strong>CAUTION:</strong> Use only for migration purposes, misconfiguration of the OpenID identifier can lead to severe security breaches like users gaining access to accounts other than their own.'),
|
374
|
'optional_unique' => TRUE,
|
375
|
);
|
376
|
}
|
377
|
|
378
|
$this->getHookTargets($targets);
|
379
|
|
380
|
return $targets;
|
381
|
}
|
382
|
|
383
|
/**
|
384
|
* Get id of an existing feed item term if available.
|
385
|
*/
|
386
|
protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
|
387
|
if ($uid = parent::existingEntityId($source, $result)) {
|
388
|
return $uid;
|
389
|
}
|
390
|
|
391
|
// Iterate through all unique targets and try to find a user for the
|
392
|
// target's value.
|
393
|
foreach ($this->uniqueTargets($source, $result) as $target => $value) {
|
394
|
switch ($target) {
|
395
|
case 'uid':
|
396
|
$uid = db_query("SELECT uid FROM {users} WHERE uid = :uid", array(':uid' => $value))->fetchField();
|
397
|
break;
|
398
|
|
399
|
case 'name':
|
400
|
$uid = db_query("SELECT uid FROM {users} WHERE name = :name", array(':name' => $value))->fetchField();
|
401
|
break;
|
402
|
|
403
|
case 'mail':
|
404
|
$uid = db_query("SELECT uid FROM {users} WHERE mail = :mail", array(':mail' => $value))->fetchField();
|
405
|
break;
|
406
|
|
407
|
case 'openid':
|
408
|
$uid = db_query("SELECT uid FROM {authmap} WHERE authname = :authname AND module = 'openid'", array(':authname' => $value))->fetchField();
|
409
|
break;
|
410
|
}
|
411
|
if ($uid) {
|
412
|
// Return with the first nid found.
|
413
|
return $uid;
|
414
|
}
|
415
|
}
|
416
|
return 0;
|
417
|
}
|
418
|
|
419
|
/**
|
420
|
* Overrides FeedsProcessor::initEntitiesToBeRemoved().
|
421
|
*
|
422
|
* Removes user 1 from the list of entities to be removed.
|
423
|
*/
|
424
|
protected function initEntitiesToBeRemoved(FeedsSource $source, FeedsState $state) {
|
425
|
parent::initEntitiesToBeRemoved($source, $state);
|
426
|
|
427
|
// Prevent user 1 from being deleted.
|
428
|
unset($state->removeList[1]);
|
429
|
}
|
430
|
|
431
|
/**
|
432
|
* Overrides FeedsProcessor::clean().
|
433
|
*
|
434
|
* Block users instead of deleting them.
|
435
|
*
|
436
|
* @param FeedsState $state
|
437
|
* The FeedsState object for the given stage.
|
438
|
*/
|
439
|
protected function clean(FeedsState $state) {
|
440
|
// Delegate to parent if not blocking or option not set.
|
441
|
if (!isset($this->config['update_non_existent']) || $this->config['update_non_existent'] !== FEEDS_BLOCK_NON_EXISTENT) {
|
442
|
return parent::clean($state);
|
443
|
}
|
444
|
|
445
|
if (!empty($state->removeList)) {
|
446
|
// @see user_user_operations_block().
|
447
|
// The following foreach is copied from above function but with an added
|
448
|
// counter to count blocked users.
|
449
|
foreach (user_load_multiple($state->removeList) as $account) {
|
450
|
$this->loadItemInfo($account);
|
451
|
$account->feeds_item->hash = $this->config['update_non_existent'];
|
452
|
// For efficiency manually save the original account before applying any
|
453
|
// changes.
|
454
|
$account->original = clone $account;
|
455
|
user_save($account, array('status' => 0));
|
456
|
$state->blocked++;
|
457
|
}
|
458
|
}
|
459
|
}
|
460
|
|
461
|
/**
|
462
|
* Returns default values for mapper "roles_list".
|
463
|
*/
|
464
|
public function rolesListDefaults() {
|
465
|
$roles = user_roles(TRUE);
|
466
|
unset($roles[DRUPAL_AUTHENTICATED_RID]);
|
467
|
$rids = array_keys($roles);
|
468
|
$rids = array_combine($rids, $rids);
|
469
|
return array(
|
470
|
'role_search' => self::ROLE_SEARCH_NAME,
|
471
|
'allowed_roles' => $rids,
|
472
|
'autocreate' => 0,
|
473
|
'revoke_roles' => 1,
|
474
|
);
|
475
|
}
|
476
|
|
477
|
/**
|
478
|
* Mapping configuration summary callback for target "roles_list".
|
479
|
*/
|
480
|
public function rolesListSummaryCallback(array $mapping, $target, array $form, array $form_state) {
|
481
|
$options = array();
|
482
|
|
483
|
// Add in defaults.
|
484
|
$defaults = $this->rolesListDefaults();
|
485
|
$mapping += $defaults;
|
486
|
$mapping['allowed_roles'] += $defaults['allowed_roles'];
|
487
|
|
488
|
// Role search.
|
489
|
$role_search_options = $this->rolesListRoleSearchOptions();
|
490
|
$options[] = t('Search roles by: <strong>@search</strong>', array('@search' => $role_search_options[$mapping['role_search']]));
|
491
|
|
492
|
// Allowed roles.
|
493
|
$role_names = array();
|
494
|
$roles = user_roles(TRUE);
|
495
|
foreach (array_filter($mapping['allowed_roles']) as $rid => $enabled) {
|
496
|
$role_names[] = $roles[$rid];
|
497
|
}
|
498
|
|
499
|
if (empty($role_names)) {
|
500
|
$role_names = array('<' . t('none') . '>');
|
501
|
}
|
502
|
$options[] = t('Allowed roles: %roles', array('%roles' => implode(', ', $role_names)));
|
503
|
|
504
|
// Autocreate.
|
505
|
if ($mapping['autocreate']) {
|
506
|
$options[] = t('Automatically create roles');
|
507
|
}
|
508
|
else {
|
509
|
$options[] = t('Only assign existing roles');
|
510
|
}
|
511
|
|
512
|
// Revoke roles.
|
513
|
if ($mapping['revoke_roles']) {
|
514
|
$options[] = t('Revoke roles: yes');
|
515
|
}
|
516
|
else {
|
517
|
$options[] = t('Revoke roles: no');
|
518
|
}
|
519
|
|
520
|
return implode('<br />', $options);
|
521
|
}
|
522
|
|
523
|
/**
|
524
|
* Mapping configuration form callback for target "roles_list".
|
525
|
*/
|
526
|
public function rolesListFormCallback(array $mapping, $target, array $form, array $form_state) {
|
527
|
// Add in defaults.
|
528
|
$defaults = $this->rolesListDefaults();
|
529
|
$mapping += $defaults;
|
530
|
$mapping['allowed_roles'] += $defaults['allowed_roles'];
|
531
|
|
532
|
$allowed_roles_options = user_roles(TRUE);
|
533
|
unset($allowed_roles_options[DRUPAL_AUTHENTICATED_RID]);
|
534
|
|
535
|
return array(
|
536
|
'role_search' => array(
|
537
|
'#type' => 'select',
|
538
|
'#title' => t('Search roles by'),
|
539
|
'#options' => $this->rolesListRoleSearchOptions(),
|
540
|
'#default_value' => $mapping['role_search'],
|
541
|
),
|
542
|
'allowed_roles' => array(
|
543
|
'#type' => 'checkboxes',
|
544
|
'#title' => t('Allowed roles'),
|
545
|
'#options' => $allowed_roles_options,
|
546
|
'#default_value' => $mapping['allowed_roles'],
|
547
|
'#description' => t('Select the roles to accept from the feed.<br />Any other roles will be ignored.'),
|
548
|
),
|
549
|
'autocreate' => array(
|
550
|
'#type' => 'checkbox',
|
551
|
'#title' => t('Auto create'),
|
552
|
'#description' => t("Create the role if it doesn't exist."),
|
553
|
'#default_value' => $mapping['autocreate'],
|
554
|
),
|
555
|
'revoke_roles' => array(
|
556
|
'#type' => 'checkbox',
|
557
|
'#title' => t('Revoke roles'),
|
558
|
'#description' => t('If enabled, roles that are not provided by the feed will be revoked for the user. This affects only the "Allowed roles" as configured above.'),
|
559
|
'#default_value' => $mapping['revoke_roles'],
|
560
|
),
|
561
|
);
|
562
|
}
|
563
|
|
564
|
/**
|
565
|
* Returns role list options.
|
566
|
*/
|
567
|
public function rolesListRoleSearchOptions() {
|
568
|
return array(
|
569
|
self::ROLE_SEARCH_NAME => t('Role name'),
|
570
|
self::ROLE_SEARCH_RID => t('Role ID'),
|
571
|
);
|
572
|
}
|
573
|
|
574
|
/**
|
575
|
* Sets role target on the user entity.
|
576
|
*/
|
577
|
public function rolesListSetTarget(FeedsSource $source, $entity, $target, array $values, array $mapping) {
|
578
|
// Add in defaults.
|
579
|
$defaults = $this->rolesListDefaults();
|
580
|
$mapping += $defaults;
|
581
|
$mapping['allowed_roles'] += $defaults['allowed_roles'];
|
582
|
|
583
|
// Eventually revoke roles. Do not touch roles that are not allowed to set
|
584
|
// by the source.
|
585
|
if ($mapping['revoke_roles']) {
|
586
|
foreach ($mapping['allowed_roles'] as $rid) {
|
587
|
unset($entity->roles[$rid]);
|
588
|
}
|
589
|
}
|
590
|
|
591
|
foreach ($values as $value) {
|
592
|
$role = NULL;
|
593
|
|
594
|
$value = trim($value);
|
595
|
if (strlen($value) < 1) {
|
596
|
// No role provided. Continue to the next role.
|
597
|
continue;
|
598
|
}
|
599
|
|
600
|
switch ($mapping['role_search']) {
|
601
|
case self::ROLE_SEARCH_NAME:
|
602
|
$role = user_role_load_by_name($value);
|
603
|
if (!$role && !empty($mapping['autocreate'])) {
|
604
|
// Create new role if role doesn't exist.
|
605
|
$role = new stdClass();
|
606
|
$role->name = $value;
|
607
|
user_role_save($role);
|
608
|
$role = user_role_load_by_name($role->name);
|
609
|
}
|
610
|
break;
|
611
|
|
612
|
case self::ROLE_SEARCH_RID:
|
613
|
$role = user_role_load($value);
|
614
|
break;
|
615
|
}
|
616
|
|
617
|
if ($role) {
|
618
|
// Check if the role may be assigned.
|
619
|
if (isset($mapping['allowed_roles'][$role->rid]) && !$mapping['allowed_roles'][$role->rid]) {
|
620
|
// This role may *not* be assiged.
|
621
|
continue;
|
622
|
}
|
623
|
$entity->roles[$role->rid] = $role->name;
|
624
|
}
|
625
|
}
|
626
|
}
|
627
|
|
628
|
/**
|
629
|
* Mapping configuration summary callback for target "pass".
|
630
|
*/
|
631
|
public function passSummaryCallback($mapping, $target, $form, $form_state) {
|
632
|
$options = $this->passSummaryCallbackOptions();
|
633
|
if (!isset($mapping['pass_encryption'])) {
|
634
|
$mapping['pass_encryption'] = self::PASS_UNENCRYPTED;
|
635
|
}
|
636
|
return t('Password encryption: <strong>@encryption</strong>', array('@encryption' => $options[$mapping['pass_encryption']]));
|
637
|
}
|
638
|
|
639
|
/**
|
640
|
* Returns the list of available password encryption methods.
|
641
|
*
|
642
|
* Used by ::passSummaryCallback().
|
643
|
*
|
644
|
* @return array
|
645
|
* An array of password encryption option titles.
|
646
|
*
|
647
|
* @see passSummaryCallback()
|
648
|
*/
|
649
|
protected function passSummaryCallbackOptions() {
|
650
|
return array(
|
651
|
self::PASS_UNENCRYPTED => t('None'),
|
652
|
self::PASS_MD5 => t('MD5'),
|
653
|
self::PASS_SHA512 => t('SHA512'),
|
654
|
);
|
655
|
}
|
656
|
|
657
|
/**
|
658
|
* Mapping configuration form callback for target "pass".
|
659
|
*/
|
660
|
public function passFormCallback($mapping, $target, $form, $form_state) {
|
661
|
return array(
|
662
|
'pass_encryption' => array(
|
663
|
'#type' => 'select',
|
664
|
'#title' => t('Password encryption'),
|
665
|
'#options' => $this->passFormCallbackOptions(),
|
666
|
'#default_value' => !empty($mapping['pass_encryption']) ? $mapping['pass_encryption'] : self::PASS_UNENCRYPTED,
|
667
|
),
|
668
|
);
|
669
|
}
|
670
|
|
671
|
/**
|
672
|
* Returns the list of available password encryption methods.
|
673
|
*
|
674
|
* Used by ::passFormCallback().
|
675
|
*
|
676
|
* @return array
|
677
|
* An array of password encryption option titles.
|
678
|
*
|
679
|
* @see passFormCallback()
|
680
|
*/
|
681
|
protected function passFormCallbackOptions() {
|
682
|
return array(
|
683
|
self::PASS_UNENCRYPTED => t('Unencrypted'),
|
684
|
self::PASS_MD5 => t('MD5 (used in older versions of Drupal)'),
|
685
|
self::PASS_SHA512 => t('SHA512 (default in Drupal 7)'),
|
686
|
);
|
687
|
}
|
688
|
|
689
|
/**
|
690
|
* Sets the password on the user target.
|
691
|
*
|
692
|
* @see setTargetElement()
|
693
|
*/
|
694
|
protected function setPassTarget($source, $target_user, $target_element, $value, $mapping) {
|
695
|
if (empty($value)) {
|
696
|
return;
|
697
|
}
|
698
|
if (!isset($mapping['pass_encryption'])) {
|
699
|
$mapping['pass_encryption'] = self::PASS_UNENCRYPTED;
|
700
|
}
|
701
|
|
702
|
switch ($mapping['pass_encryption']) {
|
703
|
case self::PASS_MD5:
|
704
|
require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
|
705
|
$new_hash = user_hash_password($value);
|
706
|
if ($new_hash) {
|
707
|
// Indicate an updated password.
|
708
|
$new_hash = 'U' . $new_hash;
|
709
|
$target_user->pass = $target_user->pass_crypted = $new_hash;
|
710
|
}
|
711
|
break;
|
712
|
|
713
|
case self::PASS_SHA512:
|
714
|
$target_user->pass = $target_user->pass_crypted = $value;
|
715
|
break;
|
716
|
|
717
|
default:
|
718
|
$target_user->pass = $value;
|
719
|
break;
|
720
|
}
|
721
|
}
|
722
|
|
723
|
}
|