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
|
/**
|
21
|
* Define entity type.
|
22
|
*/
|
23
|
public function entityType() {
|
24
|
return 'user';
|
25
|
}
|
26
|
|
27
|
/**
|
28
|
* Implements parent::entityInfo().
|
29
|
*/
|
30
|
protected function entityInfo() {
|
31
|
$info = parent::entityInfo();
|
32
|
$info['label plural'] = t('Users');
|
33
|
return $info;
|
34
|
}
|
35
|
|
36
|
/**
|
37
|
* Creates a new user account in memory and returns it.
|
38
|
*/
|
39
|
protected function newEntity(FeedsSource $source) {
|
40
|
$account = new stdClass();
|
41
|
$account->uid = 0;
|
42
|
$account->roles = array_filter($this->config['roles']);
|
43
|
$account->status = $this->config['status'];
|
44
|
return $account;
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* Loads an existing user.
|
49
|
*/
|
50
|
protected function entityLoad(FeedsSource $source, $uid) {
|
51
|
$user = parent::entityLoad($source, $uid);
|
52
|
|
53
|
// Copy the password so that we can compare it again at save.
|
54
|
$user->feeds_original_pass = $user->pass;
|
55
|
return $user;
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
* Validates a user account.
|
60
|
*/
|
61
|
protected function entityValidate($account) {
|
62
|
if (empty($account->name) || empty($account->mail) || !valid_email_address($account->mail)) {
|
63
|
throw new FeedsValidationException(t('User name missing or email not valid.'));
|
64
|
}
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Save a user account.
|
69
|
*/
|
70
|
protected function entitySave($account) {
|
71
|
if ($this->config['defuse_mail']) {
|
72
|
$account->mail = $account->mail . '_test';
|
73
|
}
|
74
|
|
75
|
$edit = (array) $account;
|
76
|
|
77
|
// Remove pass from $edit if the password is unchanged.
|
78
|
if (isset($account->feeds_original_pass) && $account->pass == $account->feeds_original_pass) {
|
79
|
unset($edit['pass']);
|
80
|
}
|
81
|
|
82
|
user_save($account, $edit);
|
83
|
if ($account->uid && !empty($account->openid)) {
|
84
|
$authmap = array(
|
85
|
'uid' => $account->uid,
|
86
|
'module' => 'openid',
|
87
|
'authname' => $account->openid,
|
88
|
);
|
89
|
if (SAVED_UPDATED != drupal_write_record('authmap', $authmap, array('uid', 'module'))) {
|
90
|
drupal_write_record('authmap', $authmap);
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* Delete multiple user accounts.
|
97
|
*/
|
98
|
protected function entityDeleteMultiple($uids) {
|
99
|
user_delete_multiple($uids);
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* Override parent::configDefaults().
|
104
|
*/
|
105
|
public function configDefaults() {
|
106
|
return array(
|
107
|
'roles' => array(),
|
108
|
'status' => 1,
|
109
|
'defuse_mail' => FALSE,
|
110
|
) + parent::configDefaults();
|
111
|
}
|
112
|
|
113
|
/**
|
114
|
* Override parent::configForm().
|
115
|
*/
|
116
|
public function configForm(&$form_state) {
|
117
|
$form = parent::configForm($form_state);
|
118
|
$form['status'] = array(
|
119
|
'#type' => 'radios',
|
120
|
'#title' => t('Status'),
|
121
|
'#description' => t('Select whether users should be imported active or blocked.'),
|
122
|
'#options' => array(0 => t('Blocked'), 1 => t('Active')),
|
123
|
'#default_value' => $this->config['status'],
|
124
|
);
|
125
|
|
126
|
$roles = user_roles(TRUE);
|
127
|
unset($roles[2]);
|
128
|
if (count($roles)) {
|
129
|
$form['roles'] = array(
|
130
|
'#type' => 'checkboxes',
|
131
|
'#title' => t('Additional roles'),
|
132
|
'#description' => t('Every user is assigned the "authenticated user" role. Select additional roles here.'),
|
133
|
'#default_value' => $this->config['roles'],
|
134
|
'#options' => $roles,
|
135
|
);
|
136
|
}
|
137
|
$form['defuse_mail'] = array(
|
138
|
'#type' => 'checkbox',
|
139
|
'#title' => t('Defuse e-mail addresses'),
|
140
|
'#description' => t('This appends _test to all imported e-mail addresses to ensure they cannot be used as recipients.'),
|
141
|
'#default_value' => $this->config['defuse_mail'],
|
142
|
);
|
143
|
$form['update_non_existent']['#options'][FEEDS_BLOCK_NON_EXISTENT] = t('Block non-existent users');
|
144
|
return $form;
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* Override setTargetElement to operate on a target item that is a node.
|
149
|
*/
|
150
|
public function setTargetElement(FeedsSource $source, $target_user, $target_element, $value) {
|
151
|
switch ($target_element) {
|
152
|
case 'created':
|
153
|
$target_user->created = feeds_to_unixtime($value, REQUEST_TIME);
|
154
|
break;
|
155
|
case 'language':
|
156
|
$target_user->language = strtolower($value);
|
157
|
break;
|
158
|
default:
|
159
|
parent::setTargetElement($source, $target_user, $target_element, $value);
|
160
|
break;
|
161
|
}
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Return available mapping targets.
|
166
|
*/
|
167
|
public function getMappingTargets() {
|
168
|
$targets = parent::getMappingTargets();
|
169
|
$targets += array(
|
170
|
'name' => array(
|
171
|
'name' => t('User name'),
|
172
|
'description' => t('Name of the user.'),
|
173
|
'optional_unique' => TRUE,
|
174
|
),
|
175
|
'mail' => array(
|
176
|
'name' => t('Email address'),
|
177
|
'description' => t('Email address of the user.'),
|
178
|
'optional_unique' => TRUE,
|
179
|
),
|
180
|
'created' => array(
|
181
|
'name' => t('Created date'),
|
182
|
'description' => t('The created (e. g. joined) data of the user.'),
|
183
|
),
|
184
|
'pass' => array(
|
185
|
'name' => t('Unencrypted Password'),
|
186
|
'description' => t('The unencrypted user password.'),
|
187
|
),
|
188
|
'status' => array(
|
189
|
'name' => t('Account status'),
|
190
|
'description' => t('Whether a user is active or not. 1 stands for active, 0 for blocked.'),
|
191
|
),
|
192
|
'language' => array(
|
193
|
'name' => t('User language'),
|
194
|
'description' => t('Default language for the user.'),
|
195
|
),
|
196
|
);
|
197
|
if (module_exists('openid')) {
|
198
|
$targets['openid'] = array(
|
199
|
'name' => t('OpenID identifier'),
|
200
|
'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.'),
|
201
|
'optional_unique' => TRUE,
|
202
|
);
|
203
|
}
|
204
|
|
205
|
$this->getHookTargets($targets);
|
206
|
|
207
|
return $targets;
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* Get id of an existing feed item term if available.
|
212
|
*/
|
213
|
protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
|
214
|
if ($uid = parent::existingEntityId($source, $result)) {
|
215
|
return $uid;
|
216
|
}
|
217
|
|
218
|
// Iterate through all unique targets and try to find a user for the
|
219
|
// target's value.
|
220
|
foreach ($this->uniqueTargets($source, $result) as $target => $value) {
|
221
|
switch ($target) {
|
222
|
case 'name':
|
223
|
$uid = db_query("SELECT uid FROM {users} WHERE name = :name", array(':name' => $value))->fetchField();
|
224
|
break;
|
225
|
case 'mail':
|
226
|
$uid = db_query("SELECT uid FROM {users} WHERE mail = :mail", array(':mail' => $value))->fetchField();
|
227
|
break;
|
228
|
case 'openid':
|
229
|
$uid = db_query("SELECT uid FROM {authmap} WHERE authname = :authname AND module = 'openid'", array(':authname' => $value))->fetchField();
|
230
|
break;
|
231
|
}
|
232
|
if ($uid) {
|
233
|
// Return with the first nid found.
|
234
|
return $uid;
|
235
|
}
|
236
|
}
|
237
|
return 0;
|
238
|
}
|
239
|
|
240
|
/**
|
241
|
* Overrides FeedsProcessor::clean().
|
242
|
*
|
243
|
* Block users instead of deleting them.
|
244
|
*
|
245
|
* @param FeedsState $state
|
246
|
* The FeedsState object for the given stage.
|
247
|
*/
|
248
|
protected function clean(FeedsState $state) {
|
249
|
// Delegate to parent if not blocking or option not set.
|
250
|
if (!isset($this->config['update_non_existent']) || $this->config['update_non_existent'] !== FEEDS_BLOCK_NON_EXISTENT) {
|
251
|
return parent::clean($state);
|
252
|
}
|
253
|
|
254
|
if (!empty($state->removeList)) {
|
255
|
// @see user_user_operations_block().
|
256
|
// The following foreach is copied from above function but with an added
|
257
|
// counter to count blocked users.
|
258
|
foreach (user_load_multiple($state->removeList) as $account) {
|
259
|
$this->loadItemInfo($account);
|
260
|
$account->feeds_item->hash = $this->config['update_non_existent'];
|
261
|
// For efficiency manually save the original account before applying any
|
262
|
// changes.
|
263
|
$account->original = clone $account;
|
264
|
user_save($account, array('status' => 0));
|
265
|
$state->blocked++;
|
266
|
}
|
267
|
}
|
268
|
}
|
269
|
|
270
|
}
|