-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_view_access.module
More file actions
executable file
·117 lines (98 loc) · 2.88 KB
/
node_view_access.module
File metadata and controls
executable file
·117 lines (98 loc) · 2.88 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* @file
* Main functions and hook implementations of the node_view_access module.
*
* Enables permissions "View own content" and "View any content" for each
* content type on permissions page.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Drupal\node\Entity\NodeType;
/**
* Node view access default gid.
*/
const NODE_VIEW_ACCESS_GID = 23;
/**
* Implements hook_node_access().
*/
function node_view_access_node_access(NodeInterface $node, $op, AccountInterface $account) {
if ($op == 'view') {
$is_owner = $account->id() === $node->getOwnerId();
if (!$node->isPublished()) {
return AccessResult::allowedIf($account->hasPermission('view own unpublished content') && $account->isAuthenticated() && $is_owner)
->cachePerPermissions()
->cachePerUser()
->addCacheableDependency($node);
}
$type = $node->bundle();
if ($account->hasPermission('view ' . $type . ' content')) {
return AccessResult::allowed()->cachePerPermissions();
}
else {
return AccessResult::allowedIf($account->hasPermission('view own ' . $type . ' content') && $is_owner)
->cachePerPermissions()
->cachePerUser()
->addCacheableDependency($node);
}
}
// No opinion.
return AccessResult::neutral();
}
/**
* Implements hook_node_grants().
*/
function node_view_access_node_grants(AccountInterface $account, $op) {
$grants = array();
if ($op == 'view') {
if ($account->hasPermission('view own unpublished content')) {
$grants['node_view_access_author_unpublished'] = array($account->id());
}
foreach (NodeType::loadMultiple() as $key => $type) {
if ($account->hasPermission("view $key content")) {
$grants["node_view_access_view_$key"] = array(NODE_VIEW_ACCESS_GID);
}
if ($account->hasPermission("view own $key content")) {
$grants['node_view_access_author'] = array($account->id());
}
}
}
return $grants;
}
/**
* Implements hook_node_access_records().
*/
function node_view_access_node_access_records(NodeInterface $node) {
$grants = array();
if ($node->isPublished()) {
$type = $node->getType();
$grants[] = array(
'realm' => 'node_view_access_view_' . $type,
'gid' => NODE_VIEW_ACCESS_GID,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
);
$grants[] = array(
'realm' => 'node_view_access_author',
'gid' => $node->getOwnerId(),
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
);
}
else {
$grants[] = array(
'realm' => 'node_view_access_author_unpublished',
'gid' => $node->getOwnerId(),
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
);
}
return $grants;
}