Skip to content

Commit

Permalink
[FEATURE] Respect permissions for receiver groups
Browse files Browse the repository at this point in the history
  • Loading branch information
einpraegsam committed Mar 5, 2024
1 parent 9602754 commit 572c28c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
20 changes: 11 additions & 9 deletions Classes/Domain/Repository/UsergroupRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
declare(strict_types=1);
namespace In2code\Luxletter\Domain\Repository;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception as ExceptionDbal;
use In2code\Luxletter\Domain\Model\Usergroup;
use In2code\Luxletter\Domain\Service\PermissionTrait;
use In2code\Luxletter\Exception\MisconfigurationException;
use In2code\Luxletter\Utility\ArrayUtility;
use In2code\Luxletter\Utility\DatabaseUtility;

class UsergroupRepository extends AbstractRepository
{
use PermissionTrait;

public function findByIdentifiersAndKeepOrderings(array $usergroupIdentifiers): array
{
$result = [];
Expand Down Expand Up @@ -37,21 +41,19 @@ public function findByIdentifiersAndKeepOrderings(array $usergroupIdentifiers):
* ]
*
* @return array
* @throws DBALException
* @throws ExceptionDbal
* @throws MisconfigurationException
*/
public function getReceiverGroups(): array
{
$groups = [];
$queryBuilder = DatabaseUtility::getQueryBuilderForTable(Usergroup::TABLE_NAME);
$statement = $queryBuilder
$groups = $queryBuilder
->select('uid', 'title')
->from(Usergroup::TABLE_NAME)
->where('luxletter_receiver=1')
->orderBy('title', 'ASC')
->executeQuery();
while ($row = $statement->fetch()) {
$groups[$row['uid']] = $row['title'];
}
return $groups;
->executeQuery()
->fetchAllKeyValue();
return $this->filterRecords($groups, Usergroup::TABLE_NAME);
}
}
26 changes: 25 additions & 1 deletion Classes/Domain/Service/PermissionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
namespace In2code\Luxletter\Domain\Service;

use Doctrine\DBAL\Exception as ExceptionDbal;
use In2code\Luxletter\Exception\MisconfigurationException;
use In2code\Luxletter\Utility\BackendUserUtility;
use In2code\Luxletter\Utility\DatabaseUtility;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;

trait PermissionTrait
{
Expand All @@ -18,6 +21,7 @@ trait PermissionTrait
* @param string $table
* @return array
* @throws ExceptionDbal
* @throws MisconfigurationException
*/
private function filterRecords(array $rows, string $table): array
{
Expand All @@ -26,14 +30,34 @@ private function filterRecords(array $rows, string $table): array
}

foreach ($rows as $key => $row) {
$identifier = is_array($row) ? $row['uid'] : $row->getUid();
$identifier = $this->getIdentifierFromArrayOrObject($row, $key);
if ($this->isAuthenticatedForRecord($identifier, $table) === false) {
unset($rows[$key]);
}
}
return $rows;
}

/**
* @param $object
* @param $key
* @return int
* @throws MisconfigurationException
*/
protected function getIdentifierFromArrayOrObject($object, $key): int
{
if (is_array($object)) { // AllAssociative
if (array_key_exists('uid', $object)) {
return $object['uid'];
}
} elseif (is_string($object) || is_int($object)) { // KeyValue
return (int)$key;
} elseif (is_a($object, AbstractEntity::class)) { // DomainObject
return $object->getUid();
}
throw new MisconfigurationException('Object not supported in ' . __CLASS__, 1709566644);
}

/**
* @param int $identifier
* @param string $table
Expand Down

0 comments on commit 572c28c

Please sign in to comment.