From 588f580bf920cf09c70cee06741d177b6de27e88 Mon Sep 17 00:00:00 2001 From: "vivien.dufour" Date: Fri, 12 Jan 2024 14:48:50 +0100 Subject: [PATCH] Can now share, unshare and open shared tactic --- front/views/Home.tsx | 3 + public/api/index.php | 2 +- src/App/Controller/EditorController.php | 10 ++- src/App/Validator/TacticValidator.php | 1 - src/Core/Gateway/TacticInfoGateway.php | 29 +++++++++ src/Core/Model/TacticModel.php | 83 ++++++++++++++++--------- 6 files changed, 97 insertions(+), 31 deletions(-) diff --git a/front/views/Home.tsx b/front/views/Home.tsx index 132457c..9a188bb 100644 --- a/front/views/Home.tsx +++ b/front/views/Home.tsx @@ -83,6 +83,9 @@ function SideMenu({
{location.pathname="/shareTactic"}}>

Partager une tactique

+
{location.pathname="/unshareTactic"}}> +

Retirer le partage d'une tactique

+
) diff --git a/public/api/index.php b/public/api/index.php index da25013..d1f0ce4 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -18,7 +18,7 @@ use IQBall\Core\Model\AuthModel; use IQBall\Core\Model\TacticModel; function getTacticController(): APITacticController { - return new APITacticController(new TacticModel(new TacticInfoGateway(new Connection(get_database())))); + return new APITacticController(new TacticModel(new TacticInfoGateway(new Connection(get_database())), new AccountGateway(new Connection(get_database())))); } function getAuthController(): APIAuthController { diff --git a/src/App/Controller/EditorController.php b/src/App/Controller/EditorController.php index 4bdcfae..fe0aee2 100644 --- a/src/App/Controller/EditorController.php +++ b/src/App/Controller/EditorController.php @@ -76,7 +76,15 @@ class EditorController { public function openEditor(int $id, SessionHandle $session): ViewHttpResponse { $tactic = $this->model->get($id); - $failure = TacticValidator::validateAccess($id, $tactic, $session->getAccount()->getUser()->getId()); + $ownerId = $session->getAccount()->getUser()->getId(); + + $ids = $this->model->getOwnerIdTacticShared($id, $session->getAccount()->getUser()->getId()); + + if(isset($ids)) { + $ownerId = $ids['owner']; + } + + $failure = TacticValidator::validateAccess($id, $tactic, $ownerId); if ($failure != null) { return ViewHttpResponse::twig('error.html.twig', ['failures' => [$failure]], HttpCodes::NOT_FOUND); diff --git a/src/App/Validator/TacticValidator.php b/src/App/Validator/TacticValidator.php index 28985de..437b7f7 100644 --- a/src/App/Validator/TacticValidator.php +++ b/src/App/Validator/TacticValidator.php @@ -15,5 +15,4 @@ class TacticValidator { } return null; } - } diff --git a/src/Core/Gateway/TacticInfoGateway.php b/src/Core/Gateway/TacticInfoGateway.php index 961045f..b1d8701 100644 --- a/src/Core/Gateway/TacticInfoGateway.php +++ b/src/Core/Gateway/TacticInfoGateway.php @@ -150,6 +150,35 @@ class TacticInfoGateway { return array_map(fn(array $row) => $this->rowToTacticInfo($row), $res); } + + public function getOwnerIdTacticSharedTeam(int $tacticId, int $accountId) : ?array { + return $this->con->fetch( + "SELECT t.owner + FROM Account a + JOIN Member m on a.id = m.id_user + JOIN TacticSharedTeam tt ON m.id_team = tt.id_team + JOIN Tactic t ON tt.id_tactic = t.id + WHERE tt.id_tactic = :tacticId AND m.id_user = :accountId", + [ + ":tacticId" => [$tacticId, PDO::PARAM_INT], + ":accountId" => [$accountId, PDO::PARAM_INT] + ] + ); + } + + public function getOwnerIdTacticSharedAccount(int $tacticId, int $accountId) : ?array { + return $this->con->fetch( + "SELECT t.owner FROM Account a + JOIN TacticSharedAccount ta ON a.id = ta.id_account + JOIN Tactic t ON ta.id_tactic = t.id + WHERE ta.id_tactic = :tacticId AND ta.id_account = :accountId", + [ + ":tacticId" => [$tacticId, PDO::PARAM_INT], + ":accountId" => [$accountId, PDO::PARAM_INT] + ] + ); + } + /** * @param string $name * @param int $owner diff --git a/src/Core/Model/TacticModel.php b/src/Core/Model/TacticModel.php index 1669dfc..3dc2891 100644 --- a/src/Core/Model/TacticModel.php +++ b/src/Core/Model/TacticModel.php @@ -135,6 +135,61 @@ class TacticModel { return $allTactics; } + public function shareTacticToAccount(int $accountId, int $tacticId): int + { + return $this->users->shareTacticToAccount($accountId, $tacticId); + } + + public function shareTacticToAccountMail(string $email, int $tacticId): ?int + { + $account = $this->users->getAccountFromMail($email); + var_dump($account); + $accountId = $account->getUser()->getId(); + var_dump($accountId); + if(isset($accountId)) { + return $this->shareTacticToAccount($accountId, $tacticId); + } + return null; + } + + + public function unshareTactic(int $tacticId): int + { + return $this->users->unshareTactic($tacticId); + } + + public function unshareTacticToAccount(int $tacticId, int $accountId): int + { + return $this->users->unshareTacticToAccount($tacticId, $accountId); + } + + public function getOwnerIdTacticShared(int $tactidId, int $accountId) : ?array { + $ids = []; + $idT = $this->tactics->getOwnerIdTacticSharedTeam($tactidId, $accountId); + if(isset($idT)) { + foreach ($idT as $id) { + if(!in_array($id, $ids)) { + array_push($ids, $id); + } + } + } + + $idA = $this->tactics->getOwnerIdTacticSharedAccount($tactidId, $accountId); + if(isset($idA)) { + foreach ($idA as $id) { + if(!in_array($id, $ids)) { + array_push($ids, $id); + } + } + } + + if($ids != null) { + return $ids[0]; + } + return null; + } + + /** * Update the name of a tactic * @param int $id the tactic identifier @@ -165,32 +220,4 @@ class TacticModel { } return null; } - - public function shareTacticToAccount(int $accountId, int $tacticId): int - { - return $this->users->shareTacticToAccount($accountId, $tacticId); - } - - public function shareTacticToAccountMail(string $email, int $tacticId): ?int - { - $account = $this->users->getAccountFromMail($email); - var_dump($account); - $accountId = $account->getUser()->getId(); - var_dump($accountId); - if(isset($accountId)) { - return $this->shareTacticToAccount($accountId, $tacticId); - } - return null; - } - - - public function unshareTactic(int $tacticId): int - { - return $this->users->unshareTactic($tacticId); - } - - public function unshareTacticToAccount(int $tacticId, int $accountId): int - { - return $this->users->unshareTacticToAccount($tacticId, $accountId); - } }