class UserStorageController extends EntityDatabaseStorageController {
function attachLoad(&$queried_users, $revision_id = FALSE) {
$picture_fids = array();
foreach ($queried_users as $key => $record) {
if ($record->picture) {
$picture_fids[] = $record->picture;
}
$queried_users[$key]->data = unserialize($record->data);
$queried_users[$key]->roles = array();
if ($record->uid) {
$queried_users[$record->uid]->roles[] = BACKDROP_AUTHENTICATED_ROLE;
}
else {
$queried_users[$record->uid]->roles[] = BACKDROP_ANONYMOUS_ROLE;
}
}
$result = db_query('SELECT role, uid FROM {users_roles} WHERE uid IN (:uids)', array(':uids' => array_keys($queried_users)));
foreach ($result as $record) {
$queried_users[$record->uid]->roles[] = $record->role;
}
if (!empty($picture_fids) && config_get('system.core', 'user_pictures')) {
$pictures = file_load_multiple($picture_fids);
foreach ($queried_users as $entity) {
if (!empty($entity->picture) && isset($pictures[$entity->picture])) {
$entity->picture = $pictures[$entity->picture];
}
}
}
parent::attachLoad($queried_users, $revision_id);
}
public function create(array $values) {
if (!isset($values['created'])) {
$values['created'] = REQUEST_TIME;
}
$values['roles'][] = BACKDROP_AUTHENTICATED_ROLE;
return parent::create($values);
}
public function save(EntityInterface $entity) {
if (empty($entity->uid)) {
$entity->uid = db_next_id(db_query('SELECT MAX(uid) FROM {users}')->fetchField());
$entity->is_new = TRUE;
}
return parent::save($entity);
}
protected function preSave(EntityInterface $entity) {
$new_pass = isset($entity->pass) && strlen(trim($entity->pass)) > 0 ? trim($entity->pass) : NULL;
if ($entity->isNew() || !is_null($new_pass) && $new_pass !== $entity->original->pass) {
require_once BACKDROP_ROOT . '/' . settings_get('password_inc', 'core/includes/password.inc');
$entity->pass = user_hash_password($new_pass);
if (!$entity->pass) {
throw new EntityMalformedException('The entity does not have a password.');
}
}
if (valid_email_address($entity->name)) {
$user_email_match = config_get('system.core', 'user_email_match');
if ($user_email_match && ($entity->name != $entity->mail)) {
throw new EntityMalformedException('The username and email are both addresses that do not match.');
}
}
if (!empty($entity->picture_upload)) {
$entity->picture = $entity->picture_upload;
}
elseif (!empty($entity->picture_delete)) {
$entity->picture = NULL;
file_usage_delete($entity->original->picture, 'user', 'user', $entity->uid);
file_delete($entity->original->picture->fid);
}
if (!$entity->isNew()) {
if (!empty($entity->picture->fid) && (!isset($entity->original->picture->fid) || $entity->picture->fid != $entity->original->picture->fid)) {
$picture = $entity->picture;
if (!$picture->status) {
$info = image_get_info($picture->uri);
$picture_directory = file_default_scheme() . '://' . config_get('system.core', 'user_picture_path');
file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY);
$destination = file_stream_wrapper_uri_normalize($picture_directory . '/picture-' . $entity->uid . '-' . REQUEST_TIME . '.' . $info['extension']);
if ($picture = file_move($picture, $destination, FILE_EXISTS_RENAME)) {
$entity->picture = $picture;
file_usage_add($picture, 'user', 'user', $entity->uid);
}
}
if (!empty($entity->original->picture->fid)) {
file_usage_delete($entity->original->picture, 'user', 'user', $entity->uid);
file_delete($entity->original->picture->fid);
}
}
$entity->picture = empty($entity->picture->fid) ? NULL : $entity->picture->fid;
if (empty($entity->pass)) {
$entity->pass = $entity->original->pass;
}
}
if (isset($entity->roles)) {
$entity->roles = array_values(array_filter($entity->roles));
}
foreach (array('user_cancel_method', 'user_cancel_notify') as $key) {
if (isset($entity->{$key})) {
$entity->data[$key] = $entity->{$key};
}
}
}
protected function postSave(EntityInterface $entity, $update) {
if ($update) {
if ($entity->pass != $entity->original->pass) {
backdrop_session_destroy_uid($entity->uid);
if ($entity->uid == $GLOBALS['user']->uid) {
backdrop_session_regenerate();
}
}
$entity->roles = array_values(array_filter($entity->roles));
if ($entity->roles != $entity->original->roles) {
db_delete('users_roles')
->condition('uid', $entity->uid)
->execute();
$query = db_insert('users_roles')->fields(array('uid', 'role'));
foreach ($entity->roles as $role_name) {
if (!in_array($role_name, array(BACKDROP_ANONYMOUS_ROLE, BACKDROP_AUTHENTICATED_ROLE))) {
$query->values(array(
'uid' => $entity->uid,
'role' => $role_name,
));
}
}
$query->execute();
}
if ($entity->original->status != $entity->status && $entity->status == 0) {
backdrop_session_destroy_uid($entity->uid);
}
if ($entity->status != $entity->original->status) {
$op = $entity->status == 1 ? 'status_activated' : 'status_blocked';
_user_mail_notify($op, $entity);
}
}
else {
$role_names_to_skip = array_merge(array(BACKDROP_ANONYMOUS_ROLE, BACKDROP_AUTHENTICATED_ROLE), db_query('SELECT role FROM {users_roles} WHERE uid = :uid', array(':uid' => $entity->uid))->fetchCol());
if ($roles_to_save = array_diff($entity->roles, $role_names_to_skip)) {
$query = db_insert('users_roles')->fields(array('uid', 'role'));
foreach ($roles_to_save as $role_name) {
$query->values(array(
'uid' => $entity->uid,
'role' => $role_name,
));
}
$query->execute();
}
}
}
}