Skip to content

Commit

Permalink
Add prefix filter (& permission to view string keys) (#43)
Browse files Browse the repository at this point in the history
* Make it possible to filter string keys by prefix and do not require admin role in that case

* Add permission for viewing string keys

* Use `RequestUtil::getActor($request)`
  • Loading branch information
iPurpl3x authored Mar 22, 2023
1 parent 77ea048 commit 68a934b
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 10 deletions.
15 changes: 14 additions & 1 deletion js/src/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,18 @@ app.initializers.add('fof-linguist', app => {
app.store.models['fof-linguist-string-key'] = StringKey;
app.store.models['fof-linguist-string'] = TextString;

app.extensionData.for('fof-linguist').registerPage(LinguistPage);
app.extensionData
.for('fof-linguist')
.registerPage(LinguistPage)
.registerPermission(
{
icon: 'fas fa-italic',
label: app.translator.trans(
'fof-linguist.admin.permissions.view_string_keys'
),
permission: 'viewStringKeys',
allowGuest: true,
},
'view'
);
});
2 changes: 2 additions & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
fof-linguist:
admin:
permissions:
view_string_keys: View translation string keys
tabs:
strings: Translations
coverage: Coverage
Expand Down
3 changes: 2 additions & 1 deletion src/Api/Controllers/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FoF\Linguist\Api\Controllers;

use Flarum\Http\RequestUtil;
use Flarum\Locale\Translator;
use FoF\Linguist\TextString;
use Illuminate\Support\Arr;
Expand All @@ -23,7 +24,7 @@ public function __construct(Translator $translator)

public function handle(ServerRequestInterface $request): ResponseInterface
{
$request->getAttribute('actor')->assertAdmin();
RequestUtil::getActor($request)->assertAdmin();

$locale = Arr::get($request->getQueryParams(), 'locale', 'en');

Expand Down
3 changes: 2 additions & 1 deletion src/Api/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FoF\Linguist\Api\Controllers;

use Flarum\Foundation\ValidationException;
use Flarum\Http\RequestUtil;
use FoF\Linguist\Repositories\CacheStatusRepository;
use FoF\Linguist\Repositories\StringRepository;
use FoF\Linguist\TextString;
Expand All @@ -28,7 +29,7 @@ public function __construct(StringRepository $repository, CacheStatusRepository

public function handle(ServerRequestInterface $request): ResponseInterface
{
$request->getAttribute('actor')->assertAdmin();
RequestUtil::getActor($request)->assertAdmin();

$locale = Arr::get($request->getParsedBody(), 'locale', 'en');

Expand Down
2 changes: 1 addition & 1 deletion src/Api/Controllers/ShowStringKeyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(DefaultStringsRepository $repository)

protected function data(ServerRequestInterface $request, Document $document)
{
RequestUtil::getActor($request)->assertAdmin();
RequestUtil::getActor($request)->assertCan('viewStringKeys');

$key = Arr::get($request->getQueryParams(), 'key');

Expand Down
3 changes: 2 additions & 1 deletion src/Api/Controllers/StringDeleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use FoF\Linguist\Repositories\StringRepository;
use Flarum\Api\Controller\AbstractDeleteController;
use Flarum\Http\RequestUtil;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;

Expand All @@ -18,7 +19,7 @@ public function __construct(StringRepository $strings)

protected function delete(ServerRequestInterface $request)
{
$request->getAttribute('actor')->assertAdmin();
RequestUtil::getActor($request)->assertAdmin();

$id = Arr::get($request->getQueryParams(), 'id');

Expand Down
3 changes: 2 additions & 1 deletion src/Api/Controllers/StringIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use FoF\Linguist\Api\Serializers\StringSerializer;
use FoF\Linguist\Repositories\StringRepository;
use Flarum\Api\Controller\AbstractListController;
use Flarum\Http\RequestUtil;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

Expand All @@ -21,7 +22,7 @@ public function __construct(StringRepository $strings)

protected function data(ServerRequestInterface $request, Document $document)
{
$request->getAttribute('actor')->assertAdmin();
RequestUtil::getActor($request)->assertAdmin();

return $this->strings->all();
}
Expand Down
33 changes: 31 additions & 2 deletions src/Api/Controllers/StringKeyIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use FoF\Linguist\Api\Serializers\StringKeySerializer;
use FoF\Linguist\Repositories\DefaultStringsRepository;
use Flarum\Api\Controller\AbstractListController;
use Flarum\Http\RequestUtil;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

Expand All @@ -19,10 +23,35 @@ public function __construct(DefaultStringsRepository $repository)
$this->repository = $repository;
}

/**
* Retrieve and optionally filter translation string keys.
*
* @param ServerRequestInterface $request The server request containing filters.
* @param Document $document The document to store the translation data.
* @return Collection The collection of filtered translations.
*/
protected function data(ServerRequestInterface $request, Document $document)
{
$request->getAttribute('actor')->assertAdmin();
RequestUtil::getActor($request)->assertCan('viewStringKeys');

return $this->repository->allTranslations();
// Extract filters from the request.
$filters = $this->extractFilter($request);

// Look for the 'prefix' key in the filters.
$prefix = Arr::get($filters, 'prefix', null);

// Retrieve all translations from the repository.
$all = $this->repository->allTranslations();

// If a prefix is provided, filter the translations by that prefix.
if ($prefix) {
return $all->filter(function ($item) use ($prefix) {
// Return true if the item's key starts with the provided prefix.
return Str::startsWith($item['key'], $prefix);
});
} else {
// If no prefix is provided, return all translations.
return $all;
}
}
}
3 changes: 2 additions & 1 deletion src/Api/Controllers/StringStoreController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use FoF\Linguist\Api\Serializers\StringSerializer;
use FoF\Linguist\Repositories\StringRepository;
use Flarum\Api\Controller\AbstractCreateController;
use Flarum\Http\RequestUtil;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
Expand All @@ -25,7 +26,7 @@ public function __construct(StringRepository $strings)

protected function data(ServerRequestInterface $request, Document $document)
{
$request->getAttribute('actor')->assertAdmin();
RequestUtil::getActor($request)->assertAdmin();

$attributes = Arr::get($request->getParsedBody(), 'data.attributes', []);

Expand Down
3 changes: 2 additions & 1 deletion src/Api/Controllers/StringUpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use FoF\Linguist\Api\Serializers\StringSerializer;
use FoF\Linguist\Repositories\StringRepository;
use Flarum\Api\Controller\AbstractShowController;
use Flarum\Http\RequestUtil;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
Expand All @@ -25,7 +26,7 @@ public function __construct(StringRepository $strings)

protected function data(ServerRequestInterface $request, Document $document)
{
$request->getAttribute('actor')->assertAdmin();
RequestUtil::getActor($request)->assertAdmin();

$id = Arr::get($request->getQueryParams(), 'id');

Expand Down

0 comments on commit 68a934b

Please sign in to comment.