PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Permission.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 
25 namespace pocketmine\permission;
26 
28 
32 class Permission{
33  const DEFAULT_OP = "op";
34  const DEFAULT_NOT_OP = "notop";
35  const DEFAULT_TRUE = "true";
36  const DEFAULT_FALSE = "false";
37 
38  public static $DEFAULT_PERMISSION = self::DEFAULT_OP;
39 
45  public static function getByName($value){
46  if(is_bool($value)){
47  if($value === true){
48  return "true";
49  }else{
50  return "false";
51  }
52  }
53  switch(strtolower($value)){
54  case "op":
55  case "isop":
56  case "operator":
57  case "isoperator":
58  case "admin":
59  case "isadmin":
60  return self::DEFAULT_OP;
61 
62  case "!op":
63  case "notop":
64  case "!operator":
65  case "notoperator":
66  case "!admin":
67  case "notadmin":
68  return self::DEFAULT_NOT_OP;
69 
70  case "true":
71  return self::DEFAULT_TRUE;
72 
73  default:
74  return self::DEFAULT_FALSE;
75  }
76  }
77 
79  private $name;
80 
82  private $description;
83 
87  private $children = [];
88 
90  private $defaultValue;
91 
100  public function __construct($name, $description = null, $defaultValue = null, array $children = []){
101  $this->name = $name;
102  $this->description = $description !== null ? $description : "";
103  $this->defaultValue = $defaultValue !== null ? $defaultValue : self::$DEFAULT_PERMISSION;
104  $this->children = $children;
105 
106  $this->recalculatePermissibles();
107  }
108 
112  public function getName(){
113  return $this->name;
114  }
115 
119  public function &getChildren(){
120  return $this->children;
121  }
122 
126  public function getDefault(){
127  return $this->defaultValue;
128  }
129 
133  public function setDefault($value){
134  if($value !== $this->defaultValue){
135  $this->defaultValue = $value;
136  $this->recalculatePermissibles();
137  }
138  }
139 
143  public function getDescription(){
144  return $this->description;
145  }
146 
150  public function setDescription($value){
151  $this->description = $value;
152  }
153 
157  public function getPermissibles(){
158  return Server::getInstance()->getPluginManager()->getPermissionSubscriptions($this->name);
159  }
160 
161  public function recalculatePermissibles(){
162  $perms = $this->getPermissibles();
163 
164  Server::getInstance()->getPluginManager()->recalculatePermissionDefaults($this);
165 
166  foreach($perms as $p){
167  $p->recalculatePermissions();
168  }
169  }
170 
171 
178  public function addParent($name, $value){
179  if($name instanceof Permission){
180  $name->getChildren()[$this->getName()] = $value;
181  $name->recalculatePermissibles();
182  }else{
183  $perm = Server::getInstance()->getPluginManager()->getPermission($name);
184  if($perm === null){
185  $perm = new Permission($name);
186  Server::getInstance()->getPluginManager()->addPermission($perm);
187  }
188 
189  $this->addParent($perm, $value);
190 
191  return $perm;
192  }
193  }
194 
201  public static function loadPermissions(array $data, $default = self::DEFAULT_OP){
202  $result = [];
203  foreach($data as $key => $entry){
204  $result[] = self::loadPermission($key, $entry, $default, $result);
205  }
206 
207  return $result;
208  }
209 
220  public static function loadPermission($name, array $data, $default = self::DEFAULT_OP, &$output = []){
221  $desc = null;
222  $children = [];
223  if(isset($data["default"])){
224  $value = Permission::getByName($data["default"]);
225  if($value !== null){
226  $default = $value;
227  }else{
228  throw new \InvalidStateException("'default' key contained unknown value");
229  }
230  }
231 
232  if(isset($data["children"])){
233  if(is_array($data["children"])){
234  foreach($data["children"] as $k => $v){
235  if(is_array($v)){
236  if(($perm = self::loadPermission($k, $v, $default, $output)) !== null){
237  $output[] = $perm;
238  }
239  }
240  $children[$k] = true;
241  }
242  }else{
243  throw new \InvalidStateException("'children' key is of wrong type");
244  }
245  }
246 
247  if(isset($data["description"])){
248  $desc = $data["description"];
249  }
250 
251  return new Permission($name, $desc, $default, $children);
252 
253  }
254 
255 
256 }
static getInstance()
Definition: Server.php:1444
static loadPermissions(array $data, $default=self::DEFAULT_OP)
Definition: Permission.php:201
static loadPermission($name, array $data, $default=self::DEFAULT_OP, &$output=[])
Definition: Permission.php:220
__construct($name, $description=null, $defaultValue=null, array $children=[])
Definition: Permission.php:100