MDL-63211 core_message: respect new setting 'messagingallusers'
authorMark Nelson <markn@moodle.com>
Mon, 1 Oct 2018 03:55:43 +0000 (11:55 +0800)
committerMark Nelson <markn@moodle.com>
Wed, 3 Oct 2018 03:58:39 +0000 (11:58 +0800)
message/classes/api.php
message/externallib.php
message/tests/api_test.php
message/tests/behat/manage_contacts.feature
message/tests/externallib_test.php

index 3189896..8bc93a7 100644 (file)
@@ -1319,6 +1319,30 @@ class api {
         return $conversation->id;
     }
 
+    /**
+     * Checks if a user can create a contact request.
+     *
+     * @param int $userid The id of the user who is creating the contact request
+     * @param int $requesteduserid The id of the user being requested
+     * @return bool
+     */
+    public static function can_create_contact(int $userid, int $requesteduserid) : bool {
+        global $CFG;
+
+        // If we can't message at all, then we can't create a contact.
+        if (empty($CFG->messaging)) {
+            return false;
+        }
+
+        // If we can message anyone on the site then we can create a contact.
+        if ($CFG->messagingallusers) {
+            return true;
+        }
+
+        // We need to check if they are in the same course.
+        return enrol_sharing_course($userid, $requesteduserid);
+    }
+
     /**
      * Handles creating a contact request.
      *
index b9da3ba..f678c2f 100644 (file)
@@ -744,6 +744,16 @@ class core_message_external extends external_api {
         $params = ['userid' => $userid, 'requesteduserid' => $requesteduserid];
         $params = self::validate_parameters(self::create_contact_request_parameters(), $params);
 
+        if (!\core_message\api::can_create_contact($params['userid'], $params['requesteduserid'])) {
+            $warning[] = [
+                'item' => 'user',
+                'itemid' => $params['requesteduserid'],
+                'warningcode' => 'cannotcreatecontactrequest',
+                'message' => 'You are unable to create a contact request for this user'
+            ];
+            return $warning;
+        }
+
         if (!\core_message\api::does_contact_request_exist($params['userid'], $params['requesteduserid'])) {
             \core_message\api::create_contact_request($params['userid'], $params['requesteduserid']);
         }
index dddb60b..0278d67 100644 (file)
@@ -1967,6 +1967,37 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
             \core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
     }
 
+    /**
+     * Test can create a contact request.
+     */
+    public function test_can_create_contact_request() {
+        global $CFG;
+
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+
+        // Disable messaging.
+        $CFG->messaging = 0;
+        $this->assertFalse(\core_message\api::can_create_contact($user1->id, $user2->id));
+
+        // Re-enable messaging.
+        $CFG->messaging = 1;
+
+        // Allow users to message anyone site-wide.
+        $CFG->messagingallusers = 1;
+        $this->assertTrue(\core_message\api::can_create_contact($user1->id, $user2->id));
+
+        // Disallow users from messaging anyone site-wide.
+        $CFG->messagingallusers = 0;
+        $this->assertFalse(\core_message\api::can_create_contact($user1->id, $user2->id));
+
+        // Put the users in the same course so a contact request should be possible.
+        $course = self::getDataGenerator()->create_course();
+        $this->getDataGenerator()->enrol_user($user1->id, $course->id);
+        $this->getDataGenerator()->enrol_user($user2->id, $course->id);
+        $this->assertTrue(\core_message\api::can_create_contact($user1->id, $user2->id));
+    }
+
     /**
      * Test creating a contact request.
      */
index 8fbd3eb..135e154 100644 (file)
@@ -6,10 +6,14 @@ Feature: Manage contacts
 
   Background:
     Given the following "users" exist:
-      | username | firstname | lastname | email            |
+      | username | firstname | lastname | email                |
       | user1    | User      | 1        | user1@example.com    |
       | user2    | User      | 2        | user2@example.com    |
       | user3    | User      | 3        | user3@example.com    |
+    And I log in as "admin"
+    And I set the following administration settings values:
+      | messagingallusers | 1 |
+    And I log out
     And I log in as "user1"
     And I view the "User 2" contact in the message area
     And I click on "Add contact" "link"
index fc8c695..784ad14 100644 (file)
@@ -607,7 +607,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
      * Test creating a contact request.
      */
     public function test_create_contact_request() {
-        global $DB;
+        global $CFG, $DB;
 
         $this->resetAfterTest();
 
@@ -616,6 +616,9 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
 
         $this->setUser($user1);
 
+        // Allow users to message anyone site-wide.
+        $CFG->messagingallusers = 1;
+
         $return = core_message_external::create_contact_request($user1->id, $user2->id);
         $return = external_api::clean_returnvalue(core_message_external::create_contact_request_returns(), $return);
         $this->assertEquals(array(), $return);
@@ -630,6 +633,32 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $this->assertEquals($user2->id, $request->requesteduserid);
     }
 
+    /**
+     * Test creating a contact request when not allowed.
+     */
+    public function test_create_contact_request_not_allowed() {
+        global $CFG;
+
+        $this->resetAfterTest();
+
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+
+        $this->setUser($user1);
+
+        $CFG->messagingallusers = 0;
+
+        $return = core_message_external::create_contact_request($user1->id, $user2->id);
+        $return = external_api::clean_returnvalue(core_message_external::create_contact_request_returns(), $return);
+
+        $warning = reset($return);
+
+        $this->assertEquals('user', $warning['item']);
+        $this->assertEquals($user2->id, $warning['itemid']);
+        $this->assertEquals('cannotcreatecontactrequest', $warning['warningcode']);
+        $this->assertEquals('You are unable to create a contact request for this user', $warning['message']);
+    }
+
     /**
      * Test creating a contact request with messaging disabled.
      */