praiadeseselle/wire/core/Roles.php

140 lines
3.5 KiB
PHP
Raw Normal View History

2022-03-08 15:55:41 +01:00
<?php namespace ProcessWire;
/**
* The Roles class serves as the $roles API variable.
*
* #pw-summary Provides management of all Role pages for access control.
*
* @method PageArray find($selector) Return the Role(s) matching the the given selector.
* @method Role add($name) Add new Role with the given name and return it.
* @method bool save(Role $role) Save given role.
* @method bool delete(Role $role) Delete the given role.
* @method array saveReady(Page $page) Hook called just before a Role is saved #pw-hooker
* @method void saved(Page $page, array $changes = [], $values = []) Hook called after a role has been saved #pw-hooker
* @method void added(Page $page) Hook called just after a Role is added #pw-hooker
* @method void deleteReady(Page $page) Hook called before a Role is deleted #pw-hooker
* @method void deleted(Page $page) Hook called after a Role is deleted #pw-hooker
*
*/
class Roles extends PagesType {
2023-03-10 19:41:40 +01:00
/**
* @var Role|null
*
*/
2022-03-08 15:55:41 +01:00
protected $guestRole = null;
/**
* Get the 'guest' role
*
* This is a performance optimized version of `$roles->get('guest')`.
*
* #pw-internal
*
2023-03-10 19:41:40 +01:00
* @return Role
2022-03-08 15:55:41 +01:00
* @throws WireException
*
*/
public function getGuestRole() {
if($this->guestRole) return $this->guestRole;
2023-03-10 19:41:40 +01:00
$this->guestRole = parent::get((int) $this->wire()->config->guestUserRolePageID);
2022-03-08 15:55:41 +01:00
return $this->guestRole;
}
/**
* Get a Role by name, numeric ID or selector
*
* @param string $selectorString Role name or selector
* @return Role|NullPage|null
*
*/
public function get($selectorString) {
if($selectorString === 'guest') return $this->getGuestRole();
return parent::get($selectorString);
}
/**
* Save a Role
*
* #pw-group-manipulation
*
* @param Role|Page $page
* @return bool True on success, false on failure
* @throws WireException
*
*/
public function ___save(Page $page) {
return parent::___save($page);
}
/**
* Permanently delete a Role
*
* #pw-group-manipulation
*
* @param Role|Page $page Permission to delete
* @param bool $recursive If set to true, then this will attempt to delete any pages below the Permission too.
* @return bool True on success, false on failure
* @throws WireException
*
*/
public function ___delete(Page $page, $recursive = false) {
return parent::___delete($page, $recursive);
}
/**
* Add a new Role with the given name and return it
*
* #pw-group-manipulation
*
* @param string $name Name of role you want to add, i.e. "hello-world"
2023-03-10 19:41:40 +01:00
* @return Role|NullPage Returns a Role page on success, or a NullPage on error
2022-03-08 15:55:41 +01:00
*
*/
public function ___add($name) {
2023-03-10 19:41:40 +01:00
/** @var Role|NullPage $role */
$role = parent::___add($name);
return $role;
2022-03-08 15:55:41 +01:00
}
/**
* Ensure that every role has at least 'page-view' permission
*
* #pw-internal
*
* @param Page $page
*
*/
protected function loaded(Page $page) {
2023-03-10 19:41:40 +01:00
$hasPageView = false;
foreach($page->permissions as $permission) {
if($permission->name === 'page-view') $hasPageView = true;
if($hasPageView) break;
}
if(!$hasPageView) {
$pageView = $this->wire()->permissions->get('page-view');
$page->permissions->add($pageView);
2022-03-08 15:55:41 +01:00
}
}
/**
* Hook called when a page and its data have been deleted
*
* #pw-internal
*
* @param Page $page
*
*/
public function ___deleted(Page $page) {
2023-03-10 19:41:40 +01:00
foreach($this->wire()->templates as $template) {
2022-03-08 15:55:41 +01:00
/** @var Template $template */
if(!$template->useRoles) continue;
$template->removeRole($page, 'all');
if($template->isChanged()) $template->save();
}
parent::___deleted($page);
}
}