Flex: Control authorization from the directory

This commit is contained in:
Matias Griese
2020-01-27 15:35:07 +02:00
parent 5cd4bf5c98
commit ede7af6b9d
2 changed files with 23 additions and 12 deletions

View File

@@ -70,6 +70,9 @@ class FlexDirectory implements FlexAuthorizeInterface
/** @var string */
protected $indexClassName;
/** @var string|null */
private $_authorize;
/**
* FlexDirectory constructor.
* @param string $type
@@ -955,4 +958,23 @@ class FlexDirectory implements FlexAuthorizeInterface
{
return isset(Grav::instance()['admin']) ? 'admin' : 'site';
}
/**
* @param string $scope
* @param string $action
* @return string
*/
public function getAuthorizeRule(string $scope, string $action): string
{
if (!$this->_authorize) {
$config = $this->getConfig('admin.permissions');
if ($config) {
$this->_authorize = array_key_first($config) . '.%2$s';
} else {
$this->_authorize = '%1$s.flex-object.%2$s';
}
}
return sprintf($this->_authorize, $scope, $action);
}
}

View File

@@ -20,9 +20,6 @@ use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
*/
trait FlexAuthorizeTrait
{
/** @var string */
private $_authorize = '%s.flex-object.%s';
/**
* Check if user is authorized for the action.
*
@@ -113,14 +110,6 @@ trait FlexAuthorizeTrait
return $user->authorize('admin.super');
}
/**
* @param string $authorize
*/
protected function setAuthorizeRule(string $authorize): void
{
$this->_authorize = $authorize;
}
/**
* @param string $scope
* @param string $action
@@ -128,6 +117,6 @@ trait FlexAuthorizeTrait
*/
protected function getAuthorizeRule(string $scope, string $action): string
{
return sprintf($this->_authorize, $scope, $action);
return $this->getFlexDirectory()->getAuthorizeRule($scope, $action);
}
}