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

View File

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

View File

@ -25,7 +25,11 @@ namespace pocketmine\permission;
use pocketmine\plugin\Plugin; 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 * Checks if this instance has a permission overridden

View File

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

View File

@ -30,6 +30,14 @@ trait PermissibleDelegateTrait{
/** @var PermissibleBase */ /** @var PermissibleBase */
private $perm; private $perm;
public function isOp() : bool{
return $this->perm->isOp();
}
public function onOpStatusChange(bool $value) : void{
$this->perm->onOpStatusChange($value);
}
/** /**
* @param Permission|string $name * @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; namespace pocketmine\player;
use pocketmine\permission\ServerOperator; interface IPlayer{
interface IPlayer extends ServerOperator{
public function isOnline() : bool; public function isOnline() : bool;

View File

@ -281,7 +281,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$this->uuid = $this->playerInfo->getUuid(); $this->uuid = $this->playerInfo->getUuid();
$this->xuid = $this->playerInfo instanceof XboxLivePlayerInfo ? $this->playerInfo->getXuid() : ""; $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->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->spawnThreshold = (int) (($this->server->getConfigGroup()->getProperty("chunk-sending.spawn-radius", 4) ** 2) * M_PI);
$this->chunkSelector = new ChunkSelector(); $this->chunkSelector = new ChunkSelector();
@ -523,24 +523,6 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
return $this->isConnected(); 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{ public function recalculatePermissions() : void{
$permManager = PermissionManager::getInstance(); $permManager = PermissionManager::getInstance();
$permManager->unsubscribeFromPermission(Server::BROADCAST_CHANNEL_USERS, $this); $permManager->unsubscribeFromPermission(Server::BROADCAST_CHANNEL_USERS, $this);
@ -552,6 +534,8 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$this->delegateRecalculatePermissions(); $this->delegateRecalculatePermissions();
$this->networkSession->syncAdventureSettings($this);
if($this->spawned){ if($this->spawned){
if($this->hasPermission(Server::BROADCAST_CHANNEL_USERS)){ if($this->hasPermission(Server::BROADCAST_CHANNEL_USERS)){
$permManager->subscribeToPermission(Server::BROADCAST_CHANNEL_USERS, $this); $permManager->subscribeToPermission(Server::BROADCAST_CHANNEL_USERS, $this);