Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
UserCapabilities.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
8 use ElggEntity;
9 use ElggMetadata;
10 use ElggSession;
11 use InvalidArgumentException;
12 
22 
26  private $hooks;
27 
31  private $entities;
32 
36  private $session;
37 
45  public function __construct(PluginHooksService $hooks, EntityTable $entities, ElggSession $session) {
46  $this->hooks = $hooks;
47  $this->entities = $entities;
48  $this->session = $session;
49  }
50 
62  public function canEdit(ElggEntity $entity, $user_guid = 0) {
63  try {
64  $user = $this->entities->getUserForPermissionsCheck($user_guid);
65  } catch (UserFetchFailureException $e) {
66  return false;
67  }
68 
69  // Test user if possible - should default to false unless a plugin hook says otherwise
70  $default = call_user_func(function () use ($entity, $user) {
71  if (!$user) {
72  return false;
73  }
74 
75  // favor the persisted attributes if not saved
76  $attrs = array_merge(
77  [
78  'owner_guid' => $entity->owner_guid,
79  'container_guid' => $entity->container_guid,
80  ], $entity->getOriginalAttributes()
81  );
82 
83  if ($attrs['owner_guid'] == $user->guid) {
84  return true;
85  }
86 
87  if ($attrs['container_guid'] == $user->guid) {
88  return true;
89  }
90 
91  if ($entity->guid == $user->guid) {
92  return true;
93  }
94 
95  $container = $this->entities->get($attrs['container_guid']);
96 
97  return ($container && $container->canEdit($user->guid));
98  });
99 
100  $params = array('entity' => $entity, 'user' => $user);
101  return $this->hooks->trigger('permissions_check', $entity->getType(), $params, $default);
102  }
103 
116  public function canDelete(ElggEntity $entity, $user_guid = 0) {
117  try {
118  $user = $this->entities->getUserForPermissionsCheck($user_guid);
119  } catch (UserFetchFailureException $e) {
120  return false;
121  }
122 
123  $return = $entity->canEdit($user_guid);
124 
125  $params = [
126  'entity' => $entity,
127  'user' => $user
128  ];
129  return $this->hooks->trigger('permissions_check:delete', $entity->getType(), $params, $return);
130  }
131 
149  if (!$entity->guid) {
150  // @todo cannot edit metadata on unsaved entity?
151  return false;
152  }
153 
154  try {
155  $user = $this->entities->getUserForPermissionsCheck($user_guid);
156  } catch (UserFetchFailureException $e) {
157  return false;
158  }
159 
160  if ($user) {
161  $user_guid = $user->guid;
162  }
163 
164  // if metadata is not owned or owned by the user, then can edit
165  if ($metadata && ($metadata->owner_guid == 0 || $metadata->owner_guid == $user_guid)) {
166  $return = true;
167  } else {
168  $return = $entity->canEdit($user_guid);
169  }
170 
171  // metadata and user may be null
172  $params = [
173  'entity' => $entity,
174  'user' => $user,
175  'metadata' => $metadata
176  ];
177  return $this->hooks->trigger('permissions_check:metadata', $entity->getType(), $params, $return);
178  }
179 
191  if (!$annotation) {
192  return false;
193  }
194  try {
195  $user = $this->entities->getUserForPermissionsCheck($user_guid);
196  } catch (UserFetchFailureException $e) {
197  return false;
198  }
199 
200  $result = false;
201 
202  if ($user) {
203  // If the owner of annotation is the specified user, they can edit.
204  if ($annotation->owner_guid == $user->guid) {
205  $result = true;
206  }
207 
208  // If the user can edit the entity this is attached to, they can edit.
209  if ($result == false && $entity->canEdit($user->guid)) {
210  $result = true;
211  }
212  }
213 
214  // Trigger plugin hook - note that $user may be null
215  $params = [
216  'entity' => $entity,
217  'user' => $user,
218  'annotation' => $annotation
219  ];
220 
221  return $this->hooks->trigger('permissions_check', 'annotation', $params, $result);
222  }
223 
235  public function canWriteToContainer(ElggEntity $entity, $user_guid = 0, $type = 'all', $subtype = 'all') {
236  try {
237  $user = $this->entities->getUserForPermissionsCheck($user_guid);
238  } catch (UserFetchFailureException $e) {
239  return false;
240  }
241 
242  if ($user) {
243  $user_guid = $user->guid;
244  }
245 
246  $return = false;
247  if ($entity) {
248  // If the user can edit the container, they can also write to it
249  if ($entity->canEdit($user_guid)) {
250  $return = true;
251  }
252  }
253 
254  // See if anyone else has anything to say
255  $params = [
256  'container' => $entity,
257  'user' => $user,
258  'subtype' => $subtype
259  ];
260  return $this->hooks->trigger('container_permissions_check', $type, $params, $return);
261  }
262 
274  public function canComment(ElggEntity $entity, $user_guid = 0) {
275  try {
276  $user = $this->entities->getUserForPermissionsCheck($user_guid);
277  } catch (UserFetchFailureException $e) {
278  return false;
279  }
280 
281  // By default, we don't take a position of whether commenting is allowed
282  // because it is handled by the subclasses of \ElggEntity
283  $params = [
284  'entity' => $entity,
285  'user' => $user
286  ];
287  return $this->hooks->trigger('permissions_check:comment', $entity->getType(), $params, null);
288  }
289 
305  public function canAnnotate(ElggEntity $entity, $user_guid = 0, $annotation_name = '') {
306  if ($annotation_name === null || $annotation_name === false) {
307  // accepting these for BC
308  $annotation_name = '';
309  } elseif (!is_string($annotation_name)) {
310  throw new InvalidArgumentException(__METHOD__ . ' expects \$annotation_name to be a string');
311  }
312 
313  try {
314  $user = $this->entities->getUserForPermissionsCheck($user_guid);
315  } catch (UserFetchFailureException $e) {
316  return false;
317  }
318 
319  $return = (bool) $user;
320 
321  $params = [
322  'entity' => $entity,
323  'user' => $user,
324  'annotation_name' => $annotation_name,
325  ];
326 
327  if (!empty($annotation_name)) {
328  $return = $this->hooks->trigger("permissions_check:annotate:$annotation_name", $entity->getType(), $params, $return);
329  }
330 
331  return $this->hooks->trigger('permissions_check:annotate', $entity->getType(), $params, $return);
332  }
333 
334 }
$annotation
Elgg default annotation view.
Definition: default.php:10
canEditMetadata(ElggEntity $entity, $user_guid=0, ElggMetadata $metadata=null)
Can a user edit metadata on this entity?
$e
Definition: metadata.php:12
$metadata
Definition: entity.php:19
$subtype
Definition: delete.php:28
$return
Definition: opendd.php:15
$default
Definition: checkbox.php:35
canAnnotate(ElggEntity $entity, $user_guid=0, $annotation_name= '')
Can a user annotate an entity?
__construct(PluginHooksService $hooks, EntityTable $entities, ElggSession $session)
Constructor.
canEdit($user_guid=0)
Can a user edit this entity?
canComment(ElggEntity $entity, $user_guid=0)
Can a user comment on an entity?
$params
Definition: login.php:72
$container
Definition: delete.php:29
canDelete(ElggEntity $entity, $user_guid=0)
Can a user delete this entity?
Exception indicating a user could not be looked up for a permissions check.
$user
Definition: ban.php:13
$attrs
Definition: ajax_loader.php:30
canEditAnnotation(ElggEntity $entity, $user_guid=0, ElggAnnotation $annotation=null)
Determines whether or not the user can edit this annotation.
getType()
Returns the entity type.
$entity
Definition: delete.php:7
getOriginalAttributes()
Get the original values of attribute(s) that have been modified since the entity was persisted...
Definition: ElggEntity.php:246
canWriteToContainer(ElggEntity $entity, $user_guid=0, $type= 'all', $subtype= 'all')
Can a user add an entity to this container.
canEdit(ElggEntity $entity, $user_guid=0)
Can a user edit this entity?
$user_guid
Avatar remove action.
Definition: remove.php:6
WARNING: API IN FLUX.
$session
Definition: login.php:9
if(!$display_name) $type
Definition: delete.php:27