MDL-30674 enrol_guest: Respect
authorAndrew Nicols <andrew@nicols.co.uk>
Fri, 23 Oct 2015 02:39:03 +0000 (10:39 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Fri, 23 Oct 2015 02:40:42 +0000 (10:40 +0800)
enrol/guest/classes/enrol_guest_edit_form.php
enrol/guest/lib.php

index ca413ad..bfcb20d 100644 (file)
@@ -58,6 +58,10 @@ class enrol_guest_edit_form extends moodleform {
         $mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_guest'));
         $mform->addHelpButton('password', 'password', 'enrol_guest');
 
         $mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_guest'));
         $mform->addHelpButton('password', 'password', 'enrol_guest');
 
+        if ($plugin->get_config('requirepassword')) {
+            $mform->addRule('password', get_string('required'), 'required', null);
+        }
+
         $mform->addElement('hidden', 'id');
         $mform->setType('id', PARAM_INT);
         $mform->addElement('hidden', 'courseid');
         $mform->addElement('hidden', 'id');
         $mform->setType('id', PARAM_INT);
         $mform->addElement('hidden', 'courseid');
@@ -92,11 +96,8 @@ class enrol_guest_edit_form extends moodleform {
         }
 
         if ($checkpassword) {
         }
 
         if ($checkpassword) {
-            $require = $plugin->get_config('requirepassword');
             $policy  = $plugin->get_config('usepasswordpolicy');
             $policy  = $plugin->get_config('usepasswordpolicy');
-            if ($require && empty($data['password'])) {
-                $errors['password'] = get_string('required');
-            } else if ($policy) {
+            if ($policy) {
                 $errmsg = '';
                 if (!check_password_policy($data['password'], $errmsg)) {
                     $errors['password'] = $errmsg;
                 $errmsg = '';
                 if (!check_password_policy($data['password'], $errmsg)) {
                     $errors['password'] = $errmsg;
index 7d3d569..7c0bfd2 100644 (file)
@@ -358,7 +358,27 @@ class enrol_guest_plugin extends enrol_plugin {
      */
     public function can_hide_show_instance($instance) {
         $context = context_course::instance($instance->courseid);
      */
     public function can_hide_show_instance($instance) {
         $context = context_course::instance($instance->courseid);
-        return has_capability('enrol/guest:config', $context);
+        if (!has_capability('enrol/guest:config', $context)) {
+            return false;
+        }
+
+        // If the instance is currently disabled, before it can be enabled, we must check whether the password meets the
+        // password policies.
+        if ($instance->status == ENROL_INSTANCE_DISABLED) {
+            if ($this->get_config('requirepassword')) {
+                if (empty($instance->password)) {
+                    return false;
+                }
+            }
+
+            if ($this->get_config('usepasswordpolicy')) {
+                if (!check_password_policy($instance->password, $errmsg)) {
+                    return false;
+                }
+            }
+        }
+
+        return true;
     }
 
     /**
     }
 
     /**