praiadeseselle/wire/core/Permission.php

150 lines
4.2 KiB
PHP
Raw Normal View History

2022-03-08 15:55:41 +01:00
<?php namespace ProcessWire;
/**
* ProcessWire Permission Page
*
* #pw-summary Permission is a Page type used for storing an individual permission.
* #pw-body =
* One or more Permission objects are attached to `Role` objects, which are then attached to `User` objects
* and `Template` objects, forming ProcessWire's role-based access control system. Outside of roles,
* Permission objects are managed with the `$permissions` API variable.
* #pw-body
*
* @property int $id Numeric page ID of the permission.
* @property string $name Name of the permission.
* @property string $title Short description of what the permission is for.
*
2023-03-10 19:41:40 +01:00
* ProcessWire 3.x, Copyright 2022 by Ryan Cramer
2022-03-08 15:55:41 +01:00
* https://processwire.com
*
*/
class Permission extends Page {
/**
* Static relations between permissions
*
* All other relations follow the name format (i.e. page-edit-created assumes page-edit as parent)
*
* @var array
*
*/
static protected $parentPermissions = array(
'page-view' => 'none',
'page-lister' => 'none',
'page-edit' => 'none',
'user-admin' => 'page-edit',
'lang-edit' => 'page-edit',
'page-lock' => 'page-edit',
'page-hide' => 'page-edit',
'page-clone' => 'page-edit',
'page-move' => 'page-edit',
'page-template' => 'page-edit',
'page-sort' => 'page-edit',
'page' => 'page-edit', // all page-* permissions
2023-03-10 19:41:40 +01:00
);
2022-03-08 15:55:41 +01:00
/**
* Create a new Permission page in memory.
*
* @param Template $tpl Template object this page should use.
*
*/
public function __construct(Template $tpl = null) {
parent::__construct($tpl);
if(!$tpl) $this->template = $this->wire()->templates->get('permission');
$this->_parent_id = $this->wire()->config->permissionsPageID;
}
/**
* Wired to API
*
*/
public function wired() {
parent::wired();
$template = $this->wire()->templates->get('permission');
if($template !== $this->template && (!$this->template || $this->template->name === 'permission')) $this->template = $template;
$this->_parent_id = $this->wire()->config->permissionsPageID;
}
/**
* Return the immediate parent permission of this permission or NullPage if no parent permission.
*
* For permissions, parents relations are typically by name. For instance, page-edit is the parent of page-edit-created.
* But all page-* permissions are assumed to have page-edit as parent, except for page-view.
*
* #pw-internal
*
* @return Permission|NullPage
*
*/
public function getParentPermission() {
$name = $this->name;
2023-03-10 19:41:40 +01:00
$permissions = $this->wire()->permissions;
2022-03-08 15:55:41 +01:00
$permission = null;
do {
// first check if we have a static definition for this permission
if(isset(self::$parentPermissions[$name])) {
$parentName = self::$parentPermissions[$name];
if($parentName == 'none') break; // NullPage
$permission = $permissions->get($parentName);
if($permission->id) break;
}
// reduce permission by one part, to a potential parent name
$parts = explode('-', $name);
array_pop($parts);
if(!count($parts)) break;
$name = implode('-', $parts);
$permission = $permissions->get($name);
} while(!$permission->id);
2023-03-10 19:41:40 +01:00
if(is_null($permission)) $permission = $this->wire()->pages->newNullPage();
2022-03-08 15:55:41 +01:00
return $permission;
}
/**
* Get the root parent permission
*
* #pw-internal
*
* @return Permission|NullPage
* @throws WireException
*
*/
public function getRootParentPermission() {
2023-03-10 19:41:40 +01:00
$permissions = $this->wire()->permissions;
$pages = $this->wire()->pages;
2022-03-08 15:55:41 +01:00
if(isset(self::$parentPermissions[$this->name])) {
$name = self::$parentPermissions[$this->name];
2023-03-10 19:41:40 +01:00
if($name == 'none') return $pages->newNullPage();
2022-03-08 15:55:41 +01:00
}
$parts = explode('-', $this->name);
2023-03-10 19:41:40 +01:00
if(count($parts) < 2) return $pages->newNullPage();
2022-03-08 15:55:41 +01:00
$name = "$parts[0]-$parts[1]";
if(isset(self::$parentPermissions[$name])) {
$name = self::$parentPermissions[$name];
2023-03-10 19:41:40 +01:00
if($name == 'none') return $pages->newNullPage();
return $permissions->get($name);
2022-03-08 15:55:41 +01:00
}
if($parts[0] == 'page') $name = 'page-edit';
2023-03-10 19:41:40 +01:00
return $permissions->get($name);
2022-03-08 15:55:41 +01:00
}
/**
* Return the API variable used for managing pages of this type
*
* #pw-internal
*
2023-03-10 19:41:40 +01:00
* @return Permissions
2022-03-08 15:55:41 +01:00
*
*/
public function getPagesManager() {
2023-03-10 19:41:40 +01:00
return $this->wire()->permissions;
2022-03-08 15:55:41 +01:00
}
}