praiadeseselle/wire/core/Role.php

247 lines
6.6 KiB
PHP
Raw Normal View History

2022-03-08 15:55:41 +01:00
<?php namespace ProcessWire;
/**
* ProcessWire Role Page
*
* #pw-summary Role is a type of Page used for grouping permissions to users.
* #pw-body =
* Any given User will have one or more roles, each with zero or more permissions assigned to it.
* Note that most public API-level access checking is typically performed from the User rather than
* the Role(s), as it accounts for the combined roles. Please also see `User`, `Permission` and the
* access related methods on `Page`.
* #pw-body
*
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
*
* @property PageArray $permissions PageArray of permissions assigned to Role.
* @property string $name Name of role.
* @property int $id Numeric page ID of role.
*
*/
class Role extends Page {
/**
* Create a new Role page in memory.
*
* @param Template $tpl
*
*/
public function __construct(Template $tpl = null) {
parent::__construct($tpl);
}
/**
* Wired to API
*
*/
public function wired() {
parent::wired();
if(!$this->template) $this->template = $this->getPredefinedTemplate();
if(!$this->_parent) $this->setParent($this->getPredefinedParent());
}
/**
* Get predefined template (template method)
*
* @return Template
*
*/
protected function getPredefinedTemplate() {
2023-03-10 19:41:40 +01:00
return $this->wire()->templates->get('role');
2022-03-08 15:55:41 +01:00
}
/**
* Get predefined parent page (template method)
*
* @return Page
*
*/
protected function getPredefinedParent() {
2023-03-10 19:41:40 +01:00
return $this->wire()->pages->get($this->wire()->config->rolesPageID);
2022-03-08 15:55:41 +01:00
}
/**
* Does this role have the given permission name, id or object?
*
* @param string|int|Permission $permission Permission object, name, or id.
* @param Page|Template|null $context Optional Page or Template context.
* @return bool
* @see User::hasPermission()
*
*/
public function hasPermission($permission, $context = null) {
$name = $permission;
$permission = null;
$has = false;
if(empty($name)) {
// do nothing
2023-03-10 19:41:40 +01:00
return false;
2022-03-08 15:55:41 +01:00
} else if($name instanceof Page) {
$permission = $name;
$has = $this->permissions->has($permission);
} else if(ctype_digit("$name")) {
$name = (int) $name;
foreach($this->permissions as $p) {
if(((int) $p->id) === $name) {
$permission = $p;
$has = true;
break;
}
}
2023-03-10 19:41:40 +01:00
} else if($name === 'page-add' || $name === 'page-create') {
2022-03-08 15:55:41 +01:00
// runtime permissions that don't have associated permission pages
if(empty($context)) return false;
2023-03-10 19:41:40 +01:00
/** @var Permission $permission */
2022-03-08 15:55:41 +01:00
$permission = $this->wire(new Permission());
$permission->name = $name;
} else if(is_string($name)) {
2023-03-10 19:41:40 +01:00
$permissions = $this->wire()->permissions; // all permissions
if(!$permissions->has($name)) {
if(!ctype_alnum(str_replace('-', '', $name))) {
$name = $this->wire()->sanitizer->pageName($name);
}
$delegated = $permissions->getDelegatedPermissions();
2022-03-08 15:55:41 +01:00
if(isset($delegated[$name])) $name = $delegated[$name];
}
foreach($this->permissions as $p) {
if($p->name === $name) {
$permission = $p;
$has = true;
break;
}
}
}
2023-03-10 19:41:40 +01:00
if($context instanceof Page || $context instanceof Template) {
if(!$permission) $permission = $this->wire()->permissions->get($name);
2022-03-08 15:55:41 +01:00
if($permission) {
$has = $this->hasPermissionContext($has, $permission, $context);
}
}
return $has;
}
/**
* Return whether the role has the permission within the context of a Page or Template
*
* @param bool $has Result from the hasPermission() method
* @param Permission $permission Permission to check
* @param Wire $context Must be a Template or Page
* @return bool
*
*/
protected function hasPermissionContext($has, Permission $permission, Wire $context) {
2023-03-10 19:41:40 +01:00
if(strpos($permission->name, 'page-') !== 0) return $has;
2022-03-08 15:55:41 +01:00
$type = str_replace('page-', '', $permission->name);
2023-03-10 19:41:40 +01:00
2022-03-08 15:55:41 +01:00
if(!in_array($type, array('view', 'edit', 'add', 'create'))) $type = 'edit';
$accessTemplate = $context instanceof Page ? $context->getAccessTemplate($type) : $context;
if(!$accessTemplate) return false;
if(!$accessTemplate->useRoles) return $has;
2023-03-10 19:41:40 +01:00
if($permission->name === 'page-view') {
2022-03-08 15:55:41 +01:00
if(!$has) return false;
$has = $accessTemplate->hasRole($this);
return $has;
}
2023-03-10 19:41:40 +01:00
if($permission->name === 'page-edit' && !$has) return false;
2022-03-08 15:55:41 +01:00
switch($permission->name) {
case 'page-edit':
$has = in_array($this->id, $accessTemplate->editRoles);
break;
case 'page-create':
$has = in_array($this->id, $accessTemplate->createRoles);
break;
case 'page-add':
$has = in_array($this->id, $accessTemplate->addRoles);
break;
default:
// some other page-* permission
$rolesPermissions = $accessTemplate->rolesPermissions;
if(!isset($rolesPermissions["$this->id"])) return $has;
foreach($rolesPermissions["$this->id"] as $permissionID) {
$revoke = strpos($permissionID, '-') === 0;
if($revoke) $permissionID = ltrim($permissionID, '-');
$permissionID = (int) $permissionID;
if($permission->id != $permissionID) continue;
if($has) {
if($revoke) $has = false;
} else {
if(!$revoke) $has = true;
}
break;
}
}
return $has;
}
/**
* Add the given Permission string, id or object.
*
* This is the same as `$role->permissions->add($permission)` except this one will accept ID or name.
*
* @param string|int|Permission $permission Permission object, name or id.
* @return bool Returns false if permission not recognized, true otherwise
*
*/
public function addPermission($permission) {
2023-03-10 19:41:40 +01:00
if(is_string($permission) || is_int($permission)) {
$permission = $this->wire()->permissions->get($permission);
}
if($permission instanceof Permission) {
2022-03-08 15:55:41 +01:00
$this->permissions->add($permission);
return true;
}
return false;
}
/**
* Remove the given permission string, id or object.
*
* This is the same as `$role->permissions->remove($permission)` except this one will accept ID or name.
*
* @param string|int|Permission $permission Permission object, name or id.
* @return bool false if permission not recognized, true otherwise
*
*/
public function removePermission($permission) {
2023-03-10 19:41:40 +01:00
if(is_string($permission) || is_int($permission)) {
$permission = $this->wire()->permissions->get($permission);
}
if($permission instanceof Permission) {
2022-03-08 15:55:41 +01:00
$this->permissions->remove($permission);
return true;
}
return false;
}
/**
* Return the API variable used for managing pages of this type
*
* #pw-internal
*
2023-03-10 19:41:40 +01:00
* @return Roles
2022-03-08 15:55:41 +01:00
*
*/
public function getPagesManager() {
2023-03-10 19:41:40 +01:00
return $this->wire()->roles;
2022-03-08 15:55:41 +01:00
}
}