Elgg  Version 3.0
access.php
Go to the documentation of this file.
1 <?php
21 
52 function elgg_set_ignore_access($ignore = true) {
53  return elgg()->session->setIgnoreAccess($ignore);
54 }
55 
64  return elgg()->session->getIgnoreAccess();
65 }
66 
82 function get_access_list($user_guid = 0, $ignored = 0, $flush = false) {
83  return _elgg_services()->accessCollections->getAccessList($user_guid, $flush);
84 }
85 
111 function get_access_array($user_guid = 0, $ignored = 0, $flush = false) {
112  return _elgg_services()->accessCollections->getAccessArray($user_guid, $flush);
113 }
114 
127 function get_default_access(ElggUser $user = null, array $input_params = []) {
128  // site default access
129  $default_access = _elgg_config()->default_access;
130 
131  // user default access if enabled
133  $user = $user ? $user : elgg()->session->getLoggedInUser();
134  if ($user) {
135  $user_access = $user->getPrivateSetting('elgg_default_access');
136  if ($user_access !== null) {
137  $default_access = $user_access;
138  }
139  }
140  }
141 
142  $params = [
143  'user' => $user,
144  'default_access' => $default_access,
145  'input_params' => $input_params,
146  ];
147  return _elgg_services()->hooks->trigger('default', 'access', $params, $default_access);
148 }
149 
156 function access_show_hidden_entities($show_hidden) {
157  elgg()->session->setDisabledEntityVisibility($show_hidden);
158 }
159 
166  return elgg()->session->getDisabledEntityVisibility();
167 }
168 
188 function has_access_to_entity($entity, $user = null) {
189  return _elgg_services()->accessCollections->hasAccessToEntity($entity, $user);
190 }
191 
218 function get_write_access_array($user_guid = 0, $ignored = 0, $flush = false, array $input_params = []) {
219  return _elgg_services()->accessCollections->getWriteAccessArray($user_guid, $flush, $input_params);
220 }
221 
236 function can_edit_access_collection($collection_id, $user_guid = null) {
237  return _elgg_services()->accessCollections->canEdit($collection_id, $user_guid);
238 }
239 
260  return _elgg_services()->accessCollections->create($name, $owner_guid, $subtype);
261 }
262 
279 function update_access_collection($collection_id, $members) {
280  return _elgg_services()->accessCollections->update($collection_id, $members);
281 }
282 
292 function delete_access_collection($collection_id) {
293  return _elgg_services()->accessCollections->delete($collection_id);
294 }
295 
308 function get_access_collection($collection_id) {
309  return _elgg_services()->accessCollections->get($collection_id);
310 }
311 
324 function add_user_to_access_collection($user_guid, $collection_id) {
325  return _elgg_services()->accessCollections->addUser($user_guid, $collection_id);
326 }
327 
341  return _elgg_services()->accessCollections->removeUser($user_guid, $collection_id);
342 }
343 
351  return _elgg_services()->accessCollections->getEntityCollections($options);
352 }
353 
364 function get_members_of_access_collection($collection_id, $guids_only = false, array $options = []) {
365  if (!isset($options['limit'])) {
366  $options['limit'] = 0;
367  }
368 
369  if (!$guids_only) {
370  return _elgg_services()->accessCollections->getMembers($collection_id, $options);
371  }
372 
373  $guids = [];
374  $options['callback'] = false;
375  $rows = _elgg_services()->accessCollections->getMembers($collection_id, $options);
376  foreach ($rows as $row) {
377  $guids[] = (int) $row->guid;
378  }
379  return $guids;
380 }
397 function get_readable_access_level($entity_access_id) {
398  return _elgg_services()->accessCollections->getReadableAccessLevel($entity_access_id);
399 }
400 
408 function access_init() {
409  _elgg_services()->accessCollections->markInitComplete();
410 }
411 
425 function access_friends_acl_create(\Elgg\Event $event) {
426  $user = $event->getObject();
427  if (!($user instanceof \ElggUser)) {
428  return;
429  }
430 
431  create_access_collection('friends', $user->guid, 'friends');
432 }
433 
447 function access_friends_acl_add_friend(\Elgg\Event $event) {
448  $relationship_object = $event->getObject();
449  if (!($relationship_object instanceof \ElggRelationship)) {
450  return;
451  }
452 
453  if ($relationship_object->relationship !== 'friend') {
454  return;
455  }
456 
457  $user = get_user($relationship_object->guid_one);
458  $friend = get_user($relationship_object->guid_two);
459 
460  if (!$user || !$friend) {
461  return;
462  }
463 
464  $acl = $user->getOwnedAccessCollection('friends');
465  if (empty($acl)) {
466  return;
467  }
468  $acl->addMember($friend->guid);
469 }
470 
484 function access_friends_acl_remove_friend(\Elgg\Event $event) {
485  $relationship_object = $event->getObject();
486  if (!($relationship_object instanceof \ElggRelationship)) {
487  return;
488  }
489 
490  if ($relationship_object->relationship !== 'friend') {
491  return;
492  }
493 
494  $user = get_user($relationship_object->guid_one);
495  $friend = get_user($relationship_object->guid_two);
496 
497  if (!$user || !$friend) {
498  return;
499  }
500 
501  $acl = $user->getOwnedAccessCollection('friends');
502  if (empty($acl)) {
503  return;
504  }
505 
506  $acl->removeMember($friend->guid);
507 }
508 
522 function access_friends_acl_get_name(\Elgg\Hook $hook) {
523  $access_collection = $hook->getParam('access_collection');
524  if (!($access_collection instanceof ElggAccessCollection)) {
525  return;
526  }
527 
528  if ($access_collection->getSubtype() !== 'friends') {
529  return;
530  }
531 
532  return elgg_echo('access:label:friends');
533 }
534 
548 function access_test($hook, $type, $value, $params) {
550  return $value;
551 }
552 
556 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
557  // Tell the access functions the system has booted, plugins are loaded,
558  // and the user is logged in so it can start caching
559  $events->registerHandler('ready', 'system', 'access_init');
560 
561  // friends ACL events
562  $events->registerHandler('create', 'user', 'access_friends_acl_create');
563  $events->registerHandler('create', 'relationship', 'access_friends_acl_add_friend');
564  $events->registerHandler('delete', 'relationship', 'access_friends_acl_remove_friend');
565  $hooks->registerHandler('access_collection:name', 'access_collection', 'access_friends_acl_get_name');
566 
567  $hooks->registerHandler('unit_test', 'system', 'access_test');
568 };
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
get_access_list($user_guid=0, $ignored=0, $flush=false)
Return a string of access_ids for $user_guid appropriate for inserting into an SQL IN clause...
Definition: access.php:82
get_user($guid)
Get a user object from a GUID.
Definition: users.php:40
foreach($keys as $key=> $default_value) $entity
Definition: access.php:47
delete_access_collection($collection_id)
Deletes a specified access collection and its membership.
Definition: access.php:292
$rows
Definition: redis.php:20
access_init()
A quick and dirty way to make sure the access permissions have been correctly set up...
Definition: access.php:408
access_friends_acl_get_name(\Elgg\Hook $hook)
Return the name of a friends ACL.
Definition: access.php:522
get_write_access_array($user_guid=0, $ignored=0, $flush=false, array $input_params=[])
Returns an array of access permissions that the user is allowed to save content with.
Definition: access.php:218
Events service.
$subtype
Definition: delete.php:22
$input_params
Definition: livesearch.php:12
$type
Definition: delete.php:21
get_access_collection($collection_id)
Get a specified access collection.
Definition: access.php:308
update_access_collection($collection_id, $members)
Updates the membership in an access collection.
Definition: access.php:279
elgg_get_access_collections($options=[])
Returns access collections.
Definition: access.php:350
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
access_test($hook, $type, $value, $params)
Runs unit tests for the access library.
Definition: access.php:548
remove_user_from_access_collection($user_guid, $collection_id)
Removes a user from an access collection.
Definition: access.php:340
$options
Elgg admin footer.
Definition: footer.php:6
elgg_get_ignore_access()
Get current ignore access setting.
Definition: access.php:63
$params
Definition: access.php:32
$user_guid
Validate a user.
Definition: validate.php:6
$owner_guid
Configuration exception.
Base class for events and hooks.
global $ENTITY_SHOW_HIDDEN_OVERRIDE
Allow disabled entities and metadata to be returned by getter functions.
Definition: access.php:20
elgg_set_ignore_access($ignore=true)
Set if Elgg&#39;s access system should be ignored.
Definition: access.php:52
add_user_to_access_collection($user_guid, $collection_id)
Adds a user to an access collection.
Definition: access.php:324
$user
Definition: ban.php:7
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$CONFIG allow_user_default_access
Are users allow to enter their own default access levels.
Definition: config.php:163
create_access_collection($name, $owner_guid=0, $subtype=null)
Creates a new access collection.
Definition: access.php:259
get_readable_access_level($entity_access_id)
Return the name of an ACCESS_* constant or an access collection, but only if the logged in user has w...
Definition: access.php:397
access_get_show_hidden_status()
Return current status of showing disabled entities.
Definition: access.php:165
access_friends_acl_remove_friend(\Elgg\Event $event)
Add the friend to the user friends ACL.
Definition: access.php:484
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
_elgg_config()
Get the Elgg config service.
$value
Definition: debugging.php:7
access_show_hidden_entities($show_hidden)
Show or hide disabled entities.
Definition: access.php:156
get_access_array($user_guid=0, $ignored=0, $flush=false)
Returns an array of access IDs a user is permitted to see.
Definition: access.php:111
class
Definition: placeholder.php:21
has_access_to_entity($entity, $user=null)
Can a user access an entity.
Definition: access.php:188
get_members_of_access_collection($collection_id, $guids_only=false, array $options=[])
Get all of members of an access collection.
Definition: access.php:364
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
can_edit_access_collection($collection_id, $user_guid=null)
Can the user change this access collection?
Definition: access.php:236
access_friends_acl_add_friend(\Elgg\Event $event)
Adds the friend to the user friend ACL.
Definition: access.php:447
get_default_access(ElggUser $user=null, array $input_params=[])
Gets the default access permission.
Definition: access.php:127
access_friends_acl_create(\Elgg\Event $event)
Creates a Friends ACL for a user.
Definition: access.php:425
var elgg
Definition: elgglib.js:4
if(!$user instanceof ElggUser) $default_access