MDL-61844 policy: Avoid to accept new policies during current session
authorSara Arjona <sara@moodle.com>
Wed, 11 Apr 2018 01:24:37 +0000 (09:24 +0800)
committerMarina Glancy <marina@moodle.com>
Mon, 16 Apr 2018 03:30:14 +0000 (11:30 +0800)
Avoid redirection for accepting new policies during the manager user session
when $user->policyagreed=0 and he/she activates the first policy for logged in users.

Part of MDL-61864

admin/tool/policy/classes/api.php
admin/tool/policy/classes/output/page_agreedocs.php
admin/tool/policy/tests/api_test.php
admin/tool/policy/tests/behat/managepolicies.feature

index 218e3fd..fab6d03 100644 (file)
@@ -477,7 +477,7 @@ class api {
      * @param int $versionid
      */
     public static function make_current($versionid) {
-        global $DB;
+        global $DB, $USER;
 
         $policyversion = new policy_version($versionid);
         if (! $policyversion->get('id') || $policyversion->get('archived')) {
@@ -498,6 +498,11 @@ class api {
 
         // Reset the policyagreed flag to force everybody re-accept the policies.
         $DB->set_field('user', 'policyagreed', 0);
+
+        // Make sure that the current user is not immediately redirected to the policy acceptance page.
+        if (isloggedin() && !isguestuser()) {
+            $USER->policyagreed = 1;
+        }
     }
 
     /**
index 7fd6ac6..049b7db 100644 (file)
@@ -144,14 +144,7 @@ class page_agreedocs implements renderable, templatable {
                     ];
                 }
                 $this->messages[] = $message;
-            } else if (empty($this->policies)) {
-                // There are no policies to agree to. Update the policyagreed value to avoid display empty consent page.
-                $currentuser = (!empty($this->behalfuser)) ? $this->behalfuser : $USER;
-                // Check for updating when the user policyagreed is false.
-                if (!$currentuser->policyagreed) {
-                    api::update_policyagreed($currentuser);
-                }
-            } else if (empty($USER->policyagreed)) {
+            } else if (!empty($this->policies) && empty($USER->policyagreed)) {
                 // Inform users they must agree to all policies before continuing.
                 $message = (object) [
                     'type' => 'error',
index fcba135..547bffc 100644 (file)
@@ -551,4 +551,19 @@ class tool_policy_api_testcase extends advanced_testcase {
         $this->assertEquals(1, $DB->count_records('tool_policy_acceptances',
             ['userid' => $user2->id, 'policyversionid' => $policy->id]));
     }
+
+    /**
+     * Test that user can login if sitepolicyhandler is set but there are no policies.
+     */
+    public function test_login_with_handler_without_policies() {
+        global $CFG;
+
+        $this->resetAfterTest();
+        $user = $this->getDataGenerator()->create_user();
+        $this->setUser($user);
+
+        $CFG->sitepolicyhandler = 'tool_policy';
+
+        require_login(null, false, null, false, true);
+    }
 }
index 5088f89..5291c09 100644 (file)
@@ -40,8 +40,7 @@ Feature: Manage policies
     And I log out
 
   Scenario: Create new policy and save as active
-    When I log in as "admin"
-    # TODO MDL-61844 change to manager!
+    When I log in as "manager"
     And I navigate to "Privacy and policies > Manage policies" in site administration
     And I follow "New policy"
     And I set the following fields to these values:
@@ -153,8 +152,7 @@ Feature: Manage policies
     Given the following policies exist:
       | Name       | Revision | Content    | Summary     | Status   |
       | Policy1    | v1       | full text2 | short text2 | draft    |
-    And I log in as "admin"
-    # TODO MDL-61844 change to manager!
+    And I log in as "manager"
     And I navigate to "Privacy and policies > Manage policies" in site administration
     And I open the action menu in "Policy1" "table_row"
     And I click on "Edit" "link" in the "Policy1" "table_row"
@@ -173,8 +171,7 @@ Feature: Manage policies
     Given the following policies exist:
       | Name       | Revision | Content    | Summary     | Status   |
       | Policy1    | v1       | full text2 | short text2 | draft    |
-    And I log in as "admin"
-    # TODO MDL-61844 change to manager!
+    And I log in as "manager"
     And I navigate to "Privacy and policies > Manage policies" in site administration
     And I open the action menu in "Policy1" "table_row"
     And I click on "Set status to \"Active\"" "link" in the "Policy1" "table_row"