Daniel Ansari’s blog Random software musings

September 30, 2008

Adding “Friends” access level to Elgg 1.1

Filed under: Open Source — Tags: , , — admin @ 10:43 am

Out of the box, Elgg 1.1 has three access levels: private, logged-in users, and public.

A few people have asked about how to add a further access level: friends. Currently, the only way to give friends access to an item is to create a friends collection, then specify that collection for the access level. The problem with this approach is that every you add or remove a friend, you need to update your collection manually.

The Elgg development philosophy is not to modify the core files, but instead to write plugins, if you need to modify any of the core functionality. However, in this case, there are no Elgg events that we can hook into, as access level is such a fundamental part of the core. Thus, we are forced to modify a couple of the core files.

Note that this won’t work for MySQL versions prior to 4.1, as they do not support subqueries.

Add line 147 in engine/lib/access.php:

  1. function get_access_sql_suffix($table_prefix = "")
  2. {
  3. global $CONFIG;
  4. global $ENTITY_SHOW_HIDDEN_OVERRIDE;
  5.  

Change line 167 in engine/lib/access.php:

  1. if (empty($sql))
  2. $sql = " ({$table_prefix}access_id in {$access} or ({$table_prefix}access_id = 0 and {$table_prefix}owner_guid = $owner) or ({$table_prefix}access_id = -1 and ({$table_prefix}owner_guid = $owner or {$table_prefix}owner_guid in (select guid_two from {$CONFIG->dbprefix}entity_relationships er where er.guid_one = $owner and er.relationship = 'friend'))))";

Change line 201 to the following:

  1. $tmp_access_array = array(0 => elgg_echo("PRIVATE"), -1 => elgg_echo("FRIENDS"), 1 => elgg_echo("LOGGED_IN"), 2 => elgg_echo("PUBLIC"));

The last change in core is to add line 190 in languages/en.php:

  1. 'PRIVATE' => "Private",
  2. 'LOGGED_IN' => "Logged in users",
  3. 'PUBLIC' => "Public",
  4. 'FRIENDS' => "Friends",
  5. 'access' => "Access",

Finally, the pages module needs to respect the new access level, so modify mod/pages/start.php by adding lines 252-256, and 262-269:

  1. if (($write_permission!=0) && (in_array($write_permission,$list)))
  2. return true;
  3.  
  4. if ($write_permission == -1) {
  5. $friends = get_friends($params['entity']);
  6. if (in_array($user->guid, $friends))
  7. return true;
  8. }
  9.  
  10. }
  11. }
  12. }
  13.  
  14. function get_friends($entity)
  15. {
  16. $friends = array();
  17. $entities = get_entities_from_relationship('friend', $entity->owner_guid, false, 'user', '', 0, '', 1000000);
  18. foreach ($entities as $friend)
  19. $friends[] = $friend->guid;
  20. return $friends;
  21. }
  22.  

Powered by WordPress