PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
PermissionParser.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\permission;
25
27use function is_bool;
28use function strtolower;
29
31
32 public const DEFAULT_OP = "op";
33 public const DEFAULT_NOT_OP = "notop";
34 public const DEFAULT_TRUE = "true";
35 public const DEFAULT_FALSE = "false";
36
37 public const DEFAULT_STRING_MAP = [
38 "op" => self::DEFAULT_OP,
39 "isop" => self::DEFAULT_OP,
40 "operator" => self::DEFAULT_OP,
41 "isoperator" => self::DEFAULT_OP,
42 "admin" => self::DEFAULT_OP,
43 "isadmin" => self::DEFAULT_OP,
44
45 "!op" => self::DEFAULT_NOT_OP,
46 "notop" => self::DEFAULT_NOT_OP,
47 "!operator" => self::DEFAULT_NOT_OP,
48 "notoperator" => self::DEFAULT_NOT_OP,
49 "!admin" => self::DEFAULT_NOT_OP,
50 "notadmin" => self::DEFAULT_NOT_OP,
51
52 "true" => self::DEFAULT_TRUE,
53 "false" => self::DEFAULT_FALSE,
54 ];
55
56 private const KEY_DEFAULT = "default";
57 private const KEY_CHILDREN = "children";
58 private const KEY_DESCRIPTION = "description";
59
63 public static function defaultFromString(bool|string $value) : string{
64 if(is_bool($value)){
65 return $value ? self::DEFAULT_TRUE : self::DEFAULT_FALSE;
66 }
67 $lower = strtolower($value);
68 if(isset(self::DEFAULT_STRING_MAP[$lower])){
69 return self::DEFAULT_STRING_MAP[$lower];
70 }
71
72 throw new PermissionParserException("Unknown permission default name \"$value\"");
73 }
74
83 public static function loadPermissions(array $data, string $default = self::DEFAULT_FALSE) : array{
84 $result = [];
85 foreach(Utils::stringifyKeys($data) as $name => $entry){
86 $desc = null;
87 if(isset($entry[self::KEY_DEFAULT])){
88 $default = PermissionParser::defaultFromString($entry[self::KEY_DEFAULT]);
89 }
90
91 if(isset($entry[self::KEY_CHILDREN])){
92 throw new PermissionParserException("Nested permission declarations are no longer supported. Declare each permission separately.");
93 }
94
95 if(isset($entry[self::KEY_DESCRIPTION])){
96 $desc = $entry[self::KEY_DESCRIPTION];
97 }
98
99 $result[$default][] = new Permission($name, $desc);
100 }
101 return $result;
102 }
103}
static defaultFromString(bool|string $value)
static loadPermissions(array $data, string $default=self::DEFAULT_FALSE)