fixup! implement community basics
All checks were successful
rvr-nextgen/pipeline/pr-master This commit looks good

This commit is contained in:
Bence Pőcze 2023-04-16 03:51:38 +02:00
parent 96b9ef6a72
commit a29d5ce432
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D
3 changed files with 43 additions and 70 deletions

View File

@ -3,9 +3,9 @@
use DateTime; use DateTime;
use RVR\PersistentData\Model\Community; use RVR\PersistentData\Model\Community;
use RVR\PersistentData\Model\CommunityMember; use RVR\PersistentData\Model\CommunityMember;
use RVR\PersistentData\Model\User;
use RVR\Repository\CommunityRepository; use RVR\Repository\CommunityRepository;
use RVR\Repository\CommunityMemberRepository; use RVR\Repository\CommunityMemberRepository;
use RVR\Repository\CurrencyRepository;
use RVR\Repository\UserRepository; use RVR\Repository\UserRepository;
use SokoWeb\Interfaces\Authorization\ISecured; use SokoWeb\Interfaces\Authorization\ISecured;
use SokoWeb\Interfaces\Request\IRequest; use SokoWeb\Interfaces\Request\IRequest;
@ -26,8 +26,6 @@ class CommunityController implements ISecured
private CommunityMemberRepository $communityMemberRepository; private CommunityMemberRepository $communityMemberRepository;
private CurrencyRepository $currencyRepository;
public function __construct(IRequest $request) public function __construct(IRequest $request)
{ {
$this->request = $request; $this->request = $request;
@ -35,7 +33,6 @@ class CommunityController implements ISecured
$this->userRepository = new UserRepository(); $this->userRepository = new UserRepository();
$this->communityRepository = new CommunityRepository(); $this->communityRepository = new CommunityRepository();
$this->communityMemberRepository = new CommunityMemberRepository(); $this->communityMemberRepository = new CommunityMemberRepository();
$this->currencyRepository = new CurrencyRepository();
} }
public function authorize(): bool public function authorize(): bool
@ -45,30 +42,14 @@ class CommunityController implements ISecured
public function getCommunityHome(): ?IContent public function getCommunityHome(): ?IContent
{ {
$community = $this->communityRepository->getById($this->request->query('communityId')); if (!$this->checkPermission($this->request->query('communityId'), false, $community, $ownCommunityMember)) {
if ($community === null) {
return null; 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', [ return new HtmlContent('communities/community', [
'community' => $community, 'community' => $community,
'members' => $this->getMembers($community), 'members' => $this->getMembers($community),
'currencyNames' => $currencyNames, 'currencyNames' => [],
'upcomingEvents' => [], 'upcomingEvents' => [],
'editPermission' => $ownCommunityMember->getOwner() 'editPermission' => $ownCommunityMember->getOwner()
]); ]);
@ -79,14 +60,9 @@ class CommunityController implements ISecured
return new HtmlContent('communities/community_edit'); return new HtmlContent('communities/community_edit');
} }
public function getCommunityEdit(): IContent public function getCommunityEdit(): ?IContent
{ {
$community = $this->communityRepository->getById($this->request->query('communityId')); if (!$this->checkPermission($this->request->query('communityId'), true, $community, $ownCommunityMember)) {
if ($community === null) {
return null;
}
$ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user());
if ($ownCommunityMember === null || !$ownCommunityMember->getOwner()) {
return null; return null;
} }
@ -97,12 +73,7 @@ class CommunityController implements ISecured
public function getMembersEdit(): ?IContent public function getMembersEdit(): ?IContent
{ {
$community = $this->communityRepository->getById($this->request->query('communityId')); if (!$this->checkPermission($this->request->query('communityId'), true, $community, $ownCommunityMember)) {
if ($community === null) {
return null;
}
$ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user());
if ($ownCommunityMember === null || !$ownCommunityMember->getOwner()) {
return null; return null;
} }
@ -124,12 +95,7 @@ class CommunityController implements ISecured
public function newMember(): ?IContent public function newMember(): ?IContent
{ {
$community = $this->communityRepository->getById($this->request->query('communityId')); if (!$this->checkPermission($this->request->query('communityId'), true, $community, $ownCommunityMember)) {
if ($community === null) {
return null;
}
$ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user());
if ($ownCommunityMember === null || !$ownCommunityMember->getOwner()) {
return null; return null;
} }
@ -145,14 +111,10 @@ class CommunityController implements ISecured
public function editMember(): ?IContent public function editMember(): ?IContent
{ {
$community = $this->communityRepository->getById($this->request->query('communityId')); if (!$this->checkPermission($this->request->query('communityId'), true, $community, $ownCommunityMember)) {
if ($community === null) {
return null;
}
$ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user());
if ($ownCommunityMember === null || !$ownCommunityMember->getOwner()) {
return null; return null;
} }
$communityMember = $this->communityMemberRepository->getById($this->request->post('community_member_id')); $communityMember = $this->communityMemberRepository->getById($this->request->post('community_member_id'));
if ($communityMember->getUserId() === $this->request->user()->getUniqueId()) { if ($communityMember->getUserId() === $this->request->user()->getUniqueId()) {
return new JsonContent([ return new JsonContent([
@ -160,8 +122,6 @@ class CommunityController implements ISecured
]); ]);
} }
return new JsonContent(['success' => false]);
$communityMember->setOwner($this->request->post('owner')); $communityMember->setOwner($this->request->post('owner'));
$this->pdm->saveToDb($communityMember); $this->pdm->saveToDb($communityMember);
@ -170,14 +130,10 @@ class CommunityController implements ISecured
public function deleteMember(): ?IContent public function deleteMember(): ?IContent
{ {
$community = $this->communityRepository->getById($this->request->query('communityId')); if (!$this->checkPermission($this->request->query('communityId'), true, $community, $ownCommunityMember)) {
if ($community === null) {
return null;
}
$ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user());
if ($ownCommunityMember === null || !$ownCommunityMember->getOwner()) {
return null; return null;
} }
$communityMember = $this->communityMemberRepository->getById($this->request->post('community_member_id')); $communityMember = $this->communityMemberRepository->getById($this->request->post('community_member_id'));
if ($communityMember->getUserId() === $this->request->user()->getUniqueId()) { if ($communityMember->getUserId() === $this->request->user()->getUniqueId()) {
return new JsonContent([ return new JsonContent([
@ -194,12 +150,7 @@ class CommunityController implements ISecured
{ {
$communityId = $this->request->query('communityId'); $communityId = $this->request->query('communityId');
if ($communityId){ if ($communityId){
$community = $this->communityRepository->getById($this->request->query('communityId')); if (!$this->checkPermission($communityId, true, $community, $ownCommunityMember)) {
if ($community === null) {
return null;
}
$ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $this->request->user());
if ($ownCommunityMember === null || !$ownCommunityMember->getOwner()) {
return null; return null;
} }
} else { } else {
@ -222,9 +173,14 @@ class CommunityController implements ISecured
$this->pdm->saveToDb($community); $this->pdm->saveToDb($community);
if (!$communityId) { if (!$communityId) {
/**
* @var User $user
*/
$user = $this->request->user();
$communityMember = new CommunityMember(); $communityMember = new CommunityMember();
$communityMember->setCommunity($community); $communityMember->setCommunity($community);
$communityMember->setUser($this->request->user()); $communityMember->setUser($user);
$communityMember->setOwner(true); $communityMember->setOwner(true);
$this->pdm->saveToDb($communityMember); $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); $community = $this->communityRepository->getById($communityId);
if ($community === null) { 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;
} }
} }

View File

@ -1,5 +1,6 @@
<?php namespace RVR\Controller; <?php namespace RVR\Controller;
use RVR\PersistentData\Model\User;
use RVR\Repository\CommunityMemberRepository; use RVR\Repository\CommunityMemberRepository;
use SokoWeb\Interfaces\Authorization\ISecured; use SokoWeb\Interfaces\Authorization\ISecured;
use SokoWeb\Interfaces\Request\IRequest; use SokoWeb\Interfaces\Request\IRequest;
@ -25,7 +26,12 @@ class HomeController implements ISecured
public function getHome(): IContent public function getHome(): IContent
{ {
$ownCommunityMembers = $this->communityMemberRepository->getAllByUser($this->request->user(), true); /**
* @var User $user
*/
$user = $this->request->user();
$ownCommunityMembers = $this->communityMemberRepository->getAllByUser($user, true);
$communities = []; $communities = [];
foreach ($ownCommunityMembers as $ownCommunityMember) { foreach ($ownCommunityMembers as $ownCommunityMember) {
$communities[] = $ownCommunityMember->getCommunity(); $communities[] = $ownCommunityMember->getCommunity();

View File

@ -16,7 +16,7 @@
</div> </div>
<div> <div>
<h3 class="marginBottom">Currencies</h3> <h3 class="marginBottom">Currencies</h3>
<p class="marginBottom">Main currency: <b><?= $community->getCurrency() ?></b></p> <p>Main currency: <b><?= $community->getCurrency() ?></b></p>
<p>Further currencies: <b><?= implode(', ', $currencyNames) ?></b></p> <p>Further currencies: <b><?= implode(', ', $currencyNames) ?></b></p>
</div> </div>
<div> <div>