From edde41e50f55b983ddd34374d626ec205fd116a8 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 28 Aug 2020 20:53:19 +0200 Subject: [PATCH] delete unique key from db --- app/controllers/api/users.php | 4 ++++ src/Appwrite/Database/Adapter.php | 9 +++++++++ src/Appwrite/Database/Adapter/MySQL.php | 22 +++++++++++++++++++++- src/Appwrite/Database/Adapter/Redis.php | 16 ++++++++++++++++ src/Appwrite/Database/Database.php | 12 ++++++++++++ 5 files changed, 62 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 2592076d3f..971396ffc3 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -209,6 +209,10 @@ $utopia->delete('/v1/users/:userId') throw new Exception('Failed to remove user from DB', 500); } + if (!$projectDB->deleteUniqueKey(md5('users:email='.$user->getAttribute('email', null)))) { + throw new Exception('Failed to remove unique key from DB', 500); + } + $reservedId = $projectDB->createDocument([ '$collection' => Database::SYSTEM_COLLECTION_RESERVED, '$id' => $userId, diff --git a/src/Appwrite/Database/Adapter.php b/src/Appwrite/Database/Adapter.php index 6fa79cbdcd..3526256b22 100644 --- a/src/Appwrite/Database/Adapter.php +++ b/src/Appwrite/Database/Adapter.php @@ -88,6 +88,15 @@ abstract class Adapter */ abstract public function deleteDocument($id); + /** + * Delete Unique Key. + * + * @param int $key + * + * @return array + */ + abstract public function deleteUniqueKey($key); + /** * Create Namespace. * diff --git a/src/Appwrite/Database/Adapter/MySQL.php b/src/Appwrite/Database/Adapter/MySQL.php index f766666306..52dd521d9e 100644 --- a/src/Appwrite/Database/Adapter/MySQL.php +++ b/src/Appwrite/Database/Adapter/MySQL.php @@ -191,7 +191,7 @@ class MySQL extends Adapter $st = $this->getPDO()->prepare('INSERT INTO `'.$this->getNamespace().'.database.unique` SET `key` = :key; '); - + $st->bindValue(':key', \md5($data['$collection'].':'.$key.'='.$value), PDO::PARAM_STR); if (!$st->execute()) { @@ -366,6 +366,26 @@ class MySQL extends Adapter return []; } + /** + * Delete Unique Key. + * + * @param int $id + * + * @return array + * + * @throws Exception + */ + public function deleteUniqueKey($key) + { + $st1 = $this->getPDO()->prepare('DELETE FROM `'.$this->getNamespace().'.database.unique` WHERE `key` = :key'); + + $st1->bindValue(':key', $key, PDO::PARAM_STR); + + $st1->execute(); + + return []; + } + /** * Create Relation. * diff --git a/src/Appwrite/Database/Adapter/Redis.php b/src/Appwrite/Database/Adapter/Redis.php index 15a7a887d8..cb35378a9c 100644 --- a/src/Appwrite/Database/Adapter/Redis.php +++ b/src/Appwrite/Database/Adapter/Redis.php @@ -153,6 +153,22 @@ class Redis extends Adapter return $data; } + /** + * Delete Unique Key. + * + * @param $key + * + * @return array + * + * @throws Exception + */ + public function deleteUniqueKey($key) + { + $data = $this->adapter->deleteUniqueKey($key); + + return $data; + } + /** * Create Namespace. * diff --git a/src/Appwrite/Database/Database.php b/src/Appwrite/Database/Database.php index bd61eb3ffd..d6650d3021 100644 --- a/src/Appwrite/Database/Database.php +++ b/src/Appwrite/Database/Database.php @@ -298,6 +298,18 @@ class Database return new Document($this->adapter->deleteDocument($id)); } + /** + * @param int $key + * + * @return Document|false + * + * @throws AuthorizationException + */ + public function deleteUniqueKey($key) + { + return new Document($this->adapter->deleteUniqueKey($key)); + } + /** * @return array */