Skip to content

Commit

Permalink
MDL-35998 New mobile menu: with CSS url + existing enable mobile web …
Browse files Browse the repository at this point in the history
…services
  • Loading branch information
mouneyrac committed Oct 31, 2012
1 parent 7e8ae12 commit 33af788
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 17 deletions.
3 changes: 0 additions & 3 deletions admin/settings/plugins.php
Expand Up @@ -253,9 +253,6 @@
$ADMIN->add('webservicesettings', new admin_externalpage('webservicedocumentation', new lang_string('wsdocapi', 'webservice'), "$CFG->wwwroot/$CFG->admin/webservice/documentation.php", 'moodle/site:config', false));
/// manage service
$temp = new admin_settingpage('externalservices', new lang_string('externalservices', 'webservice'));
$enablemobiledocurl = new moodle_url(get_docs_url('Enable_mobile_web_services'));
$enablemobiledoclink = html_writer::link($enablemobiledocurl, new lang_string('documentation'));
$temp->add(new admin_setting_enablemobileservice('enablemobilewebservice', new lang_string('enablemobilewebservice', 'admin'), new lang_string('configenablemobilewebservice', 'admin', $enablemobiledoclink), 0));
$temp->add(new admin_setting_heading('manageserviceshelpexplaination', new lang_string('information', 'webservice'), new lang_string('servicehelpexplanation', 'webservice')));
$temp->add(new admin_setting_manageexternalservices());
$ADMIN->add('webservicesettings', $temp);
Expand Down
9 changes: 9 additions & 0 deletions admin/settings/server.php
Expand Up @@ -4,6 +4,15 @@

if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page

// Mobile
$mobile = new admin_settingpage('mobile', new lang_string('mobile','admin'), 'moodle/site:config', false);
$mobile->add(new admin_setting_configtext('mobilecssurl', new lang_string('mobilecssurl', 'admin'), new lang_string('configmobilecssurl','admin'), '', PARAM_URL));
$enablemobiledocurl = new moodle_url(get_docs_url('Enable_mobile_web_services'));
$enablemobiledoclink = html_writer::link($enablemobiledocurl, new lang_string('documentation'));
$mobile->add(new admin_setting_enablemobileservice('enablemobilewebservice',
new lang_string('enablemobilewebservice', 'admin'),
new lang_string('configenablemobilewebservice', 'admin', $enablemobiledoclink), 0));
$ADMIN->add('server', $mobile);

// "systempaths" settingpage
$temp = new admin_settingpage('systempaths', new lang_string('systempaths','admin'));
Expand Down
3 changes: 3 additions & 0 deletions lang/en/admin.php
Expand Up @@ -255,6 +255,7 @@
$string['configminpasswordlower'] = 'Passwords must have at least these many lower case letters.';
$string['configminpasswordnonalphanum'] = 'Passwords must have at least these many non-alphanumeric characters.';
$string['configminpasswordupper'] = 'Passwords must have at least these many upper case letters.';
$string['configmobilecssurl'] = 'Custom CSS file to customize your mobile app interface.';
$string['configmodchooserdefault'] = 'Should the activity chooser be presented to users by default?';
$string['configmycoursesperpage'] = 'Maximum number of courses to display in any list of a user\'s own courses';
$string['configmymoodleredirect'] = 'This setting forces redirects to /my on login for non-admins and replaces the top level site navigation with /my';
Expand Down Expand Up @@ -686,6 +687,8 @@
$string['mnetrestore_extusers_mismatch'] = '<strong>Note:</strong> This backup file apparently originates from a different Moodle installation and contains remote Moodle Network user accounts that may fail to restore. This operation is unsupported. If you are certain that it was created on this Moodle installation, or you can ensure that all the needed Moodle Network Hosts are configured, you may want to still try the restore.';
$string['mnetrestore_extusers_noadmin'] = '<strong>Note:</strong> This backup file seems to come from a different Moodle installation and contains remote Moodle Network user accounts. You are not allowed to execute this type of restore. Contact the administrator of the site or, alternatively, restore this course without any user information (modules, files...)';
$string['mnetrestore_extusers_switchuserauth'] = 'Remote Moodle Network user {$a->username} (coming from {$a->mnethosturl}) switched to local {$a->auth} authenticated user.';
$string['mobile'] = 'Mobile';
$string['mobilecssurl'] = 'CSS';
$string['modchooserdefault'] = 'Activity chooser default';
$string['modeditdefaults'] = 'Default values for activity settings';
$string['modsettings'] = 'Manage activities';
Expand Down
59 changes: 47 additions & 12 deletions lib/adminlib.php
Expand Up @@ -6749,16 +6749,21 @@ public function output_html($data, $query='') {
*/
class admin_setting_enablemobileservice extends admin_setting_configcheckbox {

private $xmlrpcuse; //boolean: true => capability 'webservice/xmlrpc:use' is set for authenticated user role
/** @var boolean True means that the capability 'webservice/xmlrpc:use' is set for authenticated user role */
private $xmlrpcuse;
/** @var boolean True means that the capability 'webservice/rest:use' is set for authenticated user role */
private $restuse;

/**
* Return true if Authenticated user role has the capability 'webservice/xmlrpc:use', otherwise false
* Return true if Authenticated user role has the capability 'webservice/xmlrpc:use' and 'webservice/rest:use', otherwise false.
*
* @return boolean
*/
private function is_xmlrpc_cap_allowed() {
private function is_protocol_cap_allowed() {
global $DB, $CFG;

//if the $this->xmlrpcuse variable is not set, it needs to be set
// We keep xmlrpc enabled for backward compatibility.
// If the $this->xmlrpcuse variable is not set, it needs to be set.
if (empty($this->xmlrpcuse) and $this->xmlrpcuse!==false) {
$params = array();
$params['permission'] = CAP_ALLOW;
Expand All @@ -6767,27 +6772,37 @@ private function is_xmlrpc_cap_allowed() {
$this->xmlrpcuse = $DB->record_exists('role_capabilities', $params);
}

return $this->xmlrpcuse;
// If the $this->restuse variable is not set, it needs to be set.
if (empty($this->restuse) and $this->restuse!==false) {
$params = array();
$params['permission'] = CAP_ALLOW;
$params['roleid'] = $CFG->defaultuserroleid;
$params['capability'] = 'webservice/rest:use';
$this->restuse = $DB->record_exists('role_capabilities', $params);
}

return ($this->xmlrpcuse && $this->restuse);
}

/**
* Set the 'webservice/xmlrpc:use' to the Authenticated user role (allow or not)
* Set the 'webservice/xmlrpc:use'/'webservice/rest:use' to the Authenticated user role (allow or not)
* @param type $status true to allow, false to not set
*/
private function set_xmlrpc_cap($status) {
private function set_protocol_cap($status) {
global $CFG;
if ($status and !$this->is_xmlrpc_cap_allowed()) {
if ($status and !$this->is_protocol_cap_allowed()) {
//need to allow the cap
$permission = CAP_ALLOW;
$assign = true;
} else if (!$status and $this->is_xmlrpc_cap_allowed()){
} else if (!$status and $this->is_protocol_cap_allowed()){
//need to disallow the cap
$permission = CAP_INHERIT;
$assign = true;
}
if (!empty($assign)) {
$systemcontext = get_system_context();
assign_capability('webservice/xmlrpc:use', $permission, $CFG->defaultuserroleid, $systemcontext->id, true);
assign_capability('webservice/rest:use', $permission, $CFG->defaultuserroleid, $systemcontext->id, true);
}
}

Expand Down Expand Up @@ -6834,7 +6849,7 @@ public function get_setting() {
require_once($CFG->dirroot . '/webservice/lib.php');
$webservicemanager = new webservice();
$mobileservice = $webservicemanager->get_external_service_by_shortname(MOODLE_OFFICIAL_MOBILE_SERVICE);
if ($mobileservice->enabled and $this->is_xmlrpc_cap_allowed()) {
if ($mobileservice->enabled and $this->is_protocol_cap_allowed()) {
return $this->config_read($this->name); //same as returning 1
} else {
return 0;
Expand All @@ -6860,6 +6875,7 @@ public function write_setting($data) {
require_once($CFG->dirroot . '/webservice/lib.php');
$webservicemanager = new webservice();

$updateprotocol = false;
if ((string)$data === $this->yes) {
//code run when enable mobile web service
//enable web service systeme if necessary
Expand All @@ -6875,11 +6891,20 @@ public function write_setting($data) {

if (!in_array('xmlrpc', $activeprotocols)) {
$activeprotocols[] = 'xmlrpc';
$updateprotocol = true;
}

if (!in_array('rest', $activeprotocols)) {
$activeprotocols[] = 'rest';
$updateprotocol = true;
}

if ($updateprotocol) {
set_config('webserviceprotocols', implode(',', $activeprotocols));
}

//allow xml-rpc:use capability for authenticated user
$this->set_xmlrpc_cap(true);
$this->set_protocol_cap(true);

} else {
//disable web service system if no other services are enabled
Expand All @@ -6894,11 +6919,21 @@ public function write_setting($data) {
$protocolkey = array_search('xmlrpc', $activeprotocols);
if ($protocolkey !== false) {
unset($activeprotocols[$protocolkey]);
$updateprotocol = true;
}

$protocolkey = array_search('rest', $activeprotocols);
if ($protocolkey !== false) {
unset($activeprotocols[$protocolkey]);
$updateprotocol = true;
}

if ($updateprotocol) {
set_config('webserviceprotocols', implode(',', $activeprotocols));
}

//disallow xml-rpc:use capability for authenticated user
$this->set_xmlrpc_cap(false);
$this->set_protocol_cap(false);
}

//disable the mobile service
Expand Down
2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -30,7 +30,7 @@
defined('MOODLE_INTERNAL') || die();


$version = 2012101800.00; // YYYYMMDD = weekly release date of this DEV branch
$version = 2012101800.01; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches
// .XX = incremental changes

Expand Down
6 changes: 5 additions & 1 deletion webservice/externallib.php
Expand Up @@ -155,6 +155,9 @@ public static function get_site_info($serviceshortnames = array()) {

$siteinfo['functions'] = $availablefunctions;

// Mobile CSS theme and alternative login url
$siteinfo['mobilecssurl'] = get_config('admin', 'mobilecssurl');

return $siteinfo;
}

Expand Down Expand Up @@ -191,7 +194,8 @@ public static function get_site_info_returns() {
'downloadfiles' => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not',
VALUE_OPTIONAL),
'release' => new external_value(PARAM_TEXT, 'Moodle release number', VALUE_OPTIONAL),
'version' => new external_value(PARAM_TEXT, 'Moodle version number', VALUE_OPTIONAL)
'version' => new external_value(PARAM_TEXT, 'Moodle version number', VALUE_OPTIONAL),
'mobilecssurl' => new external_value(PARAM_URL, 'Mobile custom CSS theme', VALUE_OPTIONAL)
)
);
}
Expand Down
1 change: 1 addition & 0 deletions webservice/tests/externallib_test.php
Expand Up @@ -84,6 +84,7 @@ public function test_get_site_info() {
$this->assertEquals(true, $siteinfo['downloadfiles']);
$this->assertEquals($CFG->release, $siteinfo['release']);
$this->assertEquals($CFG->version, $siteinfo['version']);
$this->assertEquals(get_config('admin', 'mobilecssurl'), $siteinfo['mobilecssurl']);
}

}

0 comments on commit 33af788

Please sign in to comment.