-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJournalEditorRestrictionPlugin.inc.php
154 lines (124 loc) · 5.28 KB
/
JournalEditorRestrictionPlugin.inc.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
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
<?php
/**
* @file plugins/themes/default/JournalEditorRestrictionPlugin.inc.php
*
* Copyright (c) 2010-2020 openjournaltheme.com
* Copyright (c) 2010-2020 openjournaltheme team
* Read this term of use of this theme here : https://openjournaltheme.com/term-of-conditions/
*
* Modify, redistribute or make commercial copy of this part or whole of this code is prohibited without written permission from openjournaltheme.com
* Modified by openjournaltheme.com
* contact : [email protected]
*
* @class JournalEditorRestriction
*/
import('lib.pkp.classes.plugins.GenericPlugin');
class JournalEditorRestrictionPlugin extends GenericPlugin
{
/**
* @copydoc Plugin::register()
* Nama file class dan nama folder tidak boleh sama.
*/
public function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path, $mainContextId);
if ($success && $this->getEnabled($mainContextId)) {
HookRegistry::register('Dispatcher::dispatch', [$this, 'dispatcherCallback']);
HookRegistry::register('LoadHandler', [$this, 'loadHandler']);
HookRegistry::register('TemplateManager::setupBackendPage', [$this, 'setupBackendPage']);
}
return $success;
}
/**
* Hook into `LoadHandler` to Block access to interface
*/
public function loadHandler($hookName, $args)
{
if (!$this->isCurrentUserAreJournalEditorAndNotJournalManager()) return;
$request = $this->getRequest();
$router = $request->getRouter();
$requestedPage = $router->getRequestedPage($request);
$requestedOp = $router->getRequestedOp($request);
$requestedArgs = $router->getRequestedArgs($request);
// Restrict user to access some pages when the menu is removed
switch ($requestedPage) {
case 'management':
$blackListArgs = [
'context',
'website',
'workflow',
'distribution',
'access'
];
if (($requestedOp == 'settings' && !empty(array_intersect($blackListArgs, $requestedArgs))) || $requestedOp == 'tools') {
$request->redirectHome();
}
break;
}
}
/**
* Hook into `Dispatcher::dispatch` to block access to API and GridHandler
*/
public function dispatcherCallback($hookName, $request)
{
if (!$this->isCurrentUserAreJournalEditorAndNotJournalManager()) return;
$router = $request->getRouter();
// Block access to some API endpoint
if($router instanceof APIRouter){
$blockedApiEntity = [
'contexts',
'_payments',
];
if(!in_array($router->getEntity(), $blockedApiEntity)) return;
$router->handleAuthorizationFailure($request, 'api.403.unauthorized');
}
// Block access to some GridHandler
if($router instanceof PKPComponentRouter){
$rpcServiceEndpoint =& $router->getRpcServiceEndpoint($request);
// Let the system handle the request if the rpc service endpoint is not callable
if(!is_callable($rpcServiceEndpoint)) return;
[$gridHandler, $gridOp] = $rpcServiceEndpoint;
if(!$gridHandler instanceof GridHandler) return;
$blockedGridHandlerClass = [
'SettingsPluginGridHandler',
'PluginGalleryGridHandler',
'UserGridHandler',
];
if(!in_array(get_class($gridHandler), $blockedGridHandlerClass)) return;
http_response_code('403');
header('Content-Type: application/json');
echo $router->handleAuthorizationFailure($request, 'api.403.unauthorized')->getString();
exit();
}
}
public function isCurrentUserAreJournalEditorAndNotJournalManager()
{
$currentUser = $this->getRequest()->getUser();
if(!$currentUser) return false;
$userGroupDao = DAORegistry::getDAO('UserGroupDAO');
$currentUserGroups = $userGroupDao->getByUserId($currentUser->getId(), $this->getCurrentContextId());
$currentUserGroupNameLocaleKeys = collect($currentUserGroups->toArray())->map(function ($userGroup) {
return $userGroup->getData('nameLocaleKey');
})->toArray();
// Make sure the user is not a journal manager
if(in_array('default.groups.name.manager', $currentUserGroupNameLocaleKeys)) return false;
return in_array('default.groups.name.editor', $currentUserGroupNameLocaleKeys);
}
public function setupBackendPage($hookName, $args)
{
if (!$this->isCurrentUserAreJournalEditorAndNotJournalManager()) return;
$templateMgr = TemplateManager::getManager($this->getRequest());
$menu = $templateMgr->getState('menu');
unset($menu['settings']);
unset($menu['tools']);
$templateMgr->setState(['menu' => $menu]);
}
public function getDisplayName()
{
return __('plugins.generic.journalEditorRestriction.displayName');
}
public function getDescription()
{
return __('plugins.generic.journalEditorRestriction.description');
}
}