Completed
Push — 3.1 ( fd3778...912fb8 )
by Jeroen
167:59 queued 60:57
created

_elgg_groups_comment_permissions_override()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 5
nop 1
dl 0
loc 24
ccs 3
cts 3
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * Elgg Groups.
4
 * Groups contain other entities, or rather act as a placeholder for other entities to
5
 * mark any given container as their container.
6
 *
7
 * @package Elgg.Core
8
 * @subpackage DataModel.Group
9
 */
10
11
/**
12
 * Checks if a group has a specific tool enabled.
13
 * Forward to the group if the tool is disabled.
14
 *
15
 * @param string $option     The group tool option to check
16
 * @param int    $group_guid The group that owns the page. If not set, this
17
 *                           will be pulled from elgg_get_page_owner_guid().
18
 *
19
 * @return void
20
 * @throws \Elgg\Http\Exception\GroupToolGatekeeperException
21
 * @since 3.0.0
22
 */
23
function elgg_group_tool_gatekeeper($option, $group_guid = null) {
24
	$group_guid = $group_guid ?: elgg_get_page_owner_guid();
25
	
26
	$group = get_entity($group_guid);
27
	if (!$group instanceof \ElggGroup) {
28
		return;
29
	}
30
	
31
	if ($group->isToolEnabled($option)) {
32
		return;
33
	}
34
35
	$ex = new \Elgg\Http\Exception\GroupToolGatekeeperException();
36
	$ex->setRedirectUrl($group->getURL());
37
	$ex->setParams([
38
		'entity' => $group,
39
		'tool' => $option,
40
	]);
41
42
	throw $ex;
43
}
44
45
/**
46
 * Allow group members to write to the group container
47
 *
48
 * @param \Elgg\Hook $hook 'container_permissions_check', 'all'
49
 *
50
 * @return bool
51
 * @internal
52
 */
53
function _elgg_groups_container_override(\Elgg\Hook $hook) {
54 11
	$container = $hook->getParam('container');
55 11
	$user = $hook->getUserParam();
56
57 11
	if ($container instanceof ElggGroup && $user) {
58 3
		if ($container->isMember($user)) {
59 3
			return true;
60
		}
61
	}
62 11
}
63
64
/**
65
 * Don't allow users to comment on content in a group they aren't a member of
66
 *
67
 * @param \Elgg\Hook $hook 'permissions_check:comment', 'object'
68
 *
69
 * @return void|false
70
 * @internal
71
 * @since 3.1
72 119
 */
73 119
function _elgg_groups_comment_permissions_override(\Elgg\Hook $hook) {
74
	
75
	if ($hook->getValue() === false) {
76
		// already not allowed, no need to check further
77
		return;
78
	}
79 107
	
80 107
	$entity = $hook->getEntityParam();
81
	$user = $hook->getUserParam();
82
	
83
	if (!$entity instanceof ElggObject || !$user instanceof ElggUser) {
84
		return;
85
	}
86
	
87
	$container = $entity->getContainerEntity();
88
	if (!$container instanceof ElggGroup) {
89
		return;
90
	}
91
	
92
	if ($container->isMember($user)) {
93
		return;
94
	}
95
	
96
	return false;
97
}
98
99
/**
100
 * init the groups library
101
 *
102
 * @return void
103
 *
104
 * @internal
105
 */
106
function _elgg_groups_init() {
107
	elgg_register_plugin_hook_handler('container_permissions_check', 'all', '_elgg_groups_container_override');
108
	elgg_register_plugin_hook_handler('permissions_check:comment', 'object', '_elgg_groups_comment_permissions_override', 999);
109
}
110
111
/**
112
 * @see \Elgg\Application::loadCore Do not do work here. Just register for events.
113
 */
114
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
115
	$events->registerHandler('init', 'system', '_elgg_groups_init');
116
};
117