-
Notifications
You must be signed in to change notification settings - Fork 17
/
OauthPlugin.inc.php
228 lines (208 loc) · 6.14 KB
/
OauthPlugin.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* @file plugins/generic/oauth/OauthPlugin.inc.php
*
* Copyright (c) 2015-2016 University of Pittsburgh
* Copyright (c) 2014 Simon Fraser University Library
* Copyright (c) 2014 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class OauthPlugin
* @ingroup plugins_generic_oauth
*
* @brief This plugin adds the ability to link local user accounts to OAuth sources.
*/
import('lib.pkp.classes.plugins.GenericPlugin');
class OauthPlugin extends GenericPlugin {
/**
* Register the plugin, if enabled
* @param $category string
* @param $path string
* @return boolean
*/
function register($category, $path) {
if (parent::register($category, $path)) {
if ($this->getEnabled()) {
// Register template callback
HookRegistry::register('TemplateManager::display',array($this, 'templateCallback'));
// Register load callback
HookRegistry::register('LoadHandler', array($this, 'loadCallback'));
// This hook is used to register the components this plugin implements to
// permit administration of OAuth applications.
HookRegistry::register('LoadComponentHandler', array($this, 'setupGridHandler'));
}
return true;
}
return false;
}
/**
* Hook callback function for LoadHander
* @param $hookName string
* @param $args array
* @return boolean
*/
function loadCallback($hookName, $args) {
// Get the template manager from the hook parameters.
$page =& $args[0];
if ($this->getEnabled() && $page == 'oauth') {
$this->import('pages/OauthHandler');
define('HANDLER_CLASS', 'OauthHandler');
return true;
}
// Permit additional plugins to use this hook; returning true
// here would interrupt processing of this hook instead.
return false;
}
/**
* Permit requests to the OAuth application grid handler
* @param $hookName string The name of the hook being invoked
* @param $args array The parameters to the invoked hook
*/
function setupGridHandler($hookName, $params) {
$component =& $params[0];
if ($component == 'plugins.generic.oauth.controllers.grid.OauthAppGridHandler') {
define('OAUTH_PLUGIN_NAME', $this->getName());
return true;
}
return false;
}
/**
* Hook callback function for TemplateManager::display
* @param $hookName string
* @param $args array
* @return boolean
*/
function templateCallback($hookName, $args) {
// Get the template manager from the hook parameters.
$templateManager =& $args[0];
$template =& $args[1];
if ($this->getEnabled()) {
$request =& PKPApplication::getRequest();
switch ($template) {
case 'frontend/pages/userRegister.tpl':
case 'frontend/pages/userLogin.tpl':
$templateManager->register_outputfilter(array($this, 'javascriptFilter'));
$templateManager->register_outputfilter(array($this, 'loginFilter'));
break;
}
}
// Permit additional plugins to use this hook; returning true
// here would interrupt processing of this hook instead.
return false;
}
/**
* Output filter adds javascript to display the OAuth options.
* @param $output string
* @param $templateMgr TemplateManager
* @return $string
*/
function javascriptFilter($output, &$templateMgr) {
$matches = NULL;
if (preg_match('/<\/head>/', $output, $matches, PREG_OFFSET_CAPTURE)) {
$offset = $matches[0][1];
$newOutput = substr($output, 0, $offset);
$newOutput .= $templateMgr->fetch($this->getTemplatePath() . 'oauthJsLoader.tpl');
$newOutput .= substr($output, $offset);
$output = $newOutput;
$templateMgr->unregister_outputfilter('javascriptFilter');
}
return $output;
}
/**
* Output filter adds other oauth app interaction to login form.
* @param $output string
* @param $templateMgr TemplateManager
* @return $string
*/
function loginFilter($output, &$templateMgr) {
if (preg_match('#<form[^>]+id="login"[^>]+>.*<\/form>#s', $output, $matches, PREG_OFFSET_CAPTURE)) {
$match = $matches[0][0];
$offset = $matches[0][1];
$context = Request::getContext();
$contextId = ($context == null) ? 0 : $context->getId();
$oauthAppSettings = $this->getSetting($contextId, 'oauthAppSettings');
$templateMgr->assign(array(
'targetOp' => 'login',
'oauthAppSettings' => json_decode($oauthAppSettings, true),
));
$newOutput = substr($output, 0, $offset+strlen($match));
$newOutput .= $templateMgr->fetch($this->getTemplatePath() . 'oauthLoader.tpl');
$newOutput .= substr($output, $offset+strlen($match));
$output = $newOutput;
}
$templateMgr->unregister_outputfilter('loginFilter');
return $output;
}
/**
* Override the builtin to get the correct template path.
* @return string
*/
function getTemplatePath() {
return parent::getTemplatePath() . 'templates/';
}
/**
* Get the display name of this plugin
* @return string
*/
function getDisplayName() {
return __('plugins.generic.oauth.name');
}
/**
* Get the description of this plugin
* @return string
*/
function getDescription() {
return __('plugins.generic.oauth.description');
}
/**
* @see Plugin::getActions()
*/
function getActions($request, $actionArgs) {
$router = $request->getRouter();
import('lib.pkp.classes.linkAction.request.AjaxModal');
return array_merge(
$this->getEnabled()?array(
new LinkAction(
'settings',
new AjaxModal(
$router->url(
$request,
null,
null,
'manage',
null,
array(
'verb' => 'settings',
'plugin' => $this->getName(),
'category' => 'generic'
)
),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
):array(),
parent::getActions($request, $actionArgs)
);
}
/**
* @see Plugin::manage()
*/
function manage($args, $request) {
$request = $this->getRequest();
switch ($request->getUserVar('verb')) {
case 'settings':
$templateMgr = TemplateManager::getManager($request);
$dispatcher = $request->getDispatcher();
return $templateMgr->fetchAjax(
'oauthAppGridUrlGridContainer',
$dispatcher->url(
$request, ROUTE_COMPONENT, null,
'plugins.generic.oauth.controllers.grid.OauthAppGridHandler', 'fetchGrid'
)
);
}
}
}
?>