Merge pull request #1301 from PineappleIOnic/feat-fix-unique-keys-not-updating
Fix Unique Keys not being updated when changing a user's email address
This commit is contained in:
commit
f013d07df3
|
@ -1103,14 +1103,23 @@ App::patch('/v1/account/email')
|
|||
|
||||
// TODO after this user needs to confirm mail again
|
||||
|
||||
$user = $projectDB->updateDocument(\array_merge(
|
||||
if (!$isAnonymousUser) {
|
||||
// Remove previous unique ID.
|
||||
$projectDB->deleteUniqueKey(\md5($user->getArrayCopy()['$collection'].':'.'email'.'='.$user->getAttribute('email')));
|
||||
}
|
||||
|
||||
$document = (\array_merge(
|
||||
$user->getArrayCopy(),
|
||||
($isAnonymousUser ? [ 'password' => Auth::passwordHash($password) ] : []),
|
||||
[
|
||||
'email' => $email,
|
||||
'emailVerification' => false,
|
||||
]
|
||||
));
|
||||
));
|
||||
|
||||
$user = $projectDB->updateDocument($document);
|
||||
|
||||
$projectDB->addUniqueKey(\md5($document['$collection'].':'.'email'.'='.$email));
|
||||
|
||||
if (false === $user) {
|
||||
throw new Exception('Failed saving user to DB', 500);
|
||||
|
|
|
@ -91,12 +91,21 @@ abstract class Adapter
|
|||
/**
|
||||
* Delete Unique Key.
|
||||
*
|
||||
* @param int $key
|
||||
* @param string $key
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract public function deleteUniqueKey($key);
|
||||
|
||||
/**
|
||||
* Add Unique Key.
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract public function addUniqueKey($key);
|
||||
|
||||
/**
|
||||
* Create Namespace.
|
||||
*
|
||||
|
|
|
@ -360,7 +360,7 @@ class MySQL extends Adapter
|
|||
/**
|
||||
* Delete Unique Key.
|
||||
*
|
||||
* @param int $key
|
||||
* @param string $key
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
|
@ -377,6 +377,30 @@ class MySQL extends Adapter
|
|||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Unique Key.
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addUniqueKey($key)
|
||||
{
|
||||
$st = $this->getPDO()->prepare('INSERT INTO `'.$this->getNamespace().'.database.unique`
|
||||
SET `key` = :key;
|
||||
');
|
||||
|
||||
$st->bindValue(':key', $key, PDO::PARAM_STR);
|
||||
|
||||
if (!$st->execute()) {
|
||||
throw new Duplicate('Duplicated Property: '.$key);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Relation.
|
||||
*
|
||||
|
|
|
@ -169,6 +169,22 @@ class Redis extends Adapter
|
|||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Unique Key.
|
||||
*
|
||||
* @param $key
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addUniqueKey($key)
|
||||
{
|
||||
$data = $this->adapter->addUniqueKey($key);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Namespace.
|
||||
*
|
||||
|
|
|
@ -370,6 +370,18 @@ class Database
|
|||
return new Document($this->adapter->deleteUniqueKey($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $key
|
||||
*
|
||||
* @return Document|false
|
||||
*
|
||||
* @throws AuthorizationException
|
||||
*/
|
||||
public function addUniqueKey($key)
|
||||
{
|
||||
return new Document($this->adapter->addUniqueKey($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
|
|
|
@ -591,6 +591,29 @@ trait AccountBase
|
|||
|
||||
$this->assertEquals($response['headers']['status-code'], 400);
|
||||
|
||||
// Test if we can create a new account with the old email
|
||||
|
||||
/**
|
||||
* Test for SUCCESS
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_POST, '/account', array_merge([
|
||||
'origin' => 'http://localhost',
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
]), [
|
||||
'email' => $data['email'],
|
||||
'password' => $data['password'],
|
||||
'name' => $data['name'],
|
||||
]);
|
||||
|
||||
$this->assertEquals($response['headers']['status-code'], 201);
|
||||
$this->assertNotEmpty($response['body']);
|
||||
$this->assertNotEmpty($response['body']['$id']);
|
||||
$this->assertIsNumeric($response['body']['registration']);
|
||||
$this->assertEquals($response['body']['email'], $data['email']);
|
||||
$this->assertEquals($response['body']['name'], $data['name'],);
|
||||
|
||||
|
||||
$data['email'] = $newEmail;
|
||||
|
||||
return $data;
|
||||
|
|
Loading…
Reference in a new issue