Skip to content

Commit

Permalink
Handle permission checks in LanguageRules
Browse files Browse the repository at this point in the history
For consistency with other models and to avoid duplication
  • Loading branch information
lukasbestle committed Dec 17, 2024
1 parent 9ad6808 commit b24a41c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 28 deletions.
19 changes: 0 additions & 19 deletions src/Cms/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Exception\PermissionException;
use Kirby\Filesystem\F;
use Kirby\Toolkit\Locale;
use Kirby\Toolkit\Str;
Expand Down Expand Up @@ -164,12 +163,6 @@ public static function create(array $props): static

$language = new static($props);

if ($language->permissions()->can('create') === false) {
throw new PermissionException(
key: 'language.create.permission'
);
}

// validate the new language
LanguageRules::create($language);

Expand Down Expand Up @@ -223,12 +216,6 @@ public static function create(array $props): static
*/
public function delete(): bool
{
if ($this->permissions()->can('delete') === false) {
throw new PermissionException(
key: 'language.delete.permission'
);
}

$kirby = App::instance();
$code = $this->code();

Expand Down Expand Up @@ -592,12 +579,6 @@ public function url(): string
*/
public function update(array|null $props = null): static
{
if ($this->permissions()->can('update') === false) {
throw new PermissionException(
key: 'language.update.permission'
);
}

$kirby = App::instance();

// don't change the language code
Expand Down
12 changes: 3 additions & 9 deletions src/Cms/LanguageRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public static function create(Language $language): void
);
}

$user = App::instance()->user();

if ($user?->role()->permissions()->for('languages', 'create') !== true) {
if ($language->permissions()->create() !== true) {
throw new PermissionException(
key: 'language.create.permission'
);
Expand All @@ -60,9 +58,7 @@ public static function delete(Language $language): void
);
}

$user = App::instance()->user();

if ($user?->role()->permissions()->for('languages', 'delete') !== true) {
if ($language->permissions()->delete() !== true) {
throw new PermissionException(
key: 'language.delete.permission'
);
Expand Down Expand Up @@ -93,9 +89,7 @@ public static function update(
);
}

$user = $kirby->user();

if ($user?->role()->permissions()->for('languages', 'update') !== true) {
if ($newLanguage->permissions()->update() !== true) {
throw new PermissionException(
key: 'language.update.permission'
);
Expand Down

0 comments on commit b24a41c

Please sign in to comment.