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