julienmonnerie/kirby/src/Cms/ModelPermissions.php

133 lines
2.7 KiB
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
namespace Kirby\Cms;
use Kirby\Toolkit\A;
/**
* ModelPermissions
*
* @package Kirby Cms
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*/
abstract class ModelPermissions
{
2025-04-21 18:57:21 +02:00
protected string $category;
protected ModelWithContent $model;
protected array $options;
protected Permissions $permissions;
protected User $user;
2022-08-31 15:02:43 +02:00
2025-04-21 18:57:21 +02:00
public function __construct(ModelWithContent $model)
2022-08-31 15:02:43 +02:00
{
$this->model = $model;
$this->options = $model->blueprint()->options();
$this->user = $model->kirby()->user() ?? User::nobody();
$this->permissions = $this->user->role()->permissions();
}
2025-04-21 18:57:21 +02:00
public function __call(string $method, array $arguments = []): bool
{
return $this->can($method);
}
2022-08-31 15:02:43 +02:00
/**
* Improved `var_dump` output
2025-04-21 18:57:21 +02:00
* @codeCoverageIgnore
2022-08-31 15:02:43 +02:00
*/
public function __debugInfo(): array
{
return $this->toArray();
}
/**
2025-04-21 18:57:21 +02:00
* Returns whether the current user is allowed to do
* a certain action on the model
*
* @param bool $default Will be returned if $action does not exist
2022-08-31 15:02:43 +02:00
*/
2025-04-21 18:57:21 +02:00
public function can(
string $action,
bool $default = false
): bool {
$user = $this->user->id();
2022-08-31 15:02:43 +02:00
$role = $this->user->role()->id();
2025-04-21 18:57:21 +02:00
// users with the `nobody` role can do nothing
// that needs a permission check
2022-08-31 15:02:43 +02:00
if ($role === 'nobody') {
return false;
}
2025-04-21 18:57:21 +02:00
// check for a custom `can` method
// which would take priority over any other
// role-based permission rules
2022-12-19 14:56:05 +01:00
if (
method_exists($this, 'can' . $action) === true &&
$this->{'can' . $action}() === false
) {
2022-08-31 15:02:43 +02:00
return false;
}
2025-04-21 18:57:21 +02:00
// the almighty `kirby` user can do anything
if ($user === 'kirby' && $role === 'admin') {
return true;
}
2022-08-31 15:02:43 +02:00
// evaluate the blueprint options block
if (isset($this->options[$action]) === true) {
$options = $this->options[$action];
if ($options === false) {
return false;
}
if ($options === true) {
return true;
}
2022-12-19 14:56:05 +01:00
if (
is_array($options) === true &&
A::isAssociative($options) === true
) {
2025-04-21 18:57:21 +02:00
if (isset($options[$role]) === true) {
return $options[$role];
}
if (isset($options['*']) === true) {
return $options['*'];
}
2022-08-31 15:02:43 +02:00
}
}
2025-04-21 18:57:21 +02:00
return $this->permissions->for($this->category, $action, $default);
2022-08-31 15:02:43 +02:00
}
/**
2025-04-21 18:57:21 +02:00
* Returns whether the current user is not allowed to do
* a certain action on the model
*
* @param bool $default Will be returned if $action does not exist
2022-08-31 15:02:43 +02:00
*/
2025-04-21 18:57:21 +02:00
public function cannot(
string $action,
bool $default = true
): bool {
return $this->can($action, !$default) === false;
2022-08-31 15:02:43 +02:00
}
public function toArray(): array
{
$array = [];
foreach ($this->options as $key => $value) {
$array[$key] = $this->can($key);
}
return $array;
}
2022-06-17 17:51:59 +02:00
}