Toss ServerOperator; PermissibleBase now tracks its own op status

as well as being simpler, this also allows some nice features, such as granting memory-only op state that goes away when the player quits the server.
This commit is contained in:
Dylan K. Taylor 2020-11-28 18:22:59 +00:00
parent 4439781124
commit c7961bfe90
8 changed files with 32 additions and 78 deletions

View File

@ -677,7 +677,7 @@ class Server{
$this->operators->set(strtolower($name), true);
if(($player = $this->getPlayerExact($name)) !== null){
$player->recalculatePermissions();
$player->onOpStatusChange(true);
}
$this->operators->save();
}
@ -686,7 +686,7 @@ class Server{
$this->operators->remove(strtolower($name));
if(($player = $this->getPlayerExact($name)) !== null){
$player->recalculatePermissions();
$player->onOpStatusChange(false);
}
$this->operators->save();
}

View File

@ -44,7 +44,7 @@ class ConsoleCommandSender implements CommandSender{
public function __construct(Server $server, Language $language){
$this->server = $server;
$this->perm = new PermissibleBase($this);
$this->perm = new PermissibleBase($this, true);
$this->language = $language;
}
@ -76,11 +76,7 @@ class ConsoleCommandSender implements CommandSender{
return "CONSOLE";
}
public function isOp() : bool{
return true;
}
public function setOp(bool $value) : void{
public function onOpStatusChange(bool $value) : void{
}

View File

@ -25,7 +25,11 @@ namespace pocketmine\permission;
use pocketmine\plugin\Plugin;
interface Permissible extends ServerOperator{
interface Permissible{
public function isOp() : bool;
public function onOpStatusChange(bool $value) : void;
/**
* Checks if this instance has a permission overridden

View File

@ -29,11 +29,11 @@ use pocketmine\timings\Timings;
use function spl_object_id;
class PermissibleBase implements Permissible{
/** @var ServerOperator */
private $opable;
/** @var bool */
private $op;
/** @var Permissible|null */
private $parent = null;
private $parent;
/** @var PermissionAttachment[] */
private $attachments = [];
@ -41,19 +41,19 @@ class PermissibleBase implements Permissible{
/** @var PermissionAttachmentInfo[] */
private $permissions = [];
public function __construct(ServerOperator $opable){
$this->opable = $opable;
if($opable instanceof Permissible){
$this->parent = $opable;
}
public function __construct(?Permissible $permissible, bool $isOp){
$this->parent = $permissible;
$this->op = $isOp;
//TODO: permissions need to be recalculated here, or inherited permissions won't work
}
public function isOp() : bool{
return $this->opable->isOp();
return $this->op;
}
public function setOp(bool $value) : void{
$this->opable->setOp($value);
public function onOpStatusChange(bool $value) : void{
$this->op = $value;
$this->recalculatePermissions();
}
/**

View File

@ -30,6 +30,14 @@ trait PermissibleDelegateTrait{
/** @var PermissibleBase */
private $perm;
public function isOp() : bool{
return $this->perm->isOp();
}
public function onOpStatusChange(bool $value) : void{
$this->perm->onOpStatusChange($value);
}
/**
* @param Permission|string $name
*/

View File

@ -1,36 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\permission;
interface ServerOperator{
/**
* Checks if the current object has operator permissions
*/
public function isOp() : bool;
/**
* Sets the operator permission for the current object
*/
public function setOp(bool $value) : void;
}

View File

@ -23,9 +23,7 @@ declare(strict_types=1);
namespace pocketmine\player;
use pocketmine\permission\ServerOperator;
interface IPlayer extends ServerOperator{
interface IPlayer{
public function isOnline() : bool;

View File

@ -281,7 +281,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$this->uuid = $this->playerInfo->getUuid();
$this->xuid = $this->playerInfo instanceof XboxLivePlayerInfo ? $this->playerInfo->getXuid() : "";
$this->perm = new PermissibleBase($this);
$this->perm = new PermissibleBase($this, $this->server->isOp($this->username));
$this->chunksPerTick = (int) $this->server->getConfigGroup()->getProperty("chunk-sending.per-tick", 4);
$this->spawnThreshold = (int) (($this->server->getConfigGroup()->getProperty("chunk-sending.spawn-radius", 4) ** 2) * M_PI);
$this->chunkSelector = new ChunkSelector();
@ -523,24 +523,6 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
return $this->isConnected();
}
public function isOp() : bool{
return $this->server->isOp($this->getName());
}
public function setOp(bool $value) : void{
if($value === $this->isOp()){
return;
}
if($value){
$this->server->addOp($this->getName());
}else{
$this->server->removeOp($this->getName());
}
$this->networkSession->syncAdventureSettings($this);
}
public function recalculatePermissions() : void{
$permManager = PermissionManager::getInstance();
$permManager->unsubscribeFromPermission(Server::BROADCAST_CHANNEL_USERS, $this);
@ -552,6 +534,8 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$this->delegateRecalculatePermissions();
$this->networkSession->syncAdventureSettings($this);
if($this->spawned){
if($this->hasPermission(Server::BROADCAST_CHANNEL_USERS)){
$permManager->subscribeToPermission(Server::BROADCAST_CHANNEL_USERS, $this);