PocketMine-MP/src/permission/PermissionParser.php
Dylan K. Taylor cb16f5c142 Merge commit '260ac47588c76a2e6814cfba46773a990fb8c5da'
# Conflicts:
#	resources/vanilla
#	src/Server.php
#	src/lang/Language.php
#	src/network/mcpe/protocol/AddItemActorPacket.php
#	src/network/mcpe/protocol/AddPlayerPacket.php
#	src/network/mcpe/protocol/SetActorDataPacket.php
#	src/network/mcpe/serializer/NetworkBinaryStream.php
#	src/permission/Permission.php
#	src/pocketmine/block/Leaves.php
#	src/pocketmine/entity/DataPropertyManager.php
#	src/pocketmine/entity/Entity.php
#	src/pocketmine/item/Banner.php
#	src/pocketmine/item/Item.php
#	src/pocketmine/level/format/io/LevelProvider.php
#	src/pocketmine/level/format/io/LevelProviderManager.php
#	src/pocketmine/network/mcpe/protocol/AddActorPacket.php
#	src/pocketmine/network/mcpe/protocol/LoginPacket.php
#	src/pocketmine/tile/Banner.php
#	src/scheduler/BulkCurlTask.php
#	src/updater/AutoUpdater.php
#	src/utils/Config.php
#	src/utils/Utils.php
#	src/world/generator/Flat.php
#	src/world/generator/Generator.php
2020-01-31 21:07:34 +00:00

156 lines
3.9 KiB
PHP

<?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;
use function is_array;
use function is_bool;
use function ksort;
use function strtolower;
class PermissionParser{
/**
* @param bool|string $value
*
* @throws \InvalidArgumentException
*/
public static function defaultFromString($value) : string{
if(is_bool($value)){
if($value){
return "true";
}else{
return "false";
}
}
switch(strtolower($value)){
case "op":
case "isop":
case "operator":
case "isoperator":
case "admin":
case "isadmin":
return Permission::DEFAULT_OP;
case "!op":
case "notop":
case "!operator":
case "notoperator":
case "!admin":
case "notadmin":
return Permission::DEFAULT_NOT_OP;
case "true":
return Permission::DEFAULT_TRUE;
case "false":
return Permission::DEFAULT_FALSE;
}
throw new \InvalidArgumentException("Unknown permission default name \"$value\"");
}
/**
* @param mixed[][] $data
* @phpstan-param array<string, array<string, mixed>> $data
*
* @return Permission[]
*/
public static function loadPermissions(array $data, string $default = Permission::DEFAULT_OP) : array{
$result = [];
foreach($data as $key => $entry){
$result[] = self::loadPermission($key, $entry, $default, $result);
}
return $result;
}
/**
* @param mixed[] $data
* @param Permission[] $output reference parameter
* @phpstan-param array<string, mixed> $data
*
* @throws \Exception
*/
public static function loadPermission(string $name, array $data, string $default = Permission::DEFAULT_OP, array &$output = []) : Permission{
$desc = null;
$children = [];
if(isset($data["default"])){
$default = PermissionParser::defaultFromString($data["default"]);
}
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{
throw new \InvalidStateException("'children' key is of wrong type");
}
}
if(isset($data["description"])){
$desc = $data["description"];
}
return new Permission($name, $desc, $default, $children);
}
/**
* @param Permission[] $permissions
*/
public static function emitPermissions(array $permissions) : array{
$result = [];
foreach($permissions as $permission){
$result[$permission->getName()] = self::emitPermission($permission);
}
ksort($result);
return $result;
}
private static function emitPermission(Permission $permission) : array{
$result = [
"description" => $permission->getDescription(),
"default" => $permission->getDefault()
];
$children = [];
foreach($permission->getChildren() as $name => $bool){
//TODO: really? wtf??? this system is so overengineered it makes my head hurt...
$child = PermissionManager::getInstance()->getPermission($name);
if($child === null){
throw new \UnexpectedValueException("Permission child should be a registered permission");
}
$children[$name] = self::emitPermission($child);
}
if(!empty($children)){
ksort($children);
$result["children"] = $children;
}
return $result;
}
}