From a29d5ce4328c78b15e0ae6be4bf6f902f95d8c22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 16 Apr 2023 03:51:38 +0200 Subject: [PATCH] fixup! implement community basics --- src/Controller/CommunityController.php | 103 +++++++++---------------- src/Controller/HomeController.php | 8 +- views/communities/community.php | 2 +- 3 files changed, 43 insertions(+), 70 deletions(-) diff --git a/src/Controller/CommunityController.php b/src/Controller/CommunityController.php index c2d2de4..ff0bddf 100644 --- a/src/Controller/CommunityController.php +++ b/src/Controller/CommunityController.php @@ -3,9 +3,9 @@ use DateTime; use RVR\PersistentData\Model\Community; use RVR\PersistentData\Model\CommunityMember; +use RVR\PersistentData\Model\User; use RVR\Repository\CommunityRepository; use RVR\Repository\CommunityMemberRepository; -use RVR\Repository\CurrencyRepository; use RVR\Repository\UserRepository; use SokoWeb\Interfaces\Authorization\ISecured; use SokoWeb\Interfaces\Request\IRequest; @@ -26,8 +26,6 @@ class CommunityController implements ISecured private CommunityMemberRepository $communityMemberRepository; - private CurrencyRepository $currencyRepository; - public function __construct(IRequest $request) { $this->request = $request; @@ -35,7 +33,6 @@ class CommunityController implements ISecured $this->userRepository = new UserRepository(); $this->communityRepository = new CommunityRepository(); $this->communityMemberRepository = new CommunityMemberRepository(); - $this->currencyRepository = new CurrencyRepository(); } public function authorize(): bool @@ -45,30 +42,14 @@ class CommunityController implements ISecured public function getCommunityHome(): ?IContent { - $community = $this->communityRepository->getById($this->request->query('communityId')); - if ($community === null) { + if (!$this->checkPermission($this->request->query('communityId'), false, $community, $ownCommunityMember)) { return null; } - $ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user()); - if ($ownCommunityMember === null) { - return null; - } - - /*[$community, $ownCommunityMember] = $this->checkPermission($this->request->query('communityId')); - if (!$community) { - return null; - }*/ - - $currencies = $this->currencyRepository->getAllByCommunity($community); - $currencyNames = []; - foreach ($currencies as $currency) { - $currencyNames[] = $currency->getCurrency(); - } return new HtmlContent('communities/community', [ 'community' => $community, 'members' => $this->getMembers($community), - 'currencyNames' => $currencyNames, + 'currencyNames' => [], 'upcomingEvents' => [], 'editPermission' => $ownCommunityMember->getOwner() ]); @@ -79,14 +60,9 @@ class CommunityController implements ISecured return new HtmlContent('communities/community_edit'); } - public function getCommunityEdit(): IContent + public function getCommunityEdit(): ?IContent { - $community = $this->communityRepository->getById($this->request->query('communityId')); - if ($community === null) { - return null; - } - $ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user()); - if ($ownCommunityMember === null || !$ownCommunityMember->getOwner()) { + if (!$this->checkPermission($this->request->query('communityId'), true, $community, $ownCommunityMember)) { return null; } @@ -97,12 +73,7 @@ class CommunityController implements ISecured public function getMembersEdit(): ?IContent { - $community = $this->communityRepository->getById($this->request->query('communityId')); - if ($community === null) { - return null; - } - $ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user()); - if ($ownCommunityMember === null || !$ownCommunityMember->getOwner()) { + if (!$this->checkPermission($this->request->query('communityId'), true, $community, $ownCommunityMember)) { return null; } @@ -124,12 +95,7 @@ class CommunityController implements ISecured public function newMember(): ?IContent { - $community = $this->communityRepository->getById($this->request->query('communityId')); - if ($community === null) { - return null; - } - $ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user()); - if ($ownCommunityMember === null || !$ownCommunityMember->getOwner()) { + if (!$this->checkPermission($this->request->query('communityId'), true, $community, $ownCommunityMember)) { return null; } @@ -145,14 +111,10 @@ class CommunityController implements ISecured public function editMember(): ?IContent { - $community = $this->communityRepository->getById($this->request->query('communityId')); - if ($community === null) { - return null; - } - $ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user()); - if ($ownCommunityMember === null || !$ownCommunityMember->getOwner()) { + if (!$this->checkPermission($this->request->query('communityId'), true, $community, $ownCommunityMember)) { return null; } + $communityMember = $this->communityMemberRepository->getById($this->request->post('community_member_id')); if ($communityMember->getUserId() === $this->request->user()->getUniqueId()) { return new JsonContent([ @@ -160,8 +122,6 @@ class CommunityController implements ISecured ]); } - return new JsonContent(['success' => false]); - $communityMember->setOwner($this->request->post('owner')); $this->pdm->saveToDb($communityMember); @@ -170,14 +130,10 @@ class CommunityController implements ISecured public function deleteMember(): ?IContent { - $community = $this->communityRepository->getById($this->request->query('communityId')); - if ($community === null) { - return null; - } - $ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user()); - if ($ownCommunityMember === null || !$ownCommunityMember->getOwner()) { + if (!$this->checkPermission($this->request->query('communityId'), true, $community, $ownCommunityMember)) { return null; } + $communityMember = $this->communityMemberRepository->getById($this->request->post('community_member_id')); if ($communityMember->getUserId() === $this->request->user()->getUniqueId()) { return new JsonContent([ @@ -194,12 +150,7 @@ class CommunityController implements ISecured { $communityId = $this->request->query('communityId'); if ($communityId){ - $community = $this->communityRepository->getById($this->request->query('communityId')); - if ($community === null) { - return null; - } - $ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user()); - if ($ownCommunityMember === null || !$ownCommunityMember->getOwner()) { + if (!$this->checkPermission($communityId, true, $community, $ownCommunityMember)) { return null; } } else { @@ -222,9 +173,14 @@ class CommunityController implements ISecured $this->pdm->saveToDb($community); if (!$communityId) { + /** + * @var User $user + */ + $user = $this->request->user(); + $communityMember = new CommunityMember(); $communityMember->setCommunity($community); - $communityMember->setUser($this->request->user()); + $communityMember->setUser($user); $communityMember->setOwner(true); $this->pdm->saveToDb($communityMember); } @@ -234,16 +190,27 @@ class CommunityController implements ISecured ]); } - private function checkPermission(int $communityId): array + private function checkPermission( + int $communityId, + bool $needToBeOwner, + ?Community &$community, + ?CommunityMember &$ownCommunityMember): bool { $community = $this->communityRepository->getById($communityId); if ($community === null) { - return [null, null]; + return false; } - $ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user()); - if ($ownCommunityMember === null) { - return [null, null]; + + /** + * @var User $user + */ + $user = $this->request->user(); + + $ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $user); + if ($ownCommunityMember === null || ($needToBeOwner && !$ownCommunityMember->getOwner())) { + return false; } - return [$community, $ownCommunityMember]; + + return true; } } diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 112a403..7cec7b6 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -1,5 +1,6 @@ communityMemberRepository->getAllByUser($this->request->user(), true); + /** + * @var User $user + */ + $user = $this->request->user(); + + $ownCommunityMembers = $this->communityMemberRepository->getAllByUser($user, true); $communities = []; foreach ($ownCommunityMembers as $ownCommunityMember) { $communities[] = $ownCommunityMember->getCommunity(); diff --git a/views/communities/community.php b/views/communities/community.php index 9d3bf29..6cc4e5c 100644 --- a/views/communities/community.php +++ b/views/communities/community.php @@ -16,7 +16,7 @@

Currencies

-

Main currency: getCurrency() ?>

+

Main currency: getCurrency() ?>

Further currencies: