mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 01:46:04 +00:00
Fixed wrong paths
This commit is contained in:
136
src/pocketmine/permission/BanEntry.php
Normal file
136
src/pocketmine/permission/BanEntry.php
Normal file
@ -0,0 +1,136 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\permission;
|
||||
|
||||
class BanEntry{
|
||||
public static $format = "Y-m-d H:i:s O";
|
||||
|
||||
private $name;
|
||||
/** @var \DateTime */
|
||||
private $creationDate = null;
|
||||
private $source = "(Unknown)";
|
||||
/** @var \DateTime */
|
||||
private $expirationDate = null;
|
||||
private $reason = "Banned by an operator.";
|
||||
|
||||
public function __construct($name){
|
||||
$this->name = strtolower($name);
|
||||
$this->creationDate = new \DateTime();
|
||||
}
|
||||
|
||||
public function getName(){
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getCreated(){
|
||||
return $this->creationDate;
|
||||
}
|
||||
|
||||
public function setCreated(\DateTime $date){
|
||||
$this->creationDate = $date;
|
||||
}
|
||||
|
||||
public function getSource(){
|
||||
return $this->source;
|
||||
}
|
||||
|
||||
public function setSource($source){
|
||||
$this->source = $source;
|
||||
}
|
||||
|
||||
public function getExpires(){
|
||||
return $this->expirationDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $date
|
||||
*/
|
||||
public function setExpires($date){
|
||||
$this->expirationDate = $date;
|
||||
}
|
||||
|
||||
public function hasExpired(){
|
||||
$now = new \DateTime();
|
||||
|
||||
return $this->expirationDate === null ? false : $this->expirationDate < $now;
|
||||
}
|
||||
|
||||
public function getReason(){
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
public function setReason($reason){
|
||||
$this->reason = $reason;
|
||||
}
|
||||
|
||||
public function getString(){
|
||||
$str = "";
|
||||
$str .= $this->getName();
|
||||
$str .= "|";
|
||||
$str .= $this->getCreated()->format(self::$format);
|
||||
$str .= "|";
|
||||
$str .= $this->getSource();
|
||||
$str .= "|";
|
||||
$str .= $this->getExpires() === null ? "Forever" : $this->getExpires()->format(self::$format);
|
||||
$str .= "|";
|
||||
$str .= $this->getReason();
|
||||
|
||||
return $str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $str
|
||||
*
|
||||
* @return BanEntry
|
||||
*/
|
||||
public static function fromString($str){
|
||||
if(strlen($str) < 2){
|
||||
return null;
|
||||
}else{
|
||||
$str = explode("|", trim($str));
|
||||
$entry = new BanEntry(trim(array_shift($str)));
|
||||
if(count($str) > 0){
|
||||
$entry->setCreated(\DateTime::createFromFormat(self::$format, array_shift($str)));
|
||||
if(count($str) > 0){
|
||||
$entry->setSource(trim(array_shift($str)));
|
||||
if(count($str) > 0){
|
||||
$expire = trim(array_shift($str));
|
||||
if(strtolower($expire) !== "forever" and strlen($expire) > 0){
|
||||
$entry->setExpires(\DateTime::createFromFormat(self::$format, $expire));
|
||||
}
|
||||
if(count($str) > 0){
|
||||
$entry->setReason(trim(array_shift($str)));
|
||||
|
||||
return $entry;
|
||||
}else{
|
||||
return $entry;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
return $entry;
|
||||
}
|
||||
}else{
|
||||
return $entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
166
src/pocketmine/permission/BanList.php
Normal file
166
src/pocketmine/permission/BanList.php
Normal file
@ -0,0 +1,166 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\permission;
|
||||
|
||||
use pocketmine\Server;
|
||||
|
||||
class BanList{
|
||||
|
||||
/** @var BanEntry[] */
|
||||
private $list = array();
|
||||
|
||||
/** @var string */
|
||||
private $file;
|
||||
|
||||
/** @var bool */
|
||||
private $enabled = true;
|
||||
|
||||
/**
|
||||
* @param string $file
|
||||
*/
|
||||
public function __construct($file){
|
||||
$this->file = $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEnabled(){
|
||||
return $this->enabled === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $flag
|
||||
*/
|
||||
public function setEnabled($flag){
|
||||
$this->enabled = (bool) $flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BanEntry[]
|
||||
*/
|
||||
public function getEntries(){
|
||||
$this->removeExpired();
|
||||
|
||||
return $this->list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isBanned($name){
|
||||
$name = strtolower($name);
|
||||
if(!$this->isEnabled()){
|
||||
return false;
|
||||
}else{
|
||||
$this->removeExpired();
|
||||
|
||||
return isset($this->list[$name]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BanEntry $entry
|
||||
*/
|
||||
public function add(BanEntry $entry){
|
||||
$this->list[$entry->getName()] = $entry;
|
||||
$this->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $target
|
||||
* @param string $reason
|
||||
* @param \DateTime $expires
|
||||
* @param string $source
|
||||
*
|
||||
* @return BanEntry
|
||||
*/
|
||||
public function addBan($target, $reason = null, $expires = null, $source = null){
|
||||
$entry = new BanEntry($target);
|
||||
$entry->setSource($source != null ? $source : $entry->getSource());
|
||||
$entry->setExpires($expires);
|
||||
$entry->setReason($reason != null ? $reason : $entry->getReason());
|
||||
|
||||
$this->list[$entry->getName()] = $entry;
|
||||
$this->save();
|
||||
|
||||
return $entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
public function remove($name){
|
||||
$name = strtolower($name);
|
||||
if(isset($this->list[$name])){
|
||||
unset($this->list[$name]);
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
|
||||
public function removeExpired(){
|
||||
foreach($this->list as $name => $entry){
|
||||
if($entry->hasExpired()){
|
||||
unset($this->list[$name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function load(){
|
||||
$this->list = array();
|
||||
$fp = @fopen($this->file, "r");
|
||||
if(is_resource($fp)){
|
||||
while(($line = fgets($fp)) !== false){
|
||||
if($line{0} !== "#"){
|
||||
$entry = BanEntry::fromString($line);
|
||||
if($entry instanceof BanEntry){
|
||||
$this->list[$entry->getName()] = $entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose($fp);
|
||||
}else{
|
||||
console("[ERROR] Could not load ban list");
|
||||
}
|
||||
}
|
||||
|
||||
public function save($flag = true){
|
||||
$this->removeExpired();
|
||||
$fp = @fopen($this->file, "w");
|
||||
if(is_resource($fp)){
|
||||
if($flag === true){
|
||||
fwrite($fp, "# Updated " . strftime("%x %H:%M", time()) . " by " . Server::getInstance()->getName() . " " . Server::getInstance()->getPocketMineVersion() . "\n");
|
||||
fwrite($fp, "# victim name | ban date | banned by | banned until | reason\n\n");
|
||||
}
|
||||
|
||||
foreach($this->list as $entry){
|
||||
fwrite($fp, $entry->getString() . "\n");
|
||||
}
|
||||
fclose($fp);
|
||||
}else{
|
||||
console("[ERROR] Could not save ban list");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
114
src/pocketmine/permission/DefaultPermissions.php
Normal file
114
src/pocketmine/permission/DefaultPermissions.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\permission;
|
||||
|
||||
use pocketmine\Server;
|
||||
|
||||
abstract class DefaultPermissions{
|
||||
const ROOT = "pocketmine";
|
||||
|
||||
/**
|
||||
* @param Permission $perm
|
||||
* @param Permission $parent
|
||||
*
|
||||
* @return Permission
|
||||
*/
|
||||
public static function registerPermission(Permission $perm, Permission $parent = null){
|
||||
if($parent instanceof Permission){
|
||||
$parent->getChildren()[$perm->getName()] = true;
|
||||
|
||||
return self::registerPermission($perm);
|
||||
}
|
||||
Server::getInstance()->getPluginManager()->addPermission($perm);
|
||||
|
||||
return Server::getInstance()->getPluginManager()->getPermission($perm->getName());
|
||||
}
|
||||
|
||||
public static function registerCorePermissions(){
|
||||
$parent = self::registerPermission(new Permission(self::ROOT, "Allows using all PocketMine commands and utilities"));
|
||||
|
||||
$broadcasts = self::registerPermission(new Permission(self::ROOT . ".broadcast", "Allows the user to receive all broadcast messages"), $parent);
|
||||
|
||||
self::registerPermission(new Permission(self::ROOT . ".broadcast.admin", "Allows the user to receive administrative broadcasts", Permission::DEFAULT_OP), $broadcasts);
|
||||
self::registerPermission(new Permission(self::ROOT . ".broadcast.user", "Allows the user to receive user broadcasts", Permission::DEFAULT_TRUE), $broadcasts);
|
||||
|
||||
$broadcasts->recalculatePermissibles();
|
||||
|
||||
$commands = self::registerPermission(new Permission(self::ROOT . ".command", "Allows using all PocketMine commands"), $parent);
|
||||
|
||||
$whitelist = self::registerPermission(new Permission(self::ROOT . ".command.whitelist", "Allows the user to modify the server whitelist", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.whitelist.add", "Allows the user to add a player to the server whitelist"), $whitelist);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.whitelist.remove", "Allows the user to remove a player to the server whitelist"), $whitelist);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.whitelist.reload", "Allows the user to reload the server whitelist"), $whitelist);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.whitelist.enable", "Allows the user to enable the server whitelist"), $whitelist);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.whitelist.disable", "Allows the user to disable the server whitelist"), $whitelist);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.whitelist.list", "Allows the user to list all the players on the server whitelist"), $whitelist);
|
||||
$whitelist->recalculatePermissibles();
|
||||
|
||||
$ban = self::registerPermission(new Permission(self::ROOT . ".command.ban", "Allows the user to ban people", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.ban.player", "Allows the user to ban players"), $ban);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.ban.ip", "Allows the user to ban IP addresses"), $ban);
|
||||
$ban->recalculatePermissibles();
|
||||
|
||||
$unban = self::registerPermission(new Permission(self::ROOT . ".command.unban", "Allows the user to unban people", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.unban.player", "Allows the user to unban players"), $unban);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.unban.ip", "Allows the user to unban IP addresses"), $unban);
|
||||
$unban->recalculatePermissibles();
|
||||
|
||||
$op = self::registerPermission(new Permission(self::ROOT . ".command.op", "Allows the user to change operators", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.op.give", "Allows the user to give a player operator status"), $op);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.op.take", "Allows the user to take a players operator status"), $op);
|
||||
$op->recalculatePermissibles();
|
||||
|
||||
$save = self::registerPermission(new Permission(self::ROOT . ".command.save", "Allows the user to save the worlds", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.save.enable", "Allows the user to enable automatic saving"), $save);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.save.disable", "Allows the user to disable automatic saving"), $save);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.save.perform", "Allows the user to perform a manual save"), $save);
|
||||
$save->recalculatePermissibles();
|
||||
|
||||
$time = self::registerPermission(new Permission(self::ROOT . ".command.time", "Allows the user to alter the time", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.time.add", "Allows the user to fast-forward time"), $time);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.time.set", "Allows the user to change the time"), $time);
|
||||
$time->recalculatePermissibles();
|
||||
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.kill", "Allows the user to commit suicide", Permission::DEFAULT_TRUE), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.me", "Allows the user to perform a chat action", Permission::DEFAULT_TRUE), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.tell", "Allows the user to privately message another player", Permission::DEFAULT_TRUE), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.say", "Allows the user to talk as the console", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.give", "Allows the user to give items to players", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.teleport", "Allows the user to teleport players", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.kick", "Allows the user to kick players", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.stop", "Allows the user to stop the server", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.list", "Allows the user to list all online players", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.help", "Allows the user to view the help menu", Permission::DEFAULT_TRUE), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.plugins", "Allows the user to view the list of plugins", Permission::DEFAULT_OP), $commands);
|
||||
//TODO: self::registerPermission(new Permission(self::ROOT . ".command.reload", "Allows the user to reload the server settings", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.version", "Allows the user to view the version of the server", Permission::DEFAULT_TRUE), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.gamemode", "Allows the user to change the gamemode of players", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.defaultgamemode", "Allows the user to change the default gamemode", Permission::DEFAULT_OP), $commands);
|
||||
self::registerPermission(new Permission(self::ROOT . ".command.seed", "Allows the user to view the seed of the world", Permission::DEFAULT_OP), $commands);
|
||||
|
||||
$commands->recalculatePermissibles();
|
||||
|
||||
$parent->recalculatePermissibles();
|
||||
}
|
||||
}
|
74
src/pocketmine/permission/Permissible.php
Normal file
74
src/pocketmine/permission/Permissible.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\permission;
|
||||
|
||||
use pocketmine\plugin\Plugin;
|
||||
|
||||
interface Permissible extends ServerOperator{
|
||||
|
||||
/**
|
||||
* Checks if this instance has a permission overridden
|
||||
*
|
||||
* @param string|Permission $name
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isPermissionSet($name);
|
||||
|
||||
/**
|
||||
* Returns the permission value if overridden, or the default value if not
|
||||
*
|
||||
* @param string|Permission $name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function hasPermission($name);
|
||||
|
||||
/**
|
||||
* @param Plugin $plugin
|
||||
* @param string $name
|
||||
* @param bool $value
|
||||
*
|
||||
* @return PermissionAttachment
|
||||
*/
|
||||
public function addAttachment(Plugin $plugin, $name = null, $value = null);
|
||||
|
||||
/**
|
||||
* @param PermissionAttachment $attachment
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function removeAttachment(PermissionAttachment $attachment);
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function recalculatePermissions();
|
||||
|
||||
/**
|
||||
* TODO: Check this
|
||||
* @return Permission[]
|
||||
*/
|
||||
public function getEffectivePermissions();
|
||||
|
||||
}
|
219
src/pocketmine/permission/PermissibleBase.php
Normal file
219
src/pocketmine/permission/PermissibleBase.php
Normal file
@ -0,0 +1,219 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\permission;
|
||||
|
||||
use pocketmine\plugin\Plugin;
|
||||
use pocketmine\Server;
|
||||
|
||||
class PermissibleBase implements Permissible{
|
||||
/** @var ServerOperator */
|
||||
private $opable = null;
|
||||
|
||||
/** @var Permissible */
|
||||
private $parent;
|
||||
|
||||
/**
|
||||
* @var PermissionAttachment[]
|
||||
*/
|
||||
private $attachments = array();
|
||||
|
||||
/**
|
||||
* @var PermissionAttachmentInfo[]
|
||||
*/
|
||||
private $permissions = array();
|
||||
|
||||
/**
|
||||
* @param ServerOperator $opable
|
||||
*/
|
||||
public function __construct(ServerOperator $opable){
|
||||
$this->opable = $opable;
|
||||
if($opable instanceof Permissible){
|
||||
$this->parent = $opable;
|
||||
}else{
|
||||
$this->parent = $this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isOp(){
|
||||
if($this->opable === null){
|
||||
return false;
|
||||
}else{
|
||||
return $this->opable->isOp();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $value
|
||||
*/
|
||||
public function setOp($value){
|
||||
if($this->opable === null){
|
||||
trigger_error("Cannot change ip value as no ServerOperator is set", E_USER_WARNING);
|
||||
|
||||
return;
|
||||
}else{
|
||||
$this->opable->setOp($value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Permission|string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isPermissionSet($name){
|
||||
return isset($this->permissions[$name instanceof Permission ? $name->getName() : $name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Permission|string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPermission($name){
|
||||
if($name instanceof Permission){
|
||||
$name = $name->getName();
|
||||
}
|
||||
|
||||
if($this->isPermissionSet($name)){
|
||||
return $this->permissions[$name]->getValue();
|
||||
}
|
||||
|
||||
if(($perm = Server::getInstance()->getPluginManager()->getPermission($name)) !== null){
|
||||
$perm = $perm->getDefault();
|
||||
return $perm === Permission::DEFAULT_TRUE or ($this->isOp() and $perm === Permission::DEFAULT_OP) or (!$this->isOp() and $perm === Permission::DEFAULT_NOT_OP);
|
||||
}else{
|
||||
return Permission::$DEFAULT_PERMISSION === Permission::DEFAULT_TRUE or ($this->isOp() and Permission::$DEFAULT_PERMISSION === Permission::DEFAULT_OP) or (!$this->isOp() and Permission::$DEFAULT_PERMISSION === Permission::DEFAULT_NOT_OP);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* //TODO: tick scheduled attachments
|
||||
*
|
||||
* @param Plugin $plugin
|
||||
* @param string $name
|
||||
* @param bool $value
|
||||
*
|
||||
* @return PermissionAttachment
|
||||
*/
|
||||
public function addAttachment(Plugin $plugin, $name = null, $value = null){
|
||||
if($plugin === null){
|
||||
trigger_error("Plugin cannot be null", E_USER_WARNING);
|
||||
|
||||
return null;
|
||||
}elseif(!$plugin->isEnabled()){
|
||||
trigger_error("Plugin " . $plugin->getDescription()->getName() . " is disabled", E_USER_WARNING);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$result = new PermissionAttachment($plugin, $this->parent);
|
||||
$this->attachments[spl_object_hash($result)] = $result;
|
||||
if($name !== null and $value !== null){
|
||||
$result->setPermission($name, $value);
|
||||
}
|
||||
|
||||
$this->recalculatePermissions();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PermissionAttachment $attachment
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function removeAttachment(PermissionAttachment $attachment){
|
||||
if($attachment === null){
|
||||
trigger_error("Attachment cannot be null", E_USER_WARNING);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if(isset($this->attachments[spl_object_hash($attachment)])){
|
||||
unset($this->attachments[spl_object_hash($attachment)]);
|
||||
if(($ex = $attachment->getRemovalCallback()) !== null){
|
||||
$ex->attachmentRemoved($attachment);
|
||||
}
|
||||
|
||||
$this->recalculatePermissions();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function recalculatePermissions(){
|
||||
$this->clearPermissions();
|
||||
$defaults = Server::getInstance()->getPluginManager()->getDefaultPermissions($this->isOp());
|
||||
Server::getInstance()->getPluginManager()->subscribeToDefaultPerms($this->isOp(), $this->parent);
|
||||
|
||||
foreach($defaults as $perm){
|
||||
$name = $perm->getName();
|
||||
$this->permissions[$name] = new PermissionAttachmentInfo($this->parent, $name, null, true);
|
||||
Server::getInstance()->getPluginManager()->subscribeToPermission($name, $this->parent);
|
||||
$this->calculateChildPermissions($perm->getChildren(), false, null);
|
||||
}
|
||||
|
||||
foreach($this->attachments as $attachment){
|
||||
$this->calculateChildPermissions($attachment->getPermissions(), false, $attachment);
|
||||
}
|
||||
}
|
||||
|
||||
public function clearPermissions(){
|
||||
foreach(array_keys($this->permissions) as $name){
|
||||
Server::getInstance()->getPluginManager()->unsubscribeFromPermission($name, $this->parent);
|
||||
}
|
||||
|
||||
Server::getInstance()->getPluginManager()->unsubscribeFromDefaultPerms(false, $this->parent);
|
||||
Server::getInstance()->getPluginManager()->unsubscribeFromDefaultPerms(true, $this->parent);
|
||||
|
||||
$this->permissions = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool[] $children
|
||||
* @param bool $invert
|
||||
* @param PermissionAttachment $attachment
|
||||
*/
|
||||
public function calculateChildPermissions(array $children, $invert, $attachment){
|
||||
foreach(array_keys($children) as $name){
|
||||
$perm = Server::getInstance()->getPluginManager()->getPermission($name);
|
||||
$value = $invert === true ? !$children[$name] : $children[$name];
|
||||
$this->permissions[$name] = new PermissionAttachmentInfo($this->parent, $name, $attachment, $value);
|
||||
Server::getInstance()->getPluginManager()->subscribeToPermission($name, $this->parent);
|
||||
|
||||
if($perm instanceof Permission){
|
||||
$this->calculateChildPermissions($perm->getChildren(), !$value, $attachment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PermissionAttachmentInfo[]
|
||||
*/
|
||||
public function getEffectivePermissions(){
|
||||
return $this->permissions;
|
||||
}
|
||||
}
|
254
src/pocketmine/permission/Permission.php
Normal file
254
src/pocketmine/permission/Permission.php
Normal file
@ -0,0 +1,254 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Permission related classes
|
||||
*/
|
||||
namespace pocketmine\permission;
|
||||
|
||||
use pocketmine\Server;
|
||||
|
||||
/**
|
||||
* Represents a permission
|
||||
*/
|
||||
class Permission{
|
||||
const DEFAULT_OP = "op";
|
||||
const DEFAULT_NOT_OP = "notop";
|
||||
const DEFAULT_TRUE = "true";
|
||||
const DEFAULT_FALSE = "false";
|
||||
|
||||
public static $DEFAULT_PERMISSION = self::DEFAULT_OP;
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getByName($value){
|
||||
if(is_bool($value)){
|
||||
if($value === true){
|
||||
return "true";
|
||||
}else{
|
||||
return "false";
|
||||
}
|
||||
}
|
||||
switch(strtolower($value)){
|
||||
case "op":
|
||||
case "isop":
|
||||
case "operator":
|
||||
case "isoperator":
|
||||
case "admin":
|
||||
case "isadmin":
|
||||
return self::DEFAULT_OP;
|
||||
|
||||
case "!op":
|
||||
case "notop":
|
||||
case "!operator":
|
||||
case "notoperator":
|
||||
case "!admin":
|
||||
case "notadmin":
|
||||
return self::DEFAULT_NOT_OP;
|
||||
|
||||
case "true":
|
||||
return self::DEFAULT_TRUE;
|
||||
|
||||
default:
|
||||
return self::DEFAULT_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/** @var string */
|
||||
private $name;
|
||||
|
||||
/** @var string */
|
||||
private $description;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $children = array();
|
||||
|
||||
/** @var string */
|
||||
private $defaultValue;
|
||||
|
||||
/**
|
||||
* Creates a new Permission object to be attached to Permissible objects
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $description
|
||||
* @param string $defaultValue
|
||||
* @param Permission[] $children
|
||||
*/
|
||||
public function __construct($name, $description = null, $defaultValue = null, array $children = array()){
|
||||
$this->name = $name;
|
||||
$this->description = $description !== null ? $description : "";
|
||||
$this->defaultValue = $defaultValue !== null ? $defaultValue : self::$DEFAULT_PERMISSION;
|
||||
$this->children = $children;
|
||||
|
||||
$this->recalculatePermissibles();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(){
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function &getChildren(){
|
||||
return $this->children;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDefault(){
|
||||
return $this->defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setDefault($value){
|
||||
if($value !== $this->defaultValue){
|
||||
$this->defaultValue = $value;
|
||||
$this->recalculatePermissibles();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription(){
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setDescription($value){
|
||||
$this->description = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Permissible[]
|
||||
*/
|
||||
public function getPermissibles(){
|
||||
return Server::getInstance()->getPluginManager()->getPermissionSubscriptions($this->name);
|
||||
}
|
||||
|
||||
public function recalculatePermissibles(){
|
||||
$perms = $this->getPermissibles();
|
||||
|
||||
Server::getInstance()->getPluginManager()->recalculatePermissionDefaults($this);
|
||||
|
||||
foreach($perms as $p){
|
||||
$p->recalculatePermissions();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|Permission $name
|
||||
* @param $value
|
||||
*
|
||||
* @return Permission|void
|
||||
*/
|
||||
public function addParent($name, $value){
|
||||
if($name instanceof Permission){
|
||||
$name->getChildren()[$this->getName()] = $value;
|
||||
$name->recalculatePermissibles();
|
||||
}else{
|
||||
$perm = Server::getInstance()->getPluginManager()->getPermission($name);
|
||||
if($perm === null){
|
||||
$perm = new Permission($name);
|
||||
Server::getInstance()->getPluginManager()->addPermission($perm);
|
||||
}
|
||||
|
||||
$this->addParent($perm, $value);
|
||||
|
||||
return $perm;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param $default
|
||||
*
|
||||
* @return Permission[]
|
||||
*/
|
||||
public static function loadPermissions(array $data, $default = self::DEFAULT_OP){
|
||||
$result = array();
|
||||
foreach($data as $key => $entry){
|
||||
$result[] = self::loadPermission($key, $entry, $default, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $data
|
||||
* @param string $default
|
||||
* @param array $output
|
||||
*
|
||||
* @return Permission
|
||||
*/
|
||||
public static function loadPermission($name, array $data, $default = self::DEFAULT_OP, &$output = array()){
|
||||
$desc = null;
|
||||
$children = array();
|
||||
if(isset($data["default"])){
|
||||
$value = Permission::getByName($data["default"]);
|
||||
if($value !== null){
|
||||
$default = $value;
|
||||
}else{
|
||||
trigger_error("'default' key contained unknown value", E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($data["children"])){
|
||||
if(is_array($data["children"])){
|
||||
foreach($data["children"] as $k => $v){
|
||||
if(is_array($v)){
|
||||
if(($perm = self::loadPermission($k, $v, $default, $output)) !== null){
|
||||
$output[] = $perm;
|
||||
}
|
||||
}
|
||||
$children[$k] = true;
|
||||
}
|
||||
}else{
|
||||
trigger_error("'children' key is of wrong type", E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($data["description"])){
|
||||
$desc = $data["description"];
|
||||
}
|
||||
|
||||
return new Permission($name, $desc, $default, $children);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
113
src/pocketmine/permission/PermissionAttachment.php
Normal file
113
src/pocketmine/permission/PermissionAttachment.php
Normal file
@ -0,0 +1,113 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\permission;
|
||||
|
||||
use pocketmine\plugin\Plugin;
|
||||
|
||||
class PermissionAttachment{
|
||||
/** @var PermissionRemovedExecutor */
|
||||
private $removed = null;
|
||||
|
||||
/**
|
||||
* @var bool[]
|
||||
*/
|
||||
private $permissions = array();
|
||||
|
||||
/** @var Permissible */
|
||||
private $permissible;
|
||||
|
||||
/** @var Plugin */
|
||||
private $plugin;
|
||||
|
||||
/**
|
||||
* @param Plugin $plugin
|
||||
* @param Permissible $permissible
|
||||
*/
|
||||
public function __construct(Plugin $plugin, Permissible $permissible){
|
||||
if(!$plugin->isEnabled()){
|
||||
trigger_error("Plugin " . $plugin->getDescription()->getName() . " is disabled", E_USER_WARNING);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->permissible = $permissible;
|
||||
$this->plugin = $plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Plugin
|
||||
*/
|
||||
public function getPlugin(){
|
||||
return $this->plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PermissionRemovedExecutor $ex
|
||||
*/
|
||||
public function setRemovalCallback(PermissionRemovedExecutor $ex){
|
||||
$this->removed = $ex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PermissionRemovedExecutor
|
||||
*/
|
||||
public function getRemovalCallback(){
|
||||
return $this->removed;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Permissible
|
||||
*/
|
||||
public function getPermissible(){
|
||||
return $this->permissible;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool[]
|
||||
*/
|
||||
public function getPermissions(){
|
||||
return $this->permissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|Permission $name
|
||||
* @param bool $value
|
||||
*/
|
||||
public function setPermission($name, $value){
|
||||
$this->permissions[$name instanceof Permission ? $name->getName() : $name] = $value;
|
||||
$this->permissible->recalculatePermissions();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|Permission $name
|
||||
*/
|
||||
public function unsetPermission($name){
|
||||
unset($this->permissions[$name instanceof Permission ? $name->getName() : $name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function remove(){
|
||||
$this->permissible->removeAttachment($this);
|
||||
}
|
||||
}
|
84
src/pocketmine/permission/PermissionAttachmentInfo.php
Normal file
84
src/pocketmine/permission/PermissionAttachmentInfo.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\permission;
|
||||
|
||||
|
||||
class PermissionAttachmentInfo{
|
||||
/** @var Permissible */
|
||||
private $permissible;
|
||||
|
||||
/** @var string */
|
||||
private $permission;
|
||||
|
||||
/** @var PermissionAttachment */
|
||||
private $attachment;
|
||||
|
||||
/** @var bool */
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* @param Permissible $permissible
|
||||
* @param string $permission
|
||||
* @param PermissionAttachment $attachment
|
||||
* @param bool $value
|
||||
*/
|
||||
public function __construct(Permissible $permissible, $permission, $attachment, $value){
|
||||
if($permission === null){
|
||||
trigger_error("Permission may not be null", E_USER_WARNING);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->permissible = $permissible;
|
||||
$this->permission = $permission;
|
||||
$this->attachment = $attachment;
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Permissible
|
||||
*/
|
||||
public function getPermissible(){
|
||||
return $this->permissible;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPermission(){
|
||||
return $this->permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PermissionAttachment
|
||||
*/
|
||||
public function getAttachment(){
|
||||
return $this->attachment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getValue(){
|
||||
return $this->value;
|
||||
}
|
||||
}
|
33
src/pocketmine/permission/PermissionRemovedExecutor.php
Normal file
33
src/pocketmine/permission/PermissionRemovedExecutor.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\permission;
|
||||
|
||||
|
||||
interface PermissionRemovedExecutor{
|
||||
|
||||
/**
|
||||
* @param PermissionAttachment $attachment
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function attachmentRemoved(PermissionAttachment $attachment);
|
||||
}
|
41
src/pocketmine/permission/ServerOperator.php
Normal file
41
src/pocketmine/permission/ServerOperator.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\permission;
|
||||
|
||||
|
||||
interface ServerOperator{
|
||||
/**
|
||||
* Checks if the current object has operator permissions
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isOp();
|
||||
|
||||
/**
|
||||
* Sets the operator permission for the current object
|
||||
*
|
||||
* @param bool $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setOp($value);
|
||||
}
|
Reference in New Issue
Block a user