diff --git a/js/src/admin/index.js b/js/src/admin/index.js index e536298..3fe3c19 100644 --- a/js/src/admin/index.js +++ b/js/src/admin/index.js @@ -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' + ); }); diff --git a/resources/locale/en.yml b/resources/locale/en.yml index 6291180..b2790be 100644 --- a/resources/locale/en.yml +++ b/resources/locale/en.yml @@ -1,5 +1,7 @@ fof-linguist: admin: + permissions: + view_string_keys: View translation string keys tabs: strings: Translations coverage: Coverage diff --git a/src/Api/Controllers/ExportController.php b/src/Api/Controllers/ExportController.php index b1cea60..15a595c 100644 --- a/src/Api/Controllers/ExportController.php +++ b/src/Api/Controllers/ExportController.php @@ -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; @@ -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'); diff --git a/src/Api/Controllers/ImportController.php b/src/Api/Controllers/ImportController.php index c8fae77..23c9ac9 100644 --- a/src/Api/Controllers/ImportController.php +++ b/src/Api/Controllers/ImportController.php @@ -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; @@ -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'); diff --git a/src/Api/Controllers/ShowStringKeyController.php b/src/Api/Controllers/ShowStringKeyController.php index 4fed5ed..dfa05b3 100644 --- a/src/Api/Controllers/ShowStringKeyController.php +++ b/src/Api/Controllers/ShowStringKeyController.php @@ -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'); diff --git a/src/Api/Controllers/StringDeleteController.php b/src/Api/Controllers/StringDeleteController.php index d026923..7a0c164 100644 --- a/src/Api/Controllers/StringDeleteController.php +++ b/src/Api/Controllers/StringDeleteController.php @@ -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; @@ -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'); diff --git a/src/Api/Controllers/StringIndexController.php b/src/Api/Controllers/StringIndexController.php index bce7212..9636c40 100644 --- a/src/Api/Controllers/StringIndexController.php +++ b/src/Api/Controllers/StringIndexController.php @@ -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; @@ -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(); } diff --git a/src/Api/Controllers/StringKeyIndexController.php b/src/Api/Controllers/StringKeyIndexController.php index 8134447..5240a68 100644 --- a/src/Api/Controllers/StringKeyIndexController.php +++ b/src/Api/Controllers/StringKeyIndexController.php @@ -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; @@ -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; + } } } diff --git a/src/Api/Controllers/StringStoreController.php b/src/Api/Controllers/StringStoreController.php index 7b51d67..b6db7c4 100644 --- a/src/Api/Controllers/StringStoreController.php +++ b/src/Api/Controllers/StringStoreController.php @@ -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; @@ -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', []); diff --git a/src/Api/Controllers/StringUpdateController.php b/src/Api/Controllers/StringUpdateController.php index 7d0d392..4394821 100644 --- a/src/Api/Controllers/StringUpdateController.php +++ b/src/Api/Controllers/StringUpdateController.php @@ -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; @@ -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');