PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
PermissibleBase.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 
22 namespace pocketmine\permission;
23 
28 
29 class PermissibleBase implements Permissible{
31  private $opable = null;
32 
34  private $parent;
35 
39  private $attachments = [];
40 
44  private $permissions = [];
45 
49  public function __construct(ServerOperator $opable){
50  $this->opable = $opable;
51  if($opable instanceof Permissible){
52  $this->parent = $opable;
53  }else{
54  $this->parent = $this;
55  }
56  }
57 
61  public function isOp(){
62  if($this->opable === null){
63  return false;
64  }else{
65  return $this->opable->isOp();
66  }
67  }
68 
74  public function setOp($value){
75  if($this->opable === null){
76  throw new \LogicException("Cannot change op value as no ServerOperator is set");
77  }else{
78  $this->opable->setOp($value);
79  }
80  }
81 
87  public function isPermissionSet($name){
88  return isset($this->permissions[$name instanceof Permission ? $name->getName() : $name]);
89  }
90 
96  public function hasPermission($name){
97  if($name instanceof Permission){
98  $name = $name->getName();
99  }
100 
101  if($this->isPermissionSet($name)){
102  return $this->permissions[$name]->getValue();
103  }
104 
105  if(($perm = Server::getInstance()->getPluginManager()->getPermission($name)) !== null){
106  $perm = $perm->getDefault();
107 
108  return $perm === Permission::DEFAULT_TRUE or ($this->isOp() and $perm === Permission::DEFAULT_OP) or (!$this->isOp() and $perm === Permission::DEFAULT_NOT_OP);
109  }else{
110  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);
111  }
112 
113  }
114 
126  public function addAttachment(Plugin $plugin, $name = null, $value = null){
127  if($plugin === null){
128  throw new PluginException("Plugin cannot be null");
129  }elseif(!$plugin->isEnabled()){
130  throw new PluginException("Plugin " . $plugin->getDescription()->getName() . " is disabled");
131  }
132 
133  $result = new PermissionAttachment($plugin, $this->parent);
134  $this->attachments[spl_object_hash($result)] = $result;
135  if($name !== null and $value !== null){
136  $result->setPermission($name, $value);
137  }
138 
139  $this->recalculatePermissions();
140 
141  return $result;
142  }
143 
149  public function removeAttachment(PermissionAttachment $attachment){
150  if($attachment === null){
151  throw new \InvalidStateException("Attachment cannot be null");
152  }
153 
154  if(isset($this->attachments[spl_object_hash($attachment)])){
155  unset($this->attachments[spl_object_hash($attachment)]);
156  if(($ex = $attachment->getRemovalCallback()) !== null){
157  $ex->attachmentRemoved($attachment);
158  }
159 
160  $this->recalculatePermissions();
161 
162  }
163 
164  }
165 
166  public function recalculatePermissions(){
167  Timings::$permissibleCalculationTimer->startTiming();
168 
169  $this->clearPermissions();
170  $defaults = Server::getInstance()->getPluginManager()->getDefaultPermissions($this->isOp());
171  Server::getInstance()->getPluginManager()->subscribeToDefaultPerms($this->isOp(), $this->parent);
172 
173  foreach($defaults as $perm){
174  $name = $perm->getName();
175  $this->permissions[$name] = new PermissionAttachmentInfo($this->parent, $name, null, true);
176  Server::getInstance()->getPluginManager()->subscribeToPermission($name, $this->parent);
177  $this->calculateChildPermissions($perm->getChildren(), false, null);
178  }
179 
180  foreach($this->attachments as $attachment){
181  $this->calculateChildPermissions($attachment->getPermissions(), false, $attachment);
182  }
183 
184  Timings::$permissibleCalculationTimer->stopTiming();
185  }
186 
187  public function clearPermissions(){
188  foreach(array_keys($this->permissions) as $name){
189  Server::getInstance()->getPluginManager()->unsubscribeFromPermission($name, $this->parent);
190  }
191 
192  Server::getInstance()->getPluginManager()->unsubscribeFromDefaultPerms(false, $this->parent);
193  Server::getInstance()->getPluginManager()->unsubscribeFromDefaultPerms(true, $this->parent);
194 
195  $this->permissions = [];
196  }
197 
203  private function calculateChildPermissions(array $children, $invert, $attachment){
204  foreach($children as $name => $v){
205  $perm = Server::getInstance()->getPluginManager()->getPermission($name);
206  $value = ($v xor $invert);
207  $this->permissions[$name] = new PermissionAttachmentInfo($this->parent, $name, $attachment, $value);
208  Server::getInstance()->getPluginManager()->subscribeToPermission($name, $this->parent);
209 
210  if($perm instanceof Permission){
211  $this->calculateChildPermissions($perm->getChildren(), !$value, $attachment);
212  }
213  }
214  }
215 
219  public function getEffectivePermissions(){
220  return $this->permissions;
221  }
222 }
removeAttachment(PermissionAttachment $attachment)
static getInstance()
Definition: Server.php:1444
addAttachment(Plugin $plugin, $name=null, $value=null)