Commit fdcac7ba authored by Robert Lyon's avatar Robert Lyon Committed by Aaron Wells
Browse files

Bug 1393536: Tidying up of the code



To keep it consistent with rest of Mahara

1) Moving the template files to the plugin/auth dir
2) Removing the old style submenu lines
3) Removing the bad window.open() calls
4) Fixing up menu placement / order
5) Fixing the add connection form's errors so they appear against
correct fields

behatnotneeded

Change-Id: Ic73526a2db2c6f8d7009b374a8ecc0977ba57330
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent edd8e77d
......@@ -70,16 +70,17 @@ $string['isfatal'] = 'Is fatal on error';
$string['type'] = 'Web Service Type';
$string['nameexists'] = "Name already in use";
$string['emptytoken'] = 'Token must be supplied';
$string['emptyuserpass'] = 'User and Password must be supplied';
$string['emptyuser'] = 'User must be supplied';
$string['emptyuserpass'] = 'Password must be supplied';
$string['emptycert'] = 'Certificate must be supplied';
$string['header'] = 'Header Name';
$string['useheader'] = 'Put authentication in Header';
$string['invalidauthtypecombination'] = 'Invalid authentication type selected for %s';
$string['emptycertextended'] = 'When using certificate based auth you must also enter a token or username/password';
$string['emptyoauth'] = 'Consumer and Secret must be supplied for OAuth1.x';
$string['consumer'] = 'Consumer Key';
$string['emptyoauthkey'] = 'Consumer key must be supplied for OAuth1.x';
$string['emptyoauthsecret'] = 'Secret must be supplied for OAuth1.x';
$string['consumer'] = 'Consumer key';
$string['secret'] = 'Secret';
$string['chooseinstitution'] = 'Please choose the <span class="icon icon-plug"></span><span class="sr-only">connect</span> for the institution you want to create the connection for';
// core webservices strings start here
$string['control_webservices1'] = 'Use web services: ';
......
......@@ -99,36 +99,43 @@ class PluginAuthWebservice extends PluginAuth {
'url' => 'webservice/admin/index.php',
'title' => get_string('webservice', 'auth.webservice'),
'weight' => 70,
'accesskey' => 'w',
),
'webservices/config' => array(
'path' => 'webservices/config',
'url' => 'webservice/admin/index.php',
'title' => get_string('webservice', 'auth.webservice'),
'weight' => 5,
'title' => get_string('webservicesconfig', 'auth.webservice'),
'weight' => 10,
),
'webservices/oauthconfig' => array(
'path' => 'webservices/oauthconfig',
'url' => 'webservice/admin/oauthv1sregister.php',
'title' => get_string('oauth', 'auth.webservice'),
'weight' => 10,
'weight' => 40,
),
'webservices/logs' => array(
'path' => 'webservices/logs',
'url' => 'webservice/admin/webservicelogs.php',
'title' => get_string('webservicelogsnav', 'auth.webservice'),
'weight' => 20,
'weight' => 50,
),
'webservices/testclient' => array(
'path' => 'webservices/testclient',
'url' => 'webservice/testclient.php',
'title' => get_string('testclientnav', 'auth.webservice'),
'weight' => 30,
'weight' => 60,
),
'webservices/apps' => array(
'path' => 'webservices/apps',
'url' => 'webservice/apptokens.php',
'title' => get_string('apptokens', 'auth.webservice'),
'weight' => 40,
'weight' => 20,
),
'webservices/connections' => array(
'path' => 'webservices/connections',
'url' => 'webservice/admin/connections.php',
'title' => get_string('connections', 'auth.webservice'),
'weight' => 30,
),
);
......
......@@ -2068,8 +2068,16 @@ abstract class Plugin implements IPlugin {
require_once(get_config('docroot') . 'webservice/lib.php');
$userinstitutions = ($user == null ? $USER->get('institutions') : load_user_institutions($user->id));
$userinstitutions[]= 'mahara';
$userinstitutions = array();
$institutions = ($user == null ? $USER->get('institutions') : load_user_institutions($user->id));
if (!empty($institutions)) {
foreach ($institutions as $institution) {
$userinstitutions[] = $institution->institution;
}
}
else {
$userinstitutions[] = 'mahara';
}
$cdefs = self::calculate_webservice_connections($userinstitutions);
$connections = array();
......@@ -2080,7 +2088,7 @@ abstract class Plugin implements IPlugin {
if (!empty($c->token)) {
$authtype = 'token';
if ($c->useheader) {
$auth['header'] = (empty($c->header) ? 'Authorization' : $c->header.": ".$c->token);
$auth['header'] = (empty($c->header) ? 'Authorization' : $c->header . ": " . $c->token);
}
else {
if (strpos($c->token, '=')) {
......@@ -2158,7 +2166,7 @@ abstract class Plugin implements IPlugin {
require_once(get_config('docroot') . "webservice/rest/lib.php");
if ($c->authtype == WEBSERVICE_TYPE_OAUTH1) {
$client = new webservice_rest_client($c->url, $auth, 'oauth', $c->json);
$client->set_2legged($c->consumer, $c->secret);
$client->set_2legged($c->consumer, $c->secret);
}
else {
$client = new webservice_rest_client($c->url, $auth, $c->authtype, $c->json);
......
......@@ -2539,45 +2539,6 @@ function admin_nav() {
'title' => get_string('cleanurls', 'admin'),
'weight' => 40,
),
'webservices' => array(
'path' => 'webservices',
'url' => 'webservice/admin/index.php',
'title' => get_string('webservice', 'auth.webservice'),
'accessibletitle' => get_string('webservice', 'auth.webservice'),
'weight' => 70,
'accesskey' => 'w',
),
'webservices/oauthconfig' => array(
'path' => 'webservices/oauthconfig',
'url' => 'webservice/admin/oauthv1sregister.php',
'title' => get_string('oauth', 'auth.webservice'),
'weight' => 10,
),
'webservices/logs' => array(
'path' => 'webservices/logs',
'url' => 'webservice/admin/webservicelogs.php',
'title' => get_string('webservicelogs', 'auth.webservice'),
'weight' => 20,
),
'webservices/testclient' => array(
'path' => 'webservices/testclient',
'url' => 'webservice/testclient.php',
'title' => get_string('testclient', 'auth.webservice'),
'weight' => 30,
),
'webservices/apps' => array(
'path' => 'webservices/apps',
'url' => 'webservice/apptokens.php',
'title' => get_string('apptokens', 'auth.webservice'),
'weight' => 40,
),
'webservices/connections' => array(
'path' => 'webservices/connections',
'url' => 'webservice/admin/connections.php',
'title' => get_string('connections', 'auth.webservice'),
'weight' => 50,
),
);
// Add the menu items for skins, if that feature is enabled
......
......@@ -103,13 +103,12 @@
function addinstance() {
var selectedPlugin = document.getElementById('dummySelect').value;
var institution = '{{$institution}}';
window.open('{{$WWWROOT}}webservice/admin/addconnection.php?add=1&i={{$institution}}&p=' + selectedPlugin, 'addinstance', 'height=600,width=800,screenx=250,screenY=200,scrollbars=1');
window.location = '{{$WWWROOT}}webservice/admin/addconnection.php?add=1&i={{$institution}}&p=' + selectedPlugin;
return;
}
function editinstance(id, plugin) {
// if (requiresConfig(plugin)) {
window.open('addconnection.php?id='+id+'&edit=1&i={{$institution}}&p=' + plugin, 'editinstance', 'height=520,width=550,screenx=250,screenY=200,scrollbars=1');
window.location = '{{$WWWROOT}}webservice/admin/addconnection.php?id=' + id + '&edit=1&i={{$institution}}&p=' + plugin;
return;
}
......@@ -166,8 +165,7 @@ IMPORTANT: do not introduce any new whitespace into the instanceList div.
</div>
{{/foreach}}
</div>
<p/>
<div class="select">
<div class="select connections">
<span class="picker">
<select class="select form-control" name="dummy" id="dummySelect">
{{foreach $connections connection}}
......@@ -175,7 +173,9 @@ IMPORTANT: do not introduce any new whitespace into the instanceList div.
{{/foreach}}
</select>
</span>
<button class="btn btn-primary" type="button" onclick="addinstance(); return false;" name="button" value="foo">{{str tag=Add section=admin}}</button>
<div>
<button class="btn btn-primary" type="button" onclick="addinstance(); return false;" name="button" value="foo">{{str tag=Add section=admin}}</button>
</div>
</div>
<input type="hidden" id="instancePriority" name="instancePriority" value="{{$instancestring}}" />
......
......@@ -437,3 +437,7 @@
margin-left: -13px;
vertical-align: text-top;
}
.connections button {
margin-top: 10px;
}
......@@ -11,29 +11,20 @@
define('INTERNAL', 1);
define('ADMIN', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
define('TITLE', get_string('institutions', 'admin'));
define('TITLE', get_string('webservices_title', 'auth.webservice'));
define('SUBSECTIONHEADING', get_string('addconnection', 'auth.webservice'));
require_once(get_config('docroot') . '/lib/htmloutput.php');
// CHECK FOR CANCEL BEFORE THE 'REQUIRED' PARAMS:
$cancel = param_boolean('c');
if ($cancel) {
execute_javascript_and_close();
}
// NOT CANCELLING? OK - OTHER PARAMS THEN:
$institution = param_variable('i');
$connector = param_variable('p', '');
$connectionid = param_variable('id', 0);
$add = param_boolean('add', 0);
$edit = param_boolean('edit', 0);
$delete = param_boolean('delete', 0);
$json = param_boolean('j', 0);
if (!$dbinstitution = get_record('institution', 'name', $institution)) {
log_info('addconnection - institution not found: '.$institution);
execute_javascript_and_close();
throw new MaharaException('addconnection - institution not found: ' . $institution);
}
$dbconnection = get_record('client_connections_institution', 'id', $connectionid);
......@@ -68,7 +59,7 @@ else {
safe_require($type, strtolower($plugin));
$plugin_desc = get_string('name', strtolower($type).".".strtolower($plugin));
if ($delete && $json) {
if ($delete) {
try {
form_validate(param_alphanum('sesskey', null));
}
......@@ -86,46 +77,33 @@ if ($delete && $json) {
exit();
}
function allocate_client_connection_validate(Pieform $form, $values) {
global $SESSION;
$dbinstitution = get_record('institution', 'name', $values['i']);
if (empty($dbinstitution) || empty($values['p'])) {
$SESSION->add_error_msg("An unknown error occurred while processing this form");
redirect('/webservice/admin/addconnection.php?'. $_SERVER['QUERY_STRING']);
}
try {
list($type, $plugin, $connector_instance) = explode(':', $values['p']);
$classname = 'Plugin'. ucfirst(strtolower($type)) . ucfirst(strtolower($plugin));
safe_require($type, strtolower($plugin));
}
catch (Exception $e) {
log_info($e->getMessage());
log_info($e->getTrace());
$SESSION->add_error_msg("An error occurred while processing this form: " . $e->getMessage());
// $form->set_error('name', "An unknown error occurred while processing this form");
redirect('/webservice/admin/addconnection.php?'. $_SERVER['QUERY_STRING']);
$form->set_error(null, "An unknown error occurred while processing this form");
}
list($type, $plugin, $connector_instance) = explode(':', $values['p']);
$classname = 'Plugin'. ucfirst(strtolower($type)) . ucfirst(strtolower($plugin));
safe_require($type, strtolower($plugin));
// check that the name is not already used
if ($values['id'] > 0) {
if ($results = get_records_sql_assoc(
'SELECT cci.*
FROM client_connections_institution AS cci
WHERE cci.name = ? AND
cci.id <> ? AND
cci.institution = ? ', array($values['name'], $values['id'], $values['i']))) {
// $form->set_error('name', get_string('nameexists', 'auth.webservice'));
$SESSION->add_error_msg(get_string('nameexists', 'auth.webservice'));
redirect('/webservice/admin/addconnection.php?'. $_SERVER['QUERY_STRING']);
'SELECT cci.*
FROM client_connections_institution AS cci
WHERE cci.name = ? AND
cci.id <> ? AND
cci.institution = ? ', array($values['name'], $values['id'], $values['i']))) {
$form->set_error(null, get_string('nameexists', 'auth.webservice'));
}
}
else {
$clientconnection = get_record('client_connections_institution', 'name', $values['name']);
if ($clientconnection) {
$SESSION->add_error_msg(get_string('nameexists', 'auth.webservice'));
redirect('/webservice/admin/addconnection.php?'. $_SERVER['QUERY_STRING']);
$form->set_error('name', get_string('nameexists', 'auth.webservice'));
}
}
......@@ -141,12 +119,22 @@ function allocate_client_connection_validate(Pieform $form, $values) {
$form->set_error('token', get_string('emptytoken', 'auth.webservice'));
}
if ($values['authtype'] == 'oauth1' && (empty($values['consumer']) || empty($values['secret']))) {
$form->set_error('token', get_string('emptyoauth', 'auth.webservice'));
if ($values['authtype'] == 'oauth1') {
if (empty($values['consumer'])) {
$form->set_error('consumer', get_string('emptyoauthkey', 'auth.webservice'));
}
if (empty($values['secret'])) {
$form->set_error('secret', get_string('emptyoauthsecret', 'auth.webservice'));
}
}
if (($values['authtype'] == 'user' || $values['authtype'] == 'wsse') && (empty($values['username']) || empty($values['password']))) {
$form->set_error('authtype', get_string('emptyuserpass', 'auth.webservice'));
if (($values['authtype'] == 'user' || $values['authtype'] == 'wsse')) {
if (empty($values['username'])) {
$form->set_error('username', get_string('emptyuser', 'auth.webservice'));
}
if (empty($values['password'])) {
$form->set_error('password', get_string('emptyuserpass', 'auth.webservice'));
}
}
if ($values['authtype'] == 'cert' && empty($values['certificate'])) {
......@@ -160,94 +148,78 @@ function allocate_client_connection_validate(Pieform $form, $values) {
function allocate_client_connection_submit(Pieform $form, $values) {
global $SESSION;
try {
$clientconnection = new stdClass();
if ($values['id'] > 0) {
$values['create'] = false;
$clientconnection = get_record('client_connections_institution', 'id', $values['id']);
$clientconnection->id = $values['id'];
}
else {
$values['create'] = true;
$clientconnection->institution = $values['i'];
list($type, $plugin, $connector_instance) = explode(':', $values['p']);
$classname = 'Plugin'. ucfirst(strtolower($type)) . ucfirst(strtolower($plugin));
$max = get_field('client_connections_institution', 'MAX(priority)', 'institution', $clientconnection->institution);
if (empty($max)) {
$clientconnection->priority = 1;
}
else {
$clientconnection->priority = $max + 1;
}
$clientconnection->plugintype = $type;
$clientconnection->pluginname = $plugin;
$clientconnection->class = $classname;
$clientconnection->connection = $connector_instance;
}
$clientconnection = new stdClass();
$clientconnection->url = $values['url'];
$clientconnection->name = $values['name'];
$clientconnection->username = $values['username'];
$clientconnection->password = $values['password'];
$clientconnection->consumer = $values['consumer'];
$clientconnection->secret = $values['secret'];
$clientconnection->token = $values['token'];
$clientconnection->certificate = $values['certificate'];
$clientconnection->parameters = $values['parameters'];
$clientconnection->enable = (int) $values['enable'];
$clientconnection->isfatal = (int) $values['isfatal'];
$clientconnection->useheader = (int) $values['useheader'];
$clientconnection->header = $values['header'];
$clientconnection->type = $values['type'];
if ($clientconnection->type != 'rest') {
$clientconnection->json = 0;
$clientconnection->useheader = 0;
}
else {
$clientconnection->json = (int) $values['json'];
}
$clientconnection->authtype = $values['authtype'];
if ($values['authtype'] != 'token' && $values['authtype'] != 'cert') {
$clientconnection->token = '';
}
if ($values['authtype'] != 'user' && $values['authtype'] != 'wsse' && $values['authtype'] != 'cert') {
$clientconnection->username = '';
$clientconnection->password = '';
}
if ($values['authtype'] != 'cert') {
$clientconnection->certificate = '';
}
if (!$clientconnection->useheader) {
$clientconnection->header = '';
}
if ($values['authtype'] != 'oauth1') {
$clientconnection->consumer = '';
$clientconnection->secret = '';
}
if ($values['create']) {
$values['id'] = insert_record('client_connections_institution', $clientconnection, 'id', true);
if ($values['id'] > 0) {
$values['create'] = false;
$clientconnection = get_record('client_connections_institution', 'id', $values['id']);
$clientconnection->id = $values['id'];
}
else {
$values['create'] = true;
$clientconnection->institution = $values['i'];
list($type, $plugin, $connector_instance) = explode(':', $values['p']);
$classname = 'Plugin'. ucfirst(strtolower($type)) . ucfirst(strtolower($plugin));
$max = get_field('client_connections_institution', 'MAX(priority)', 'institution', $clientconnection->institution);
if (empty($max)) {
$clientconnection->priority = 1;
}
else {
update_record('client_connections_institution', $clientconnection, array('id' => $values['id']));
$clientconnection->priority = $max + 1;
}
$clientconnection->plugintype = $type;
$clientconnection->pluginname = $plugin;
$clientconnection->class = $classname;
$clientconnection->connection = $connector_instance;
}
$clientconnection->url = $values['url'];
$clientconnection->name = $values['name'];
$clientconnection->username = $values['username'];
$clientconnection->password = $values['password'];
$clientconnection->consumer = $values['consumer'];
$clientconnection->secret = $values['secret'];
$clientconnection->token = $values['token'];
$clientconnection->certificate = $values['certificate'];
$clientconnection->parameters = $values['parameters'];
$clientconnection->enable = (int) $values['enable'];
$clientconnection->isfatal = (int) $values['isfatal'];
$clientconnection->useheader = (int) $values['useheader'];
$clientconnection->header = $values['header'];
$clientconnection->type = $values['type'];
if ($clientconnection->type != 'rest') {
$clientconnection->json = 0;
$clientconnection->useheader = 0;
}
catch (Exception $e) {
log_info($e->getMessage());
log_info($e->getTrace());
$SESSION->add_error_msg("An error occurred while processing this form: " . $e->getMessage());
redirect('/webservice/admin/addconnection.php?'. $_SERVER['QUERY_STRING']);
else {
$clientconnection->json = (int) $values['json'];
}
if (array_key_exists('create', $values) && $values['create']) {
execute_javascript_and_close('window.opener.addConnection('.$values['id'].', "'.addslashes($values['name']).'");');
$clientconnection->authtype = $values['authtype'];
if ($values['authtype'] != 'token' && $values['authtype'] != 'cert') {
$clientconnection->token = '';
}
if ($values['authtype'] != 'user' && $values['authtype'] != 'wsse' && $values['authtype'] != 'cert') {
$clientconnection->username = '';
$clientconnection->password = '';
}
if ($values['authtype'] != 'cert') {
$clientconnection->certificate = '';
}
if (!$clientconnection->useheader) {
$clientconnection->header = '';
}
if ($values['authtype'] != 'oauth1') {
$clientconnection->consumer = '';
$clientconnection->secret = '';
}
if ($values['create']) {
$values['id'] = insert_record('client_connections_institution', $clientconnection, 'id', true);
}
else {
redirect('/webservice/admin/addconnection.php?c=1');
update_record('client_connections_institution', $clientconnection, array('id' => $values['id']));
}
redirect(get_config('wwwroot') . 'webservice/admin/connections.php?i=' . $values['i']);
}
$js = <<<EOF
......@@ -545,4 +517,4 @@ else {
}
$smarty->assign('form', $form);
$smarty->assign('INLINEJAVASCRIPT', $js);
$smarty->display('addconnection.tpl');
$smarty->display('auth:webservice:addconnection.tpl');
......@@ -29,15 +29,25 @@ require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once(dirname(dirname(__FILE__)) . '/lib.php');
require_once(dirname(dirname(dirname(__FILE__))) . '/lib/upgrade.php');
define('TITLE', get_string('webservices_title', 'auth.webservice'));
define('SUBSECTIONHEADING', get_string('connections', 'auth.webservice'));
$serviceenabled = get_string('webservicesenabled', 'auth.webservice');
$servicenotenabled = get_string('webservicesnotenabled', 'auth.webservice');
$institution = param_variable('i', '');
require_once('institution.php');
$institutionelement = get_institution_selector(true);
if (empty($institutionelement)) {
$smarty = smarty();
$smarty->display('admin/users/noinstitutions.tpl');
exit;
}
if (empty($institution)) {
$SESSION->add_error_msg(get_string('chooseinstitution', 'auth.webservice'), false);
redirect(get_config('wwwroot') .'admin/users/institutions.php');
$institution = 'mahara';
}
$institutionelement['defaultvalue'] = $institution;
$ids = param_variable('ids', '');
$reorder = param_boolean('reorder', 0);
$json = param_boolean('j', 0);
......@@ -219,7 +229,7 @@ function webservice_connection_classes($institution) {
$smarty->assign('institution', $institution);
$smarty->assign('instancestring', $instancestring);
$smarty->assign('sesskey', $USER->get('sesskey'));
return $smarty->fetch('connections.tpl');
return $smarty->fetch('auth:webservice:connections.tpl');
}
......@@ -269,14 +279,31 @@ function webservices_oauth_token_submit(Pieform $form, $values) {
}
redirect('/webservice/apptokens.php');
}
$js = <<< EOF
jQuery(function($) {
if ($('#institutionselect_institution').length) {
$('#institutionselect_institution').on('change', function() {
window.location.replace(config.wwwroot + 'webservice/admin/connections.php?i=' + $('#institutionselect_institution').val());
});
}
});
EOF;
$institutionselector = pieform(array(
'name' => 'institutionselect',
'class' => 'form-inline',
'elements' => array(
'institution' => $institutionelement,
)
));
// render the page
$form = webservice_client_connections($institution);
$smarty = smarty();
setpageicon($smarty, 'icon-puzzle-piece');
setpageicon($smarty, 'icon-plug');
safe_require('auth', 'webservice');
$smarty->assign('INLINEJAVASCRIPT', $js);
$smarty->assign('institutionselector', $institutionselector);
$smarty->assign('form', $form);
$smarty->assign('PAGEHEADING', TITLE);
$smarty->assign('pagedescription', get_string('webserviceconnectionsconfigdesc', 'auth.webservice'));
......
......@@ -22,7 +22,6 @@ define('TITLE', get_string('webservices_title', 'auth.webservice'));
safe_require('auth', 'webservice');
$heading = get_string('webservices_title', 'auth.webservice');
// $webservice_menu = PluginAuthWebservice::admin_menu_items();
$form = get_config_options_extended();
$serviceenabled = get_string('webservicesenabled', 'auth.webservice');
$servicenotenabled = get_string('webservicesnotenabled', 'auth.webservice');
......@@ -110,7 +109,6 @@ setpageicon($smarty, 'icon-puzzle-piece');
$smarty->assign('form', $form);
$smarty->assign('opened', param_alphanumext('open', ''));
// $smarty->assign('SUBPAGENAV', $webservice_menu);
$smarty->assign('PAGEHEADING', $heading);