From 76b38ded29a25609d688805833217d271eaf1538 Mon Sep 17 00:00:00 2001 From: Andrew Male Date: Thu, 1 Apr 2021 20:57:09 -0400 Subject: [PATCH] Adding relation API endpoints --- api/1/Account.api.php | 73 ++++++++++++++++++++++++++ inc/repositories/UserRelations.rpo.php | 10 ++-- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/api/1/Account.api.php b/api/1/Account.api.php index 4e547b0..bba9ed0 100644 --- a/api/1/Account.api.php +++ b/api/1/Account.api.php @@ -475,9 +475,12 @@ protected function registerEndpoints() : void { $this->registerEndpoint('GET', '/^Account\/Profile\/?/i', 'getProfile', true); $this->registerEndpoint('POST', '/^Account\/Register\/?/i', 'registerUser', null); $this->registerEndpoint('GET', '/^Account\/Relations\/?/i', 'getRelations', true); + $this->registerEndpoint('GET', '/^Account\/RelatedTo\/?/i', 'relatedTo', true); + $this->registerEndpoint('POST', '/^Account\/RemoveRelation\/?/i', 'removeRelation', true); $this->registerEndpoint('POST', '/^Account\/ResetPassword\/?/i', 'resetPassword', false); $this->registerEndpoint('POST', '/^Account\/SendPasswordReset\/?/i', 'sendPasswordReset', false); $this->registerEndpoint('GET', '/^Account\/Settings\/?/i', 'getSettings', true); + $this->registerEndpoint('POST', '/^Account\/SetRelation\/?/i', 'setRelation', true); $this->registerEndpoint('GET', '/^Account\/?/i', 'get', true); return; @@ -497,6 +500,52 @@ public function registerUser(Request $request, array $matches = null) : Response return $ret; } + /** + * Determines if the user is related to the given identifier. + * + * @param \Stoic\Web\Request $request The current request which routed to the endpoint. + * @param array|null $matches Array of matches returned by endpoint regex pattern. + * @return \Stoic\Web\Api\Response + */ + public function relatedTo(Request $request, array $matches = null) : Response { + $user = $this->getUser(); + $ret = $this->newResponse(); + $params = $request->getInput(); + + if (!$params->has('id')) { + $ret->setAsError('Invalid parameters supplied for request'); + + return $ret; + } + + $ret->setData((new UserRelations($this->db, $this->log))->areRelated($user->id, $params->getInt('id'))); + + return $ret; + } + + /** + * Attempts to remove a relationship between the authenticated user and another. + * + * @param \Stoic\Web\Request $request The current request which routed to the endpoint. + * @param array|null $matches Array of matches returned by endpoint regex pattern. + * @return \Stoic\Web\Api\Response + */ + public function removeRelation(Request $request, array $matches = null) : Response { + $user = $this->getUser(); + $ret = $this->newResponse(); + $params = $request->getInput(); + + if (!$params->has('id')) { + $ret->setAsError('Invalid parameters supplied'); + + return $ret; + } + + $ret->setData((new UserRelations($this->db, $this->log))->deleteRelation($user->id, $params->getInt('id'))); + + return $ret; + } + /** * Attempts to reset the user's password. * @@ -540,4 +589,28 @@ public function sendPasswordReset(Request $request, array $matches = null) : Res return $ret; } + + /** + * Sets the stage of relationship between two users. + * + * @param \Stoic\Web\Request $request The current request which routed to the endpoint. + * @param array|null $matches Array of matches returned by endpoint regex pattern. + * @return \Stoic\Web\Api\Response + */ + public function setRelation(Request $request, array $matches = null) : Response { + $user = $this->getUser(); + $ret = $this->newResponse(); + $params = $request->getInput(); + $rels = new UserRelations($this->db, $this->log); + + if (!$params->hasAll('id', 'stage')) { + $ret->setAsError('Invalid parameters supplied'); + + return $ret; + } + + $ret->setData($rels->changeStage($user->id, $params->getInt('id'), $params->getInt('stage'))); + + return $ret; + } } diff --git a/inc/repositories/UserRelations.rpo.php b/inc/repositories/UserRelations.rpo.php index 1a2a88f..c0e7bdb 100644 --- a/inc/repositories/UserRelations.rpo.php +++ b/inc/repositories/UserRelations.rpo.php @@ -51,8 +51,8 @@ protected function __initialize() : void { $this->urObj = new UserRelation($this->db, $this->log); if (!static::$dbInitialized) { - PdoHelper::storeQuery(PdoDrivers::PDO_SQLSRV, self::SQL_DELREL, "DELETE FROM {$this->urObj->getDbTableName()} WHERE [UserID_One] = :userOne AND [UserID_Two] = :userTwo"); - PdoHelper::storeQuery(PdoDrivers::PDO_MYSQL, self::SQL_DELREL, "DELETE FROM {$this->urObj->getDbTableName()} WHERE `UserID_One` = :userOne AND `UserID_Two` = :userTwo"); + PdoHelper::storeQuery(PdoDrivers::PDO_SQLSRV, self::SQL_DELREL, "DELETE FROM {$this->urObj->getDbTableName()} WHERE ([UserID_One] = :userOne AND [UserID_Two] = :userTwo) OR ([UserID_One] = :userTwo AND [UserID_Two] = :userOne)"); + PdoHelper::storeQuery(PdoDrivers::PDO_MYSQL, self::SQL_DELREL, "DELETE FROM {$this->urObj->getDbTableName()} WHERE (`UserID_One` = :userOne AND `UserID_Two` = :userTwo) OR (`UserID_One` = :userTwo AND `UserID_Two` = :userOne)"); PdoHelper::storeQuery(PdoDrivers::PDO_SQLSRV, self::SQL_DELALLFORUSR, "DELETE FROM {$this->urObj->getDbTableName()} WHERE [UserID_One] = :userId OR [UserID_Two] = :userId"); PdoHelper::storeQuery(PdoDrivers::PDO_MYSQL, self::SQL_DELALLFORUSR, "DELETE FROM {$this->urObj->getDbTableName()} WHERE `UserID_One` = :userId OR `UserID_Two` = :userId"); @@ -157,6 +157,10 @@ public function changeStage(int $userOne, int $userTwo, int $stage) : bool { return false; } + if ($rel[0]->stage->is(UserRelationStages::INVITED) && $stage > UserRelationStages::INVITED && $rel[0]->userOne == $userOne) { + return false; + } + if ($rel[0]->stage->is($stage)) { return true; } @@ -190,7 +194,7 @@ public function deleteRelation(int $userOne, int $userTwo) : bool { $stmt->execute(); }, "Failed to delete user's relation"); - return false; + return true; } /**