Skip to content

Commit b0c5811

Browse files
ApiV1ContactsController: PUT: Always update column contact.username
1 parent 24ed19f commit b0c5811

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

application/controllers/ApiV1ContactsController.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ function (Filter\Condition $condition) {
236236
$contactId = $this->getContactId($identifier);
237237
if ($contactId !== null) {
238238
if (! empty($data['username'])) {
239-
$this->assertUniqueUsername($data['username']);
239+
$this->assertUniqueUsername($data['username'], $contactId);
240240
}
241241

242242
$db->update('contact', [
@@ -447,19 +447,24 @@ private function addContact(array $data): void
447447
* Assert that the username is unique
448448
*
449449
* @param string $username
450+
* @param int $contactId The id of the contact to exclude
450451
*
451452
* @return void
452453
*
453454
* @throws HttpException if the username already exists
454455
*/
455-
private function assertUniqueUsername(string $username): void
456+
private function assertUniqueUsername(string $username, int $contactId = null): void
456457
{
457-
$user = Database::get()->fetchOne(
458-
(new Select())
459-
->from('contact')
460-
->columns('1')
461-
->where(['username = ?' => $username])
462-
);
458+
$stmt = (new Select())
459+
->from('contact')
460+
->columns('1')
461+
->where(['username = ?' => $username]);
462+
463+
if ($contactId) {
464+
$stmt->where(['id != ?' => $contactId]);
465+
}
466+
467+
$user = Database::get()->fetchOne($stmt);
463468

464469
if ($user !== false) {
465470
throw new HttpException(422, 'Username already exists');

0 commit comments

Comments
 (0)