1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* FeedsUserProcessor class.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Feeds processor plugin. Create users from feed items.
|
10
|
*/
|
11
|
class FeedsUserProcessor extends FeedsProcessor {
|
12
|
/**
|
13
|
* Define entity type.
|
14
|
*/
|
15
|
public function entityType() {
|
16
|
return 'user';
|
17
|
}
|
18
|
|
19
|
/**
|
20
|
* Implements parent::entityInfo().
|
21
|
*/
|
22
|
protected function entityInfo() {
|
23
|
$info = parent::entityInfo();
|
24
|
$info['label plural'] = t('Users');
|
25
|
return $info;
|
26
|
}
|
27
|
|
28
|
/**
|
29
|
* Creates a new user account in memory and returns it.
|
30
|
*/
|
31
|
protected function newEntity(FeedsSource $source) {
|
32
|
$account = new stdClass();
|
33
|
$account->uid = 0;
|
34
|
$account->roles = array_filter($this->config['roles']);
|
35
|
$account->status = $this->config['status'];
|
36
|
return $account;
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* Loads an existing user.
|
41
|
*/
|
42
|
protected function entityLoad(FeedsSource $source, $uid) {
|
43
|
$user = parent::entityLoad($source, $uid);
|
44
|
|
45
|
// Copy the password so that we can compare it again at save.
|
46
|
$user->feeds_original_pass = $user->pass;
|
47
|
return $user;
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Validates a user account.
|
52
|
*/
|
53
|
protected function entityValidate($account) {
|
54
|
if (empty($account->name) || empty($account->mail) || !valid_email_address($account->mail)) {
|
55
|
throw new FeedsValidationException(t('User name missing or email not valid.'));
|
56
|
}
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* Save a user account.
|
61
|
*/
|
62
|
protected function entitySave($account) {
|
63
|
if ($this->config['defuse_mail']) {
|
64
|
$account->mail = $account->mail . '_test';
|
65
|
}
|
66
|
|
67
|
$edit = (array) $account;
|
68
|
|
69
|
// Remove pass from $edit if the password is unchanged.
|
70
|
if (isset($account->feeds_original_pass) && $account->pass == $account->feeds_original_pass) {
|
71
|
unset($edit['pass']);
|
72
|
}
|
73
|
|
74
|
user_save($account, $edit);
|
75
|
if ($account->uid && !empty($account->openid)) {
|
76
|
$authmap = array(
|
77
|
'uid' => $account->uid,
|
78
|
'module' => 'openid',
|
79
|
'authname' => $account->openid,
|
80
|
);
|
81
|
if (SAVED_UPDATED != drupal_write_record('authmap', $authmap, array('uid', 'module'))) {
|
82
|
drupal_write_record('authmap', $authmap);
|
83
|
}
|
84
|
}
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* Delete multiple user accounts.
|
89
|
*/
|
90
|
protected function entityDeleteMultiple($uids) {
|
91
|
user_delete_multiple($uids);
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Override parent::configDefaults().
|
96
|
*/
|
97
|
public function configDefaults() {
|
98
|
return array(
|
99
|
'roles' => array(),
|
100
|
'status' => 1,
|
101
|
'defuse_mail' => FALSE,
|
102
|
) + parent::configDefaults();
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* Override parent::configForm().
|
107
|
*/
|
108
|
public function configForm(&$form_state) {
|
109
|
$form = parent::configForm($form_state);
|
110
|
$form['status'] = array(
|
111
|
'#type' => 'radios',
|
112
|
'#title' => t('Status'),
|
113
|
'#description' => t('Select whether users should be imported active or blocked.'),
|
114
|
'#options' => array(0 => t('Blocked'), 1 => t('Active')),
|
115
|
'#default_value' => $this->config['status'],
|
116
|
);
|
117
|
|
118
|
$roles = user_roles(TRUE);
|
119
|
unset($roles[2]);
|
120
|
if (count($roles)) {
|
121
|
$form['roles'] = array(
|
122
|
'#type' => 'checkboxes',
|
123
|
'#title' => t('Additional roles'),
|
124
|
'#description' => t('Every user is assigned the "authenticated user" role. Select additional roles here.'),
|
125
|
'#default_value' => $this->config['roles'],
|
126
|
'#options' => $roles,
|
127
|
);
|
128
|
}
|
129
|
$form['defuse_mail'] = array(
|
130
|
'#type' => 'checkbox',
|
131
|
'#title' => t('Defuse e-mail addresses'),
|
132
|
'#description' => t('This appends _test to all imported e-mail addresses to ensure they cannot be used as recipients.'),
|
133
|
'#default_value' => $this->config['defuse_mail'],
|
134
|
);
|
135
|
return $form;
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Override setTargetElement to operate on a target item that is a node.
|
140
|
*/
|
141
|
public function setTargetElement(FeedsSource $source, $target_user, $target_element, $value) {
|
142
|
switch ($target_element) {
|
143
|
case 'created':
|
144
|
$target_user->created = feeds_to_unixtime($value, REQUEST_TIME);
|
145
|
break;
|
146
|
case 'language':
|
147
|
$target_user->language = strtolower($value);
|
148
|
break;
|
149
|
default:
|
150
|
parent::setTargetElement($source, $target_user, $target_element, $value);
|
151
|
break;
|
152
|
}
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* Return available mapping targets.
|
157
|
*/
|
158
|
public function getMappingTargets() {
|
159
|
$targets = parent::getMappingTargets();
|
160
|
$targets += array(
|
161
|
'name' => array(
|
162
|
'name' => t('User name'),
|
163
|
'description' => t('Name of the user.'),
|
164
|
'optional_unique' => TRUE,
|
165
|
),
|
166
|
'mail' => array(
|
167
|
'name' => t('Email address'),
|
168
|
'description' => t('Email address of the user.'),
|
169
|
'optional_unique' => TRUE,
|
170
|
),
|
171
|
'created' => array(
|
172
|
'name' => t('Created date'),
|
173
|
'description' => t('The created (e. g. joined) data of the user.'),
|
174
|
),
|
175
|
'pass' => array(
|
176
|
'name' => t('Unencrypted Password'),
|
177
|
'description' => t('The unencrypted user password.'),
|
178
|
),
|
179
|
'status' => array(
|
180
|
'name' => t('Account status'),
|
181
|
'description' => t('Whether a user is active or not. 1 stands for active, 0 for blocked.'),
|
182
|
),
|
183
|
'language' => array(
|
184
|
'name' => t('User language'),
|
185
|
'description' => t('Default language for the user.'),
|
186
|
),
|
187
|
);
|
188
|
if (module_exists('openid')) {
|
189
|
$targets['openid'] = array(
|
190
|
'name' => t('OpenID identifier'),
|
191
|
'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.'),
|
192
|
'optional_unique' => TRUE,
|
193
|
);
|
194
|
}
|
195
|
|
196
|
// Let other modules expose mapping targets.
|
197
|
self::loadMappers();
|
198
|
$entity_type = $this->entityType();
|
199
|
$bundle = $this->bundle();
|
200
|
drupal_alter('feeds_processor_targets', $targets, $entity_type, $bundle);
|
201
|
|
202
|
return $targets;
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* Get id of an existing feed item term if available.
|
207
|
*/
|
208
|
protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
|
209
|
if ($uid = parent::existingEntityId($source, $result)) {
|
210
|
return $uid;
|
211
|
}
|
212
|
|
213
|
// Iterate through all unique targets and try to find a user for the
|
214
|
// target's value.
|
215
|
foreach ($this->uniqueTargets($source, $result) as $target => $value) {
|
216
|
switch ($target) {
|
217
|
case 'name':
|
218
|
$uid = db_query("SELECT uid FROM {users} WHERE name = :name", array(':name' => $value))->fetchField();
|
219
|
break;
|
220
|
case 'mail':
|
221
|
$uid = db_query("SELECT uid FROM {users} WHERE mail = :mail", array(':mail' => $value))->fetchField();
|
222
|
break;
|
223
|
case 'openid':
|
224
|
$uid = db_query("SELECT uid FROM {authmap} WHERE authname = :authname AND module = 'openid'", array(':authname' => $value))->fetchField();
|
225
|
break;
|
226
|
}
|
227
|
if ($uid) {
|
228
|
// Return with the first nid found.
|
229
|
return $uid;
|
230
|
}
|
231
|
}
|
232
|
return 0;
|
233
|
}
|
234
|
}
|