Commit 15334434 authored by Aaron Wells's avatar Aaron Wells

Bug 1620879: Adding mobileapi module

This module will hold most of the code specifically needed
by the Mahara Mobile app (as opposed to normal webservices
functionality, which is mostly meant to be server-to-server)

behatnotneeded: Test to come later

Change-Id: I8dca163ba9dd4c1777564e305cf49768358be088
parent 93a432c2
<?php
/**
*
* @package mahara
* @subpackage module-mobileapi
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
defined('INTERNAL') || die();
$string['autoconfiguredesc'] = 'Automatically enable settings and configurations needed for the mobile apps API.';
$string['autoconfiguretitle'] = 'Auto-configure mobile apps API?';
$string['configstep'] = 'Conguration item';
$string['configstepstatus'] = 'Status';
$string['manualtokensdesc'] = 'Users can manually generate webservice access tokens, in order to copy and paste them into an app. (Normally an app should be able to generate the tokens for users automatically, but some auth plugins may not make that possible.)';
$string['manualtokenstitle'] = 'Manual token generation';
$string['mobileapiserviceexists'] = 'Mobile API service group is registered';
$string['mobileapiserviceconfigured'] = 'Mobile API service enabled, "%s" disabled, "%s" enabled';
$string['noticeenabled'] = 'The Mahara mobile apps API is currently enabled.';
$string['noticenotenabled'] = 'The Mahara mobile apps API is <b>not</b> currently enabled.';
$string['notreadylabel'] = 'Not ready';
$string['readylabel'] = 'Ready';
$string['restprotocolenabled'] = 'REST protocol enabled';
$string['webserviceproviderenabled'] = 'Incoming web service requests allowed';
\ No newline at end of file
<?php
/**
*
* @package mahara
* @subpackage module.mobileapi
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
defined('INTERNAL') || die();
/**
* This plugin is mostly a placeholder for the servicegroup and functions
* required by the Mahara Mobile app.
*/
class PluginModuleMobileapi extends PluginModule {
public static function postinst($fromversion) {
require_once(get_config('docroot') . 'webservice/lib.php');
external_reload_component('module/mobileapi', false);
}
public static function has_config() {
return true;
}
/**
* Check the status of each configuration element needed for the Mobile API
* webservice to run.
*
* @param boolean $clearcache Whether to clear the cached results of the check
* @return array Information about the status of each config step needed.
*/
public static function check_service_status($clearcache = false) {
static $statuslist = null;
if (!$clearcache && $statuslist !== null) {
return $statuslist;
}
require_once(get_config('docroot') . 'webservice/lib.php');
// Check all the configs needed for the mobileapi to work.
$statuslist = array();
$statuslist[] = array(
'name' => get_string('webserviceproviderenabled', 'module.mobileapi'),
'status' => (bool) get_config('webservice_provider_enabled')
);
$statuslist[] = array(
'name' => get_string('restprotocolenabled', 'module.mobileapi'),
'status' => webservice_protocol_is_enabled('rest')
);
$servicerec = get_record('external_services', 'shortname', 'maharamobile', 'component', 'module/mobileapi', null, null, 'enabled, restrictedusers, tokenusers');
$statuslist[] = array(
'name' => get_string('mobileapiserviceexists', 'module.mobileapi'),
'status' => (bool) $servicerec
);
$statuslist[] = array(
'name' => get_string('mobileapiserviceconfigured', 'module.mobileapi', get_string('restrictedusers', 'auth.webservice'), get_string('fortokenusers', 'auth.webservice')),
'status' => ($servicerec && $servicerec->enabled && !$servicerec->restrictedusers && $servicerec->tokenusers),
);
return $statuslist;
}
/**
* Determine whether the mobileapi webservice, as a whole, is fully configured
* @param boolean $clearcache Whether to clear cached results from a previous check
* @return boolean
*/
public static function is_service_ready($clearcache = false) {
return array_reduce(
static::check_service_status($clearcache),
function($carry, $item) {
return $carry && $item['status'];
},
true
);
}
public static function get_config_options() {
$statuslist = static::check_service_status(true);
$ready = static::is_service_ready();
$smarty = smarty_core();
$smarty->assign('statuslist', $statuslist);
if ($ready) {
$smarty->assign('notice', get_string('noticeenabled', 'module.mobileapi'));
}
else {
$smarty->assign('notice', get_string('noticenotenabled', 'module.mobileapi'));
}
$statushtml = $smarty->fetch('module:mobileapi:statustable.tpl');
unset($smarty);
$elements = array();
$elements['statustable'] = array(
'type' => 'html',
'value' => $statushtml
);
if (!$ready) {
$elements['activate'] = array(
'type' => 'switchbox',
'title' => get_string('autoconfiguretitle', 'module.mobileapi'),
'description' => get_string('autoconfiguredesc', 'module.mobileapi'),
'switchtext' => 'yesno',
);
}
$elements['manualtokens'] = array(
'type' => 'switchbox',
'title' => get_string('manualtokenstitle', 'module.mobileapi'),
'description' => get_string('manualtokensdesc', 'module.mobileapi'),
'defaultvalue' => (bool) get_config_plugin('module', 'mobileapi', 'manualtokens')
);
$form = array('elements' => $elements);
if (!$ready) {
// HACK: Reload the page after form submission, so that the status
// table gets updated.
$form['jssuccesscallback'] = 'module_mobileapi_reload_page';
}
return $form;
}
public static function save_config_options(Pieform $form, $values) {
set_config_plugin('module', 'mobileapi', 'manualtokens', $values['manualtokens']);
if (!empty($values['activate'])) {
set_config('webservice_provider_enabled', true);
set_config('webservice_provider_rest_enabled', true);
require_once(get_config('docroot') . 'webservice/lib.php');
external_reload_component('module/mobileapi', false);
set_field('external_services', 'enabled', 1, 'shortname', 'maharamobile', 'component', 'module/mobileapi');
set_field('external_services', 'restrictedusers', 0, 'shortname', 'maharamobile', 'component', 'module/mobileapi');
set_field('external_services', 'tokenusers', 1, 'shortname', 'maharamobile', 'component', 'module/mobileapi');
}
return true;
}
}
<?php
/**
*
* @package mahara
* @subpackage module-framework
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
defined('INTERNAL') || die();
$config = new stdClass();
$config->version = 2016092104;
$config->release = '1.0.0';
<?php
/**
* Core external functions and service definitions.
*
* @package mahara
* @subpackage module-mobilapi
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*/
$services = array(
'Mahara Mobile API' => array(
'shortname' => 'maharamobile',
'functions' => [
],
'enabled' => 1,
'restrictedusers' => 0,
'tokenusers' => 1,
// Increment this whenever you make a change to the profile or
// behavior of this service and its exposed functions.
'apiversion' => 1,
),
);
<div class="alert alert-default">
{if $header}<h3>{$header}</h3>{/if}
<p>{$notice|safe}</p>
</div>
<table class="table fullwidth table-padded">
<tbody>
<thead>
<tr>
<th>{str tag="configstep" section="module.mobileapi"}</th>
<th>{str tag="configstepstatus" section="module.mobileapi"}</th>
</tr>
</thead>
{foreach from=$statuslist item=item}
<tr>
<td>
<h3 class="title">
{$item.name}
</h3>
</td>
<td>
{if $item.status}
<span class="icon icon-check text-success" title="{str tag="readylabel" section="module.mobileapi"}" role="presentation" aria-hidden="true"></span>
{else}
<span class="icon icon-exclamation-triangle" title="{str tag="notreadylabel" section="module.mobileapi"}" role="presentation" aria-hidden="true"></span>
{/if}
</td>
</tr>
{/foreach}
</tbody>
</table>
<script type="text/javascript">
if (typeof module_mobileapi_reload_page === "undefined") {
function module_mobileapi_reload_page() {
window.location.reload(true);
}
}
</script>
\ No newline at end of file
......@@ -1935,9 +1935,14 @@ function external_delete_descriptions($component) {
delete_records_select('external_services_users', "externalserviceid IN (SELECT id FROM {external_services} WHERE component = ?)", $params);
delete_records_select('external_tokens', "externalserviceid IN (SELECT id FROM {external_services} WHERE component = ?)", $params);
delete_records_select('external_services_functions', "externalserviceid IN (SELECT id FROM {external_services} WHERE component = ?)", $params);
delete_records_select('oauth_server_token', "osr_id_ref IN (SELECT id FROM {oauth_server_registry} WHERE externalserviceid IN (SELECT id FROM {external_services} WHERE component = ?))", $params);
delete_records_select('oauth_server_registry', "externalserviceid IN (SELECT id FROM {external_services} WHERE component = ?)", $params);
delete_records_select(
'external_services_functions',
"externalserviceid IN (SELECT id FROM {external_services} WHERE component = ?)"
. " OR functionname IN (SELECT name FROM {external_functions} WHERE component = ?)",
array($component, $component)
);
delete_records('external_services', 'component', $component);
delete_records('external_functions', 'component', $component);
}
......@@ -2014,8 +2019,8 @@ function webservice_load_services_file($component) {
else {
// Not a plugin, must handle manually
$filepath = get_config('docroot') . $wsdir . '/services.php';
if (file_exists($filepath . '/services.php')) {
include($filepath . '/services.php');
if (file_exists($filepath)) {
include($filepath);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment