From 9f82758c01c1483e6f759595d42376130553617f Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Mon, 5 Nov 2018 16:08:48 +0800 Subject: [PATCH] MDL-63724 core_message: fix method signature for can_contact_user() This method required objects, but only used their ids, so has been changed to require only ids. --- message/classes/api.php | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/message/classes/api.php b/message/classes/api.php index 70619370210..fe8f099c5bc 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -1428,7 +1428,7 @@ class api { } // Check if the recipient can be messaged by the sender. - return (self::can_contact_user($recipient, $sender)); + return (self::can_contact_user($recipient->id, $sender->id)); } /** @@ -1465,11 +1465,7 @@ class api { }); $otheruser = reset($otheruser); - // Spoof the stdClass for now - can_contact_user() only uses the ids. - $user = (object) ['id' => $userid]; - $otheruser = (object) ['id' => $otheruser->id]; - - return self::can_contact_user($otheruser, $user); + return self::can_contact_user($otheruser->id, $userid); } else { throw new \moodle_exception("Invalid conversation type '$conversation->type'."); } @@ -2274,12 +2270,12 @@ class api { /** * Checks if the sender can message the recipient. * - * @param \stdClass $recipient The user object. - * @param \stdClass $sender The user object. + * @param int $recipientid + * @param int $senderid * @return bool true if recipient hasn't blocked sender and sender can contact to recipient, false otherwise. */ - protected static function can_contact_user(\stdClass $recipient, \stdClass $sender) : bool { - if (has_capability('moodle/site:messageanyuser', \context_system::instance(), $sender->id)) { + protected static function can_contact_user(int $recipientid, int $senderid) : bool { + if (has_capability('moodle/site:messageanyuser', \context_system::instance(), $senderid)) { // The sender has the ability to contact any user across the entire site. return true; } @@ -2287,7 +2283,7 @@ class api { // The initial value of $cancontact is null to indicate that a value has not been determined. $cancontact = null; - if (self::is_blocked($recipient->id, $sender->id)) { + if (self::is_blocked($recipientid, $senderid)) { // The recipient has specifically blocked this sender. $cancontact = false; } @@ -2301,7 +2297,7 @@ class api { // // The Site option is only possible when the messagingallusers site setting is also enabled. - $privacypreference = self::get_user_privacy_messaging_preference($recipient->id); + $privacypreference = self::get_user_privacy_messaging_preference($recipientid); if (self::MESSAGE_PRIVACY_SITE === $privacypreference) { // The user preference is to allow any user to contact them. // No need to check anything else. @@ -2309,12 +2305,12 @@ class api { } else { // This user only allows their own contacts, and possibly course peers, to contact them. // If the users are contacts then we can avoid the more expensive shared courses check. - $cancontact = self::is_contact($sender->id, $recipient->id); + $cancontact = self::is_contact($senderid, $recipientid); if (!$cancontact && self::MESSAGE_PRIVACY_COURSEMEMBER === $privacypreference) { // The users are not contacts and the user allows course member messaging. // Check whether these two users share any course together. - $sharedcourses = enrol_get_shared_courses($recipient->id, $sender->id, true); + $sharedcourses = enrol_get_shared_courses($recipientid, $senderid, true); $cancontact = (!empty($sharedcourses)); } } @@ -2327,12 +2323,12 @@ class api { // Note: You cannot use empty($sharedcourses) here because this may be an empty array. if (null === $sharedcourses) { - $sharedcourses = enrol_get_shared_courses($recipient->id, $sender->id, true); + $sharedcourses = enrol_get_shared_courses($recipientid, $senderid, true); } foreach ($sharedcourses as $course) { // Note: enrol_get_shared_courses will preload any shared context. - if (has_capability('moodle/site:messageanyuser', \context_course::instance($course->id), $sender->id)) { + if (has_capability('moodle/site:messageanyuser', \context_course::instance($course->id), $senderid)) { $cancontact = true; break; } -- 2.43.0