1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 09:44:38 +02:00

Minor housecleaning in User.php plus resolve issue processwire/processwire-issues#1293 User::isLoggedin() improvement/phpdoc

This commit is contained in:
Ryan Cramer
2021-08-04 11:21:30 -04:00
parent cee6a51579
commit e4eed8358d

View File

@@ -78,6 +78,7 @@ class User extends Page {
*/ */
public function hasRole($role) { public function hasRole($role) {
/** @var PageArray $roles */
$roles = $this->get('roles'); $roles = $this->get('roles');
$has = false; $has = false;
@@ -124,7 +125,7 @@ class User extends Page {
* *
*/ */
public function addRole($role) { public function addRole($role) {
if(is_string($role) || is_int($role)) $role = $this->wire('roles')->get($role); if(is_string($role) || is_int($role)) $role = $this->wire()->roles->get($role);
if(is_object($role) && $role instanceof Role) { if(is_object($role) && $role instanceof Role) {
$this->get('roles')->add($role); $this->get('roles')->add($role);
return true; return true;
@@ -148,7 +149,7 @@ class User extends Page {
* *
*/ */
public function removeRole($role) { public function removeRole($role) {
if(is_string($role) || is_int($role)) $role = $this->wire('roles')->get($role); if(is_string($role) || is_int($role)) $role = $this->wire()->roles->get($role);
if(is_object($role) && $role instanceof Role) { if(is_object($role) && $role instanceof Role) {
$this->get('roles')->remove($role); $this->get('roles')->remove($role);
return true; return true;
@@ -180,13 +181,14 @@ class User extends Page {
*/ */
public function hasPermission($name, $context = null) { public function hasPermission($name, $context = null) {
// This method serves as the public interface to the hasPagePermission and hasTemplatePermission methods. // This method serves as the public interface to the hasPagePermission and hasTemplatePermission methods.
$hooks = $this->wire()->hooks;
if($context === null || $context instanceof Page) { if($context === null || $context instanceof Page) {
$hook = $this->wire('hooks')->isHooked('hasPagePermission()'); $hook = $hooks->isHooked('hasPagePermission()');
return $hook ? $this->hasPagePermission($name, $context) : $this->___hasPagePermission($name, $context); return $hook ? $this->hasPagePermission($name, $context) : $this->___hasPagePermission($name, $context);
} }
$hook = $this->wire('hooks')->isHooked('hasTemplatePermission()'); $hook = $hooks->isHooked('hasTemplatePermission()');
if($context instanceof Template) { if($context instanceof Template) {
return $hook ? $this->hasTemplatePermission($name, $context) : $this->___hasTemplatePermission($name, $context); return $hook ? $this->hasTemplatePermission($name, $context) : $this->___hasTemplatePermission($name, $context);
@@ -194,7 +196,7 @@ class User extends Page {
if($context === true || $context === 'templates') { if($context === true || $context === 'templates') {
$addedTemplates = array(); $addedTemplates = array();
foreach($this->wire('templates') as $t) { foreach($this->wire()->templates as $t) {
if(!$t->useRoles) continue; if(!$t->useRoles) continue;
$has = $hook ? $this->hasTemplatePermission($name, $t) : $this->___hasTemplatePermission($name, $t); $has = $hook ? $this->hasTemplatePermission($name, $t) : $this->___hasTemplatePermission($name, $t);
if($has) $addedTemplates[] = $t; if($has) $addedTemplates[] = $t;
@@ -223,7 +225,7 @@ class User extends Page {
protected function ___hasPagePermission($name, Page $page = null) { protected function ___hasPagePermission($name, Page $page = null) {
if($this->isSuperuser()) return true; if($this->isSuperuser()) return true;
$permissions = $this->wire('permissions'); $permissions = $this->wire()->permissions;
// convert $name to a Permission object (if it isn't already) // convert $name to a Permission object (if it isn't already)
if($name instanceof Page) { if($name instanceof Page) {
@@ -253,6 +255,7 @@ class User extends Page {
if(!$permission || !$permission->id) return false; if(!$permission || !$permission->id) return false;
/** @var PageArray $roles */
$roles = $this->getUnformatted('roles'); $roles = $this->getUnformatted('roles');
if(empty($roles) || !$roles instanceof PageArray) return false; if(empty($roles) || !$roles instanceof PageArray) return false;
$has = false; $has = false;
@@ -317,7 +320,7 @@ class User extends Page {
if($template instanceof Template) { if($template instanceof Template) {
// fantastic then // fantastic then
} else if(is_string($template) || is_int($template)) { } else if(is_string($template) || is_int($template)) {
$template = $this->templates->get($this->wire('sanitizer')->name($template)); $template = $this->templates->get($this->wire()->sanitizer->name($template));
if(!$template) return false; if(!$template) return false;
} else { } else {
return false; return false;
@@ -327,6 +330,7 @@ class User extends Page {
// because we don't have any page context to inherit from at this point // because we don't have any page context to inherit from at this point
// if(!$template->useRoles) return false; // if(!$template->useRoles) return false;
/** @var PageArray $roles */
$roles = $this->get('roles'); $roles = $this->get('roles');
if(empty($roles)) return false; if(empty($roles)) return false;
$has = false; $has = false;
@@ -383,8 +387,9 @@ class User extends Page {
*/ */
public function getPermissions(Page $page = null) { public function getPermissions(Page $page = null) {
// Does not currently include page-add or page-create permissions (runtime). // Does not currently include page-add or page-create permissions (runtime).
if($this->isSuperuser()) return $this->wire('permissions')->getIterator(); // all permissions if($this->isSuperuser()) return $this->wire()->permissions->getIterator(); // all permissions
$permissions = $this->wire('pages')->newPageArray(); $permissions = $this->wire()->pages->newPageArray();
/** @var PageArray $roles */
$roles = $this->get('roles'); $roles = $this->get('roles');
if(empty($roles)) return $permissions; if(empty($roles)) return $permissions;
foreach($roles as $key => $role) { foreach($roles as $key => $role) {
@@ -411,21 +416,25 @@ class User extends Page {
*/ */
public function isSuperuser() { public function isSuperuser() {
if(is_bool($this->isSuperuser)) return $this->isSuperuser; if(is_bool($this->isSuperuser)) return $this->isSuperuser;
$config = $this->wire('config'); $config = $this->wire()->config;
if($this->id === $config->superUserPageID) { if($this->id === $config->superUserPageID) {
$is = true; $is = true;
} else if($this->id === $config->guestUserPageID) { } else if($this->id === $config->guestUserPageID) {
$is = false; $is = false;
} else { } else {
$superuserRoleID = (int) $config->superUserRolePageID; $superuserRoleID = (int) $config->superUserRolePageID;
/** @var PageArray $roles */
$roles = $this->getUnformatted('roles'); $roles = $this->getUnformatted('roles');
if(empty($roles)) return false; // no cache intentional if(empty($roles)) return false; // no cache intentional
$is = false; $is = false;
foreach($roles as $role) if(((int) $role->id) === $superuserRoleID) { foreach($roles as $role) {
/** @var Role $role */
if(((int) $role->id) === $superuserRoleID) {
$is = true; $is = true;
break; break;
} }
} }
}
$this->isSuperuser = $is; $this->isSuperuser = $is;
return $is; return $is;
} }
@@ -437,17 +446,23 @@ class User extends Page {
* *
*/ */
public function isGuest() { public function isGuest() {
return $this->id === $this->wire('config')->guestUserPageID; return $this->id === $this->wire()->config->guestUserPageID;
} }
/** /**
* Is the current user logged in? * Is the current $user logged in and the same as this user?
*
* When this method returns true, it means the current $user (API variable) is
* this user and that they are logged in.
* *
* @return bool * @return bool
* *
*/ */
public function isLoggedin() { public function isLoggedin() {
return !$this->isGuest(); if($this->isGuest()) return false;
$user = $this->wire()->user;
$userId = $user ? $user->id : 0;
return $userId && "$userId" === "$this->id";
} }
/** /**
@@ -461,7 +476,7 @@ class User extends Page {
protected function getFieldValue($key, $selector = '') { protected function getFieldValue($key, $selector = '') {
$value = parent::getFieldValue($key, $selector); $value = parent::getFieldValue($key, $selector);
if(!$value && $key == 'language') { if(!$value && $key == 'language') {
$languages = $this->wire('languages'); $languages = $this->wire()->languages;
if($languages) $value = $languages->getDefault(); if($languages) $value = $languages->getDefault();
} }
return $value; return $value;
@@ -496,7 +511,7 @@ class User extends Page {
*/ */
public function ___setEditor(WirePageEditor $editor) { public function ___setEditor(WirePageEditor $editor) {
parent::___setEditor($editor); parent::___setEditor($editor);
if(!$editor instanceof ProcessUser) $this->wire('session')->redirect($this->editUrl()); if(!$editor instanceof ProcessUser) $this->wire()->session->redirect($this->editUrl());
} }
/** /**
@@ -504,11 +519,11 @@ class User extends Page {
* *
* #pw-internal * #pw-internal
* *
* @return Pages|PagesType * @return Pages|PagesType|Users
* *
*/ */
public function getPagesManager() { public function getPagesManager() {
return $this->wire('users'); return $this->wire()->users;
} }
/** /**