public function editUser(int $userId, array $post = []) : bool
{
$this->db->beginTransaction();
$updates = [];
foreach (\array_keys($post['groups']) as $i) {
$post['groups'][$i] += 0;
}
$oldGroups = $this->getUsersGroups($userId);
$delete = \array_diff($oldGroups, $post['groups']);
$insert = \array_diff($post['groups'], $oldGroups);
// Manage group changes:
foreach ($insert as $ins) {
$this->db->insert('airship_users_groups', ['userid' => $userId, 'groupid' => $ins]);
}
foreach ($delete as $del) {
$this->db->delete('airship_users_groups', ['userid' => $userId, 'groupid' => $del]);
}
foreach (['username', 'uniqueid', 'email', 'display_name', 'real_name'] as $f) {
$updates[$f] = $post[$f] ?? null;
}
if (!empty($post['password'])) {
$updates['password'] = $this->airship_auth->createHash(new HiddenString($post['password']));
}
$updates['custom_fields'] = \json_encode(\json_decode($post['custom_fields'], true));
$this->db->update('airship_users', $updates, ['userid' => $userId]);
return $this->db->commit();
}