-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmembership_entity.api.php
78 lines (70 loc) · 2.38 KB
/
membership_entity.api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* @file
* Membership Entity API Documentation.
*
* This file contains no working PHP code; it exists to provide additional
* documentation for doxygen as well as to document hooks in the standard
* Drupal manner.
*/
/**
* Control access to memberships.
*
* @param string $op
* The membership operation.
* @param optional|MembershipEntity $membership
* The membership object. NULL if $op is 'join'.
* @param object $account
* The loaded user account object. Defaults to logged in user.
*
* @return bool
* TRUE if $account as permission for $op.
*/
function hook_membership_entity_access($op, $membership = NULL, $account = NULL) {
$user = isset($account) ? $account : $GLOBALS['user'];
// Join page access.
if ($op == 'join') {
$type = str_replace('-', '_', $membership);
// Check if a membership already exists for this user.
$exists = (bool) db_query('SELECT mid FROM {membership_entity} WHERE uid = :uid AND uid <> 0 AND type = :type', array(':uid' => $user->uid, ':type' => $type))->rowCount();
if (!$exists && user_access("$type join", $user)) {
return TRUE;
}
return FALSE;
}
if (isset($membership) && $type_name = $membership->type) {
if (user_access("$type_name $op any membership", $user)) {
return TRUE;
}
if (user_access("$type_name $op own membership", $user) && $membership->uid == $user->uid) {
return TRUE;
}
// Secondary member access.
$results = db_query('SELECT mid FROM {membership_entity_secondary_member} WHERE uid = :uid', array(':uid' => $user->uid))->fetchAllAssoc('mid');
if (!empty($results) && in_array($membership->mid, array_keys($results))) {
// Can secondary members edit the membership?
$bundle_settings = membership_entity_get_bundle_settings($type_name);
if ($op == 'edit' && $bundle_settings['all_edit'] && user_access("$type edit own membership", $user)) {
return TRUE;
}
// Can secondary members view the membership?
if ($op == 'view' && user_access("$type view own membership", $user)) {
return TRUE;
}
}
}
}
/**
* Define membership operations.
*
* @return array
* An array of operations with a label and and callback.
*/
function hook_membership_entity_operations() {
return array(
'delete' => array(
'label' => t('Delete selected memberships'),
'callback' => NULL,
),
);
}