1
0
Fork 0
mirror of synced 2024-06-01 18:39:57 +12:00

Fix Unique Keys not being update when changing a user's email address

This commit is contained in:
Bradley Schofield 2021-06-17 16:46:29 +01:00
parent a433181e17
commit a56463e56e
5 changed files with 75 additions and 4 deletions

View file

@ -1103,14 +1103,23 @@ App::patch('/v1/account/email')
// TODO after this user needs to confirm mail again // 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(), $user->getArrayCopy(),
($isAnonymousUser ? [ 'password' => Auth::passwordHash($password) ] : []), ($isAnonymousUser ? [ 'password' => Auth::passwordHash($password) ] : []),
[ [
'email' => $email, 'email' => $email,
'emailVerification' => false, 'emailVerification' => false,
] ]
)); ));
$user = $projectDB->updateDocument($document, ['email' => $email]);
$projectDB->addUniqueKey(\md5($document['$collection'].':'.'email'.'='.$email));
if (false === $user) { if (false === $user) {
throw new Exception('Failed saving user to DB', 500); throw new Exception('Failed saving user to DB', 500);

View file

@ -91,12 +91,21 @@ abstract class Adapter
/** /**
* Delete Unique Key. * Delete Unique Key.
* *
* @param int $key * @param String $key
* *
* @return array * @return array
*/ */
abstract public function deleteUniqueKey($key); abstract public function deleteUniqueKey($key);
/**
* Add Unique Key.
*
* @param String $key
*
* @return array
*/
abstract public function addUniqueKey($key);
/** /**
* Create Namespace. * Create Namespace.
* *

View file

@ -360,7 +360,7 @@ class MySQL extends Adapter
/** /**
* Delete Unique Key. * Delete Unique Key.
* *
* @param int $key * @param String $key
* *
* @return array * @return array
* *
@ -377,6 +377,31 @@ class MySQL extends Adapter
return []; 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. * Create Relation.
* *

View file

@ -169,6 +169,22 @@ class Redis extends Adapter
return $data; return $data;
} }
/**
* Add Unique Key.
*
* @param $key
*
* @return array
*
* @throws Exception
*/
public function addUniqueKey($key)
{
$data = $this->adapter->addUniqueKey($key);
return $data;
}
/** /**
* Create Namespace. * Create Namespace.
* *

View file

@ -370,6 +370,18 @@ class Database
return new Document($this->adapter->deleteUniqueKey($key)); 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 * @return array
*/ */