-
Notifications
You must be signed in to change notification settings - Fork 11
/
embargoes.module
188 lines (181 loc) · 8.62 KB
/
embargoes.module
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
use \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* @file
* Contains embargoes.module.
*/
/**
* Implements hook_node_access().
*/
function embargoes_node_access(\Drupal\node\NodeInterface $node, $operation, \Drupal\Core\Session\AccountInterface $account) {
$nids = array($node->id());
$ip = \Drupal::request()->getClientIp();
$embargoes = \Drupal::service('embargoes.embargoes')->getActiveNodeEmbargoesByNids($nids, $ip, $account);
$ip_allowed_embargoes = \Drupal::service('embargoes.embargoes')->getIpAllowedEmbargoes($embargoes);
if (!empty($embargoes) && empty($ip_allowed_embargoes)) {
$access = \Drupal\Core\Access\AccessResult::forbidden();
}
else {
$access = \Drupal\Core\Access\AccessResult::neutral();
}
return $access;
}
/**
* Implements hook_ENTITY_TYPE_access().
*/
function embargoes_media_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account){
$nids = \Drupal::service('embargoes.embargoes')->getMediaParentNids($entity->id());
$ip = \Drupal::request()->getClientIp();
$active_embargoes = \Drupal::service('embargoes.embargoes')->getActiveEmbargoesByNids($nids, $ip, $account);
$ip_allowed_embargoes = \Drupal::service('embargoes.embargoes')->getIpAllowedEmbargoes($active_embargoes);
if (!empty($active_embargoes) && empty($ip_allowed_embargoes)) {
$access = \Drupal\Core\Access\AccessResult::forbidden();
}
else {
$access = \Drupal\Core\Access\AccessResult::neutral();
}
return $access;
}
/**
* Implements hook_ENTITY_TYPE_access().
*/
function embargoes_file_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account){
$nids = \Drupal::service('embargoes.embargoes')->getParentNidsOfFileEntity($entity);
$ip = \Drupal::request()->getClientIp();
$active_embargoes = \Drupal::service('embargoes.embargoes')->getActiveEmbargoesByNids($nids, $ip, $account);
$ip_allowed_embargoes = \Drupal::service('embargoes.embargoes')->getIpAllowedEmbargoes($active_embargoes);
if (!empty($active_embargoes) && empty($ip_allowed_embargoes)) {
$access = \Drupal\Core\Access\AccessResult::forbidden();
}
else {
$access = \Drupal\Core\Access\AccessResult::neutral();
}
return $access;
}
/**
* Implements hook_node_view().
*/
function embargoes_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
$ip = \Drupal::request()->getClientIp();
$user = \Drupal::currentUser();
$path = \Drupal::request()->getRequestUri();
$active_embargoes = \Drupal::service('embargoes.embargoes')->getActiveEmbargoesByNids(array($entity->id()), $ip, $user);
$active_node_embargoes = \Drupal::service('embargoes.embargoes')->getActiveNodeEmbargoesByNids(array($entity->id()), $ip, $user);
$ip_allowed_embargoes = \Drupal::service('embargoes.embargoes')->getIpAllowedEmbargoes($active_node_embargoes);
if (!empty($active_node_embargoes)) {
$build['#attached']['library'][] = 'embargoes/embargoes-file-embargoes';
}
if (!empty($active_node_embargoes) && !empty($ip_allowed_embargoes)) {
$imploded_ranges = implode('.', array_unique($ip_allowed_embargoes));
$response = new RedirectResponse("/embargoes/ip-access-denied?path={$path}&ranges={$imploded_ranges}");
$response->send();
}
else if ((\Drupal::config('embargoes.settings')->get('show_embargo_message') == FALSE || is_null(\Drupal::config('embargoes.settings')->get('show_embargo_message'))) && $view_mode != 'teaser') {;
$embargoes = \Drupal::service('embargoes.embargoes')->getCurrentEmbargoesByNids(array($entity->id()));
if (!empty($embargoes)) {
$build['#cache']['max-age'] = 0;
$embargo_count = count($embargoes);
$embargo_word = ($embargo_count > 1 ? "embargoes" : "embargo" );
drupal_set_message("This resource is under {$embargo_count} {$embargo_word}:", 'warning');
foreach ($embargoes as $embargo_id) {
$embargo = \Drupal::entityTypeManager()->getStorage('embargoes_embargo_entity')->load($embargo_id);
$embargo_message = "- Access to ";
if ($embargo->getEmbargoTypeAsInt() == 0) {
$embargo_message .= " all associated files of this resource";
}
else {
$embargo_message .= " this resource and all associated files";
}
$embargo_message .= " is restricted";
if ($embargo->getExemptIps() != 'none') {
$ip_range = \Drupal::entityTypeManager()->getStorage('embargoes_ip_range_entity')->load($embargo->getExemptIps());
$embargo_message .= " to the {$ip_range->label()} network";
}
else {
}
if ($embargo->getExpirationTypeAsInt() == 0) {
$embargo_message .= " indefinitely.";
}
else {
$embargo_message .= " until {$embargo->getExpirationDate()}.";
}
drupal_set_message($embargo_message, 'warning');
}
}
}
}
/**
* Implements hook_ENTITY_TYPE_view().
*/
function embargoes_media_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
$ip = \Drupal::request()->getClientIp();
$user = \Drupal::currentUser();
$path = \Drupal::request()->getRequestUri();
$nids = \Drupal::service('embargoes.embargoes')->getMediaParentNids($entity->id());
$active_embargoes = \Drupal::service('embargoes.embargoes')->getActiveEmbargoesByNids($nids, $ip, $user);
$active_node_embargoes = \Drupal::service('embargoes.embargoes')->getActiveNodeEmbargoesByNids(array($entity->id()), $ip, $user);
$ip_allowed_embargoes = \Drupal::service('embargoes.embargoes')->getIpAllowedEmbargoes($active_embargoes);
if (!empty($active_embargoes)) {
$build['#attached']['library'][] = 'embargoes/embargoes-file-embargoes';
}
if (!empty($active_node_embargoes) && !empty($ip_allowed_embargoes)) {
$imploded_ranges = implode('.', array_unique($ip_allowed_embargoes));
$response = new RedirectResponse("/embargoes/ip-access-denied?path={$path}&ranges={$imploded_ranges}");
$response->send();
}
else if ((\Drupal::config('embargoes.settings')->get('show_embargo_message') == FALSE) || is_null(\Drupal::config('embargoes.settings')->get('show_embargo_message')) && $view_mode != 'teaser') {;
$embargoes = \Drupal::service('embargoes.embargoes')->getCurrentEmbargoesByNids(array($entity->id()));
if (!empty($embargoes)) {
$build['#cache']['max-age'] = 0;
$embargo_count = count($embargoes);
$embargo_word = ($embargo_count > 1 ? "embargoes" : "embargo" );
drupal_set_message("This resource is under {$embargo_count} {$embargo_word}:", 'warning');
foreach ($embargoes as $embargo_id) {
$embargo = \Drupal::entityTypeManager()->getStorage('embargoes_embargo_entity')->load($embargo_id);
$embargo_message = "- Access to ";
if ($embargo->getEmbargoTypeAsInt() == 0) {
$embargo_message .= " all associated files of this resource";
}
else {
$embargo_message .= " this resource and all associated files";
}
$embargo_message .= " is restricted";
if ($embargo->getExemptIps() != 'none') {
$ip_range = \Drupal::entityTypeManager()->getStorage('embargoes_ip_range_entity')->load($embargo->getExemptIps());
$embargo_message .= " to the {$ip_range->label()} network";
}
else {
}
if ($embargo->getExpirationTypeAsInt() == 0) {
$embargo_message .= " indefinitely.";
}
else {
$embargo_message .= " until {$embargo->getExpirationDate()}.";
}
drupal_set_message($embargo_message, 'warning');
}
}
}
}
/**
* Implements hook_file_download().
*/
function embargoes_file_download($uri) {
$files = \Drupal::entityTypeManager()
->getStorage('file')
->loadByProperties(['uri' => $uri]);
$file = array_values($files)[0];
$nids = \Drupal::service('embargoes.embargoes')->getParentNidsOfFileEntity($file);
$ip = \Drupal::request()->getClientIp();
$user = \Drupal::currentUser();
$path = \Drupal::request()->getRequestUri();
$embargoes = \Drupal::service('embargoes.embargoes')->getActiveEmbargoesByNids($nids, $ip, $user);
$ip_allowed_embargoes = \Drupal::service('embargoes.embargoes')->getIpAllowedEmbargoes($embargoes);
if (!empty($embargoes) && !empty($ip_allowed_embargoes)) {
$imploded_ranges = implode('.', array_unique($ip_allowed_embargoes));
$response = new RedirectResponse("/embargoes/ip-access-denied?path={$path}&ranges={$imploded_ranges}");
$response->send();
}
return NULL;
}