-
Notifications
You must be signed in to change notification settings - Fork 2
/
collabora_online.module
178 lines (163 loc) · 5.53 KB
/
collabora_online.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
<?php
/*
* Copyright the Collabora Online contributors.
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
use Drupal\collabora_online\Cool\CoolUtils;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\media\MediaInterface;
/**
* Implements hook_theme().
*
* Register a module or theme's theme implementations. The
* implementations declared by this hook specify how a particular
* render array is to be rendered as HTML.
*
* See:
* https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21theme.api.php/function/hook_theme
*
* If you change this method, clear theme registry and routing table
* 'drush cc theme-registry' and 'drush cc router'.
*/
function collabora_online_theme($existing, $type, $theme, $path) {
return [
'collabora_online' => [
'render element' => 'children',
'template' => 'collabora-online',
'variables' => [
'accessToken' => 'test',
'accessTokenTtl' => '86400',
'iFrameStyle' => 'width:95%;',
'closebutton' => '',
'allowfullscreen' => '',
'wopiSrc' => 'http://localhost:9980/',
'wopiClient' => 'https://localhost:9980/',
],
],
// This is the template for the field preview.
'collabora_online_preview' => [
'render element' => 'children',
'template' => 'collabora-online-preview',
'variables' => [
'editorUrl' => 'about:blank',
'fileName' => '',
],
],
// This is the template for the complete page with embedding.
'collabora_online_full' => [
'template' => 'collabora-online-full',
'variables' => [
'accessToken' => 'test',
'accessTokenTtl' => '86400',
'iFrameStyle' => '',
'closebutton' => '',
'allowfullscreen' => '',
'wopiSrc' => '/wopi/files/123',
'wopiClient' => 'https://localhost:9980/',
],
'file' => 'collabora_online.theme.inc',
],
];
}
/**
* Implements hook_entity_operation().
*
* This is used to add the menu entry in the Media content to
* open the viewer/editor directly.
*/
function collabora_online_entity_operation(EntityInterface $entity) {
if (($entity->getEntityTypeId() != "media") ||
($entity->getSource()->getPluginId() != "file")) {
return [];
}
/** @var \Drupal\media\MediaInterface $media */
$media = $entity;
if (!$media->access('preview in collabora')) {
return [];
}
$file = CoolUtils::getFile($media);
$type = CoolUtils::getDocumentType($file);
if ($type == NULL) {
return [];
}
$entries = [
'collabora_online_view' => [
'title' => t("View in Collabora Online"),
'weight' => 50,
'url' => CoolUtils::getEditorUrl($media, FALSE),
],
];
if (CoolUtils::canEdit($file) && $media->access('edit in collabora')) {
$entries['collabora_online_edit'] = [
'title' => t("Edit in Collabora Online"),
'weight' => 50,
'url' => CoolUtils::getEditorUrl($media, TRUE),
];
}
return $entries;
}
/**
* Implements hook_ENTITY_TYPE_access() for 'media'.
*
* Checks access for the new media operations provided by this module.
*/
function collabora_online_media_access(MediaInterface $media, string $operation, AccountInterface $account): AccessResultInterface {
$type = $media->bundle();
switch ($operation) {
case 'preview in collabora':
if ($media->isPublished()) {
return AccessResult::allowedIfHasPermission($account, "preview $type in collabora")
->addCacheableDependency($media);
}
$preview_own_permission = "preview own unpublished $type in collabora";
$access_result = AccessResult::allowedIfHasPermission($account, $preview_own_permission)
->addCacheableDependency($media);
if (!$access_result->isAllowed()) {
return $access_result;
}
// Use '==' because Drupal sometimes loads integers as strings.
$is_owner = ($account->id() && $account->id() == $media->getOwnerId());
if ($is_owner) {
$access_result = AccessResult::allowed();
}
else {
$access_result = AccessResult::neutral()
->setReason("The user has the '$preview_own_permission' permission, but is not the owner of the media item.");
}
return $access_result
->cachePerUser()
->addCacheableDependency($media);
case 'edit in collabora':
if ($account->hasPermission("edit any $type in collabora")) {
return AccessResult::allowed()
->cachePerPermissions();
}
$edit_own_permission = "edit own $type in collabora";
$access_result = AccessResult::allowedIfHasPermission($account, $edit_own_permission);
if (!$access_result->isAllowed()) {
return $access_result;
}
// Use '==' because Drupal sometimes loads integers as strings.
$is_owner = ($account->id() && $account->id() == $media->getOwnerId());
if (!$is_owner) {
$access_result = AccessResult::neutral()
->setReason("The user has the '$edit_own_permission' permission, but is not the owner of the media item.");
}
else {
$access_result = AccessResult::allowed();
}
return $access_result
->cachePerUser()
->addCacheableDependency($media);
default:
return AccessResult::neutral();
}
}