Skip to content

Commit

Permalink
adjust Form and FormMapper to new access enum
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hartmann <chris-hartmann@gmx.de>
  • Loading branch information
Chartman123 committed Apr 3, 2024
1 parent 8c12725 commit b794104
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
43 changes: 40 additions & 3 deletions lib/Db/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Form extends Entity {
protected $ownerId;
protected $fileId;
protected $fileFormat;
protected $accessJson;
protected $accessEnum;
protected $created;
protected $expires;
protected $isAnonymous;
Expand All @@ -94,12 +94,49 @@ public function __construct() {

// JSON-Decoding of access-column.
public function getAccess(): array {
return json_decode($this->getAccessJson(), true); // assoc=true, => Convert to associative Array
$accessEnum = $this->getAccessEnum();
$access = [];

if ($accessEnum > 3) {
$access['legacyLink'] = true;
}
switch ($accessEnum % 3) {
case 0:
$access['permitAllUsers'] = false;
$access['showToAllUsers'] = false;
break;
case 1:
$access['permitAllUsers'] = true;
$access['showToAllUsers'] = false;
break;
case 2:
$access['permitAllUsers'] = true;
$access['showToAllUsers'] = true;
break;
}

return $access;
}

// JSON-Encoding of access-column.
public function setAccess(array $access) {
$this->setAccessJson(json_encode($access));
// No further permissions -> 0
// Permit all users, but don't show in navigation -> 1
// Permit all users and show in navigation -> 2
if (!$access['permitAllUsers'] && !$access['showToAllUsers']) {
$value = 0;
} elseif ($access['permitAllUsers'] && !$access['showToAllUsers']) {
$value = 1;
} else {
$value = 2;
}

// If legacyLink add 3
if (isset($access['legacyLink'])) {
$value += 3;
}

$this->setAccessEnum($value);
}

// Read full form
Expand Down
4 changes: 2 additions & 2 deletions lib/Db/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ public function findPublicForms(string $userId, bool $filterShown = true): array
$qb = $this->db->getQueryBuilder();

if ($filterShown) {
$access = $qb->expr()->like('access_json', $qb->createNamedParameter('%"showToAllUsers":true%'));
$access = $qb->expr()->in('access_enum', $qb->createNamedParameter([2, 5], IQueryBuilder::PARAM_INT_ARRAY));
} else {
$access = $qb->expr()->like('access_json', $qb->createNamedParameter('%"permitAllUsers":true%'));
$access = $qb->expr()->in('access_enum', $qb->createNamedParameter([1, 4], IQueryBuilder::PARAM_INT_ARRAY));
}

$qb->select('*')
Expand Down
4 changes: 2 additions & 2 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@
</file>
<file src="lib/Db/Form.php">
<UndefinedMagicMethod>
<code>getAccessJson</code>
<code>setAccessJson</code>
<code>getAccessEnum</code>
<code>setAccessEnum</code>
</UndefinedMagicMethod>
</file>
<file src="lib/Db/Question.php">
Expand Down

0 comments on commit b794104

Please sign in to comment.