Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access permissions 3: Smaller refactorings #6879

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/sections/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
'computed' => [
'form' => function () {
$fields = $this->fields;
$disabled = $this->model->permissions()->update() === false;
$disabled = $this->model->permissions()->cannot('update');
$lang = $this->model->kirby()->languageCode();
$content = $this->model->content($lang)->toArray();

Expand Down
14 changes: 7 additions & 7 deletions src/Cms/FileRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FileRules
*/
public static function changeName(File $file, string $name): void
{
if ($file->permissions()->changeName() !== true) {
if ($file->permissions()->can('changeName') !== true) {
throw new PermissionException(
key: 'file.changeName.permission',
data: ['filename' => $file->filename()]
Expand Down Expand Up @@ -58,7 +58,7 @@ public static function changeName(File $file, string $name): void
*/
public static function changeSort(File $file, int $sort): void
{
if ($file->permissions()->sort() !== true) {
if ($file->permissions()->can('sort') !== true) {
throw new PermissionException(
key: 'file.sort.permission',
data: ['filename' => $file->filename()]
Expand All @@ -74,7 +74,7 @@ public static function changeSort(File $file, int $sort): void
*/
public static function changeTemplate(File $file, string $template): void
{
if ($file->permissions()->changeTemplate() !== true) {
if ($file->permissions()->can('changeTemplate') !== true) {
throw new PermissionException(
key: 'file.changeTemplate.permission',
data: ['id' => $file->id()]
Expand Down Expand Up @@ -134,7 +134,7 @@ public static function create(File $file, BaseFile $upload): void
);
}

if ($file->permissions()->create() !== true) {
if ($file->permissions()->can('create') !== true) {
throw new PermissionException(
message: 'The file cannot be created'
);
Expand All @@ -153,7 +153,7 @@ public static function create(File $file, BaseFile $upload): void
*/
public static function delete(File $file): void
{
if ($file->permissions()->delete() !== true) {
if ($file->permissions()->can('delete') !== true) {
throw new PermissionException(
message: 'The file cannot be deleted'
);
Expand All @@ -168,7 +168,7 @@ public static function delete(File $file): void
*/
public static function replace(File $file, BaseFile $upload): void
{
if ($file->permissions()->replace() !== true) {
if ($file->permissions()->can('replace') !== true) {
throw new PermissionException(
message: 'The file cannot be replaced'
);
Expand Down Expand Up @@ -197,7 +197,7 @@ public static function replace(File $file, BaseFile $upload): void
*/
public static function update(File $file, array $content = []): void
{
if ($file->permissions()->update() !== true) {
if ($file->permissions()->can('update') !== true) {
throw new PermissionException(
message: 'The file cannot be updated'
);
Expand Down
6 changes: 3 additions & 3 deletions src/Cms/LanguageRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function create(Language $language): void
);
}

if ($language->permissions()->create() !== true) {
if ($language->permissions()->can('create') !== true) {
throw new PermissionException(
key: 'language.create.permission'
);
Expand All @@ -52,7 +52,7 @@ public static function create(Language $language): void
*/
public static function delete(Language $language): void
{
if ($language->permissions()->delete() !== true) {
if ($language->permissions()->can('delete') !== true) {
throw new PermissionException(
key: 'language.delete.permission'
);
Expand Down Expand Up @@ -83,7 +83,7 @@ public static function update(
);
}

if ($newLanguage->permissions()->update() !== true) {
if ($newLanguage->permissions()->can('update') !== true) {
throw new PermissionException(
key: 'language.update.permission'
);
Expand Down
4 changes: 1 addition & 3 deletions src/Cms/ModelPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
abstract class ModelPermissions
{
protected string $category;
protected ModelWithContent|Language $model;
protected array $options;
protected Permissions $permissions;
protected User $user;

public function __construct(ModelWithContent|Language $model)
public function __construct(protected ModelWithContent|Language $model)
{
$this->model = $model;
$this->user = $model->kirby()->user() ?? User::nobody();
$this->permissions = $this->user->role()->permissions();

Expand Down
20 changes: 10 additions & 10 deletions src/Cms/PageRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function changeNum(Page $page, int|null $num = null): void
*/
public static function changeSlug(Page $page, string $slug): void
{
if ($page->permissions()->changeSlug() !== true) {
if ($page->permissions()->can('changeSlug') !== true) {
throw new PermissionException(
key: 'page.changeSlug.permission',
data: ['slug' => $page->slug()]
Expand Down Expand Up @@ -99,7 +99,7 @@ public static function changeStatus(
*/
public static function changeStatusToDraft(Page $page): void
{
if ($page->permissions()->changeStatus() !== true) {
if ($page->permissions()->can('changeStatus') !== true) {
throw new PermissionException(
key: 'page.changeStatus.permission',
data: ['slug' => $page->slug()]
Expand Down Expand Up @@ -160,7 +160,7 @@ public static function changeStatusToUnlisted(Page $page)
*/
public static function changeTemplate(Page $page, string $template): void
{
if ($page->permissions()->changeTemplate() !== true) {
if ($page->permissions()->can('changeTemplate') !== true) {
throw new PermissionException(
key: 'page.changeTemplate.permission',
data: ['slug' => $page->slug()]
Expand Down Expand Up @@ -188,7 +188,7 @@ public static function changeTemplate(Page $page, string $template): void
*/
public static function changeTitle(Page $page, string $title): void
{
if ($page->permissions()->changeTitle() !== true) {
if ($page->permissions()->can('changeTitle') !== true) {
throw new PermissionException(
key: 'page.changeTitle.permission',
data: ['slug' => $page->slug()]
Expand All @@ -207,7 +207,7 @@ public static function changeTitle(Page $page, string $title): void
*/
public static function create(Page $page): void
{
if ($page->permissions()->create() !== true) {
if ($page->permissions()->can('create') !== true) {
throw new PermissionException(
key: 'page.create.permission',
data: ['slug' => $page->slug()]
Expand Down Expand Up @@ -251,7 +251,7 @@ public static function create(Page $page): void
*/
public static function delete(Page $page, bool $force = false): void
{
if ($page->permissions()->delete() !== true) {
if ($page->permissions()->can('delete') !== true) {
throw new PermissionException(
key: 'page.delete.permission',
data: ['slug' => $page->slug()]
Expand All @@ -273,7 +273,7 @@ public static function duplicate(
string $slug,
array $options = []
): void {
if ($page->permissions()->duplicate() !== true) {
if ($page->permissions()->can('duplicate') !== true) {
throw new PermissionException(
key: 'page.duplicate.permission',
data: ['slug' => $page->slug()]
Expand All @@ -294,7 +294,7 @@ public static function move(Page $page, Site|Page $parent): void
return;
}

if ($page->permissions()->move() !== true) {
if ($page->permissions()->can('move') !== true) {
throw new PermissionException(
key: 'page.move.permission',
data: ['slug' => $page->slug()]
Expand Down Expand Up @@ -373,7 +373,7 @@ public static function move(Page $page, Site|Page $parent): void
*/
public static function publish(Page $page): void
{
if ($page->permissions()->changeStatus() !== true) {
if ($page->permissions()->can('changeStatus') !== true) {
throw new PermissionException(
key: 'page.changeStatus.permission',
data: [
Expand All @@ -397,7 +397,7 @@ public static function publish(Page $page): void
*/
public static function update(Page $page, array $content = []): void
{
if ($page->permissions()->update() !== true) {
if ($page->permissions()->can('update') !== true) {
throw new PermissionException(
key: 'page.update.permission',
data: ['slug' => $page->slug()]
Expand Down
4 changes: 2 additions & 2 deletions src/Cms/SiteRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SiteRules
*/
public static function changeTitle(Site $site, string $title): void
{
if ($site->permissions()->changeTitle() !== true) {
if ($site->permissions()->can('changeTitle') !== true) {
throw new PermissionException(
key: 'site.changeTitle.permission'
);
Expand All @@ -45,7 +45,7 @@ public static function changeTitle(Site $site, string $title): void
*/
public static function update(Site $site, array $content = []): void
{
if ($site->permissions()->update() !== true) {
if ($site->permissions()->can('update') !== true) {
throw new PermissionException(
key: 'site.update.permission'
);
Expand Down
16 changes: 8 additions & 8 deletions src/Cms/UserRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class UserRules
*/
public static function changeEmail(User $user, string $email): void
{
if ($user->permissions()->changeEmail() !== true) {
if ($user->permissions()->can('changeEmail') !== true) {
throw new PermissionException(
key: 'user.changeEmail.permission',
data: ['name' => $user->username()]
Expand All @@ -46,7 +46,7 @@ public static function changeEmail(User $user, string $email): void
*/
public static function changeLanguage(User $user, string $language): void
{
if ($user->permissions()->changeLanguage() !== true) {
if ($user->permissions()->can('changeLanguage') !== true) {
throw new PermissionException(
key: 'user.changeLanguage.permission',
data: ['name' => $user->username()]
Expand All @@ -63,7 +63,7 @@ public static function changeLanguage(User $user, string $language): void
*/
public static function changeName(User $user, string $name): void
{
if ($user->permissions()->changeName() !== true) {
if ($user->permissions()->can('changeName') !== true) {
throw new PermissionException(
key: 'user.changeName.permission',
data: ['name' => $user->username()]
Expand All @@ -81,7 +81,7 @@ public static function changePassword(
#[SensitiveParameter]
string $password
): void {
if ($user->permissions()->changePassword() !== true) {
if ($user->permissions()->can('changePassword') !== true) {
throw new PermissionException(
key: 'user.changePassword.permission',
data: ['name' => $user->username()]
Expand Down Expand Up @@ -118,7 +118,7 @@ public static function changeRole(User $user, string $role): void
}

// check permissions
if ($user->permissions()->changeRole() !== true) {
if ($user->permissions()->can('changeRole') !== true) {
throw new PermissionException(
key: 'user.changeRole.permission',
data: ['name' => $user->username()]
Expand Down Expand Up @@ -197,7 +197,7 @@ public static function create(User $user, array $props = []): void
}

// check user permissions
if ($user->permissions()->create() !== true) {
if ($user->permissions()->can('create') !== true) {
throw new PermissionException([
'key' => 'user.create.permission'
]);
Expand Down Expand Up @@ -236,7 +236,7 @@ public static function delete(User $user): void
);
}

if ($user->permissions()->delete() !== true) {
if ($user->permissions()->can('delete') !== true) {
throw new PermissionException(
key: 'user.delete.permission',
data: ['name' => $user->username()]
Expand All @@ -254,7 +254,7 @@ public static function update(
array $values = [],
array $strings = []
): void {
if ($user->permissions()->update() !== true) {
if ($user->permissions()->can('update') !== true) {
throw new PermissionException(
key: 'user.update.permission',
data: ['name' => $user->username()]
Expand Down
Loading
Loading