Last active
July 28, 2016 10:47
-
-
Save hypeJunction/e6cc1f5efc00ca06671acba0f142d942 to your computer and use it in GitHub Desktop.
Add Friends Of ACL to Elgg access levels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Define a new access level to allow owners to save content visible to those who added them as a friend, | |
* as oppose to Friends access level, which shows content to users that have been added as friends by the owner | |
* | |
* Note that Elgg friendships are not reciprocal by default | |
* | |
* @author Ismayil Khayredinov <[email protected]> | |
* @copyright 2016 (c) Ismayil Khayredinov | |
* @license GNU GPL 2.0 | |
*/ | |
define('ACCESS_FRIENDS_OF', -2); // make sure this is not used by other plugins | |
elgg_register_event_handler('init', 'system', function() { | |
elgg_register_plugin_hook_handler('get_sql', 'access', 'friends_of_acl_get_access_sql_clauses'); | |
elgg_register_plugin_hook_handler('access:collections:write', 'user', 'friends_of_acl_access_collections_write'); | |
}); | |
/** | |
* Populates access WHERE sql clauses | |
* | |
* @param string $hook "get_sql" | |
* @param string $type "access" | |
* @param array $return Clauses | |
* @param array $params Hook params | |
* @return array | |
*/ | |
function friends_of_acl_get_access_sql_clauses($hook, $type, $return, $params) { | |
$ignore_access = elgg_extract('ignore_access', $params); | |
if ($ignore_access) { | |
return; | |
} | |
$user_guid = elgg_extract('user_guid', $params); | |
if (!$user_guid) { | |
return; | |
} | |
$prefix = elgg_get_config('dbprefix'); | |
$table_alias = $params['table_alias'] ? $params['table_alias'] . '.' : ''; | |
$access_column = $params['access_column']; | |
$owner_guid_column = $params['owner_guid_column']; | |
$id = ACCESS_FRIENDS_OF; | |
$return['ors'][] = "{$table_alias}{$access_column} = {$id} | |
AND {$table_alias}{$owner_guid_column} IN ( | |
SELECT guid_two FROM {$prefix}entity_relationships | |
WHERE relationship='friend' AND guid_one = {$user_guid} | |
)"; | |
return $return; | |
} | |
/** | |
* Populates write access array with relationship ACLs | |
* | |
* @param string $hook "access:collections:write" | |
* @param string $type "user" | |
* @param array $return ACL IDs | |
* @param array $params Hook params | |
* @return array | |
*/ | |
function friends_of_acl_access_collections_write($hook, $type, $return, $params) { | |
$page_owner = elgg_get_page_owner_entity(); | |
if ($page_owner instanceof ElggGroup) { | |
return; | |
} | |
$return[ACCESS_FRIENDS_OF] = elgg_echo('access:friends_of'); | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment