MDL-35238 Add a new admin setting to enable updates deployment
authorDavid Mudrák <david@moodle.com>
Wed, 12 Sep 2012 01:06:00 +0000 (03:06 +0200)
committerDavid Mudrák <david@moodle.com>
Thu, 8 Nov 2012 21:33:06 +0000 (22:33 +0100)
admin/settings/server.php
lang/en/admin.php
lib/adminlib.php

index 0c8746e..fe2a3dc 100644 (file)
@@ -228,6 +228,8 @@ if (empty($CFG->disableupdatenotifications)) {
     $temp = new admin_settingpage('updatenotifications', new lang_string('updatenotifications', 'core_admin'));
     $temp->add(new admin_setting_configcheckbox('updateautocheck', new lang_string('updateautocheck', 'core_admin'),
                                                 new lang_string('updateautocheck_desc', 'core_admin'), 1));
+    $temp->add(new admin_setting_updateautodeploy('updateautodeploy', new lang_string('updateautodeploy', 'core_admin'),
+                                                new lang_string('updateautodeploy_desc', 'core_admin'), 0));
     $temp->add(new admin_setting_configselect('updateminmaturity', new lang_string('updateminmaturity', 'core_admin'),
                                               new lang_string('updateminmaturity_desc', 'core_admin'), MATURITY_STABLE,
                                               array(
index a0c3226..64b01d6 100644 (file)
@@ -1011,6 +1011,9 @@ $string['updatenotificationfooter'] = 'Your Moodle site {$a->siteurl} is configu
 $string['updatenotificationsubject'] = 'Moodle updates are available ({$a->siteurl})';
 $string['updateautocheck'] = 'Automatically check for available updates';
 $string['updateautocheck_desc'] = 'If enabled, your site will automatically check for available updates for both Moodle code and all additional plugins. If there is a new update available, a notification will be sent to site admins.';
+$string['updateautodeploy'] = 'Enable updates deployment';
+$string['updateautodeploy_desc'] = 'If enabled, you will be able to download and install available updates directly from Moodle administration pages. Note that your web server process has to have write access into folders with Moodle installation to make this work. That can be seen as a potential security risk.';
+$string['updateautodeploy_unablewriteplugins'] = 'Can\'t enable the feature - unable to write into plugin locations!';
 $string['updateminmaturity'] = 'Required code maturity';
 $string['updateminmaturity_desc'] = 'Notify about available updates only if the available code has the selected maturity level at least. Updates for plugins that do not declare their code maturity level are always reported regardless this setting.';
 $string['updatenotifybuilds'] = 'Notify about new builds';
index 3813b4d..ede57ab 100644 (file)
@@ -8052,3 +8052,77 @@ class admin_setting_configmultiselect_modules extends admin_setting_configmultis
         return true;
     }
 }
+
+
+/**
+ * Checkbox for the updateautodeploy setting
+ *
+ * This class implements the extra check to make sure that the web server
+ * process user has write access to the $CFG->dirroot.
+ *
+ * @copyright 2012 David Mudrak <david@moodle.com>
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class admin_setting_updateautodeploy extends admin_setting_configcheckbox {
+
+    /**
+     * Sets the value for the setting
+     *
+     * When the feature is just about to be enabled, proceed some extra checks
+     * prior to setting the value.
+     *
+     * @param string $data the checkbox value
+     * @return string empty string or error
+     */
+    public function write_setting($data) {
+
+        // Are we just going to activate the feature?
+        $currentlyenabled = $this->config_read('updateautodeploy');
+        if ((string)$data === $this->yes and empty($currentlyenabled)) {
+            if (!$this->plugin_locations_writeable()) {
+                return get_string('updateautodeploy_unablewriteplugins', 'core_admin');
+            }
+            // TODO MDL-35239 check if the whole dirroot is writeable
+        }
+
+        // Let the parent class actually save the value.
+        return parent::write_setting($data);
+    }
+
+    /**
+     * Check if it is possible to write into plugin locations
+     *
+     * @return bool
+     */
+    private function plugin_locations_writeable() {
+        global $CFG;
+        require_once($CFG->libdir.'/pluginlib.php');
+
+        // Check that the web server process is able to deploy new plugins of all types
+        $plugintypes = get_plugin_types(true);
+        foreach ($plugintypes as $plugintype => $plugintyperoot) {
+            if (!is_writeable($plugintyperoot)) {
+                debugging('Plugin type location not writeable: '.$plugintyperoot, DEBUG_ALL);
+                return false;
+            }
+        }
+
+        // Check that the web server process is able to modify contributed plugins
+        $pluginman = plugin_manager::instance();
+        $plugininfo = $pluginman->get_plugins();
+        foreach ($plugininfo as $plugintype => $plugininstances) {
+            foreach ($plugininstances as $pluginname => $plugininfo) {
+                if ($plugininfo->is_standard()) {
+                    // No need to check for these now until MDL-35239 is implemented
+                    continue;
+                }
+                if (!is_writeable($plugininfo->rootdir)) {
+                    debugging('Contributed plugin directory not writeable: '.$plugininfo->rootdir);
+                    return false;
+                }
+            }
+        }
+
+        return true;
+    }
+}