Skip to content

Commit

Permalink
MDL-65093 core_message: deprecate can_post_message()
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Jul 29, 2019
1 parent f622ee9 commit 06d046c
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/deprecatedlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2412,7 +2412,7 @@ function message_page_type_list() {
*/
function message_can_post_message() {
throw new coding_exception('message_can_post_message() can not be used anymore. Please use ' .
'\core_message\api::can_post_message() instead.');
'\core_message\api::can_send_message() instead.');
}

/**
Expand Down
23 changes: 20 additions & 3 deletions message/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1844,29 +1844,46 @@ public static function count_blocked_users($user = null) {
* Determines if a user is permitted to send another user a private message.
* If no sender is provided then it defaults to the logged in user.
*
* @deprecated since 3.8
* @todo Final deprecation in MDL-66266
* @param \stdClass $recipient The user object.
* @param \stdClass|null $sender The user object.
* @return bool true if user is permitted, false otherwise.
*/
public static function can_post_message($recipient, $sender = null) {
global $USER;

debugging('\core_message\api::can_post_message is deprecated, please use ' .
'\core_message\api::can_send_message instead.', DEBUG_DEVELOPER);

if (is_null($sender)) {
// The message is from the logged in user, unless otherwise specified.
$sender = $USER;
}

return self::can_send_message($recipient->id, $sender->id);
}

/**
* Determines if a user is permitted to send another user a private message.
*
* @param int $recipientid The recipient user id.
* @param int $senderid The sender user id.
* @return bool true if user is permitted, false otherwise.
*/
public static function can_send_message(int $recipientid, int $senderid) : bool {
$systemcontext = \context_system::instance();
if (!has_capability('moodle/site:sendmessage', $systemcontext, $sender)) {

if (!has_capability('moodle/site:sendmessage', $systemcontext, $senderid)) {
return false;
}

if (has_capability('moodle/site:readallmessages', $systemcontext, $sender->id)) {
if (has_capability('moodle/site:readallmessages', $systemcontext, $senderid)) {
return true;
}

// Check if the recipient can be messaged by the sender.
return (self::can_contact_user($recipient->id, $sender->id));
return self::can_contact_user($recipientid, $senderid);
}

/**
Expand Down
9 changes: 1 addition & 8 deletions message/classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,7 @@ public static function get_member_info(int $referenceuserid, array $userids, boo
if ($includeprivacyinfo) {
$privacysetting = api::get_user_privacy_messaging_preference($member->id);
$data->requirescontact = $privacysetting == api::MESSAGE_PRIVACY_ONLYCONTACTS;

$recipient = new \stdClass();
$recipient->id = $member->id;

$sender = new \stdClass();
$sender->id = $referenceuserid;

$data->canmessage = !$data->isdeleted && api::can_post_message($recipient, $sender);
$data->canmessage = !$data->isdeleted && api::can_send_message($member->id, $referenceuserid);
}

// Populate the contact requests, even if we don't need them.
Expand Down
2 changes: 1 addition & 1 deletion message/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public static function send_instant_messages($messages = array()) {

// TODO MDL-31118 performance improvement - edit the function so we can pass an array instead userid
// Check if the recipient can be messaged by the sender.
if ($success && !\core_message\api::can_post_message($tousers[$message['touserid']], $USER)) {
if ($success && !\core_message\api::can_send_message($tousers[$message['touserid']]->id, $USER->id)) {
$success = false;
$errormessage = get_string('usercantbemessaged', 'message', fullname(\core_user::get_user($message['touserid'])));
}
Expand Down
4 changes: 1 addition & 3 deletions message/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@
}

if ($userid) {
$recipient = new stdClass();
$recipient->id = $userid;
if (!\core_message\api::can_post_message($recipient)) {
if (!\core_message\api::can_send_message($userid, $USER->id)) {
throw new moodle_exception('Can not contact user');
}
}
Expand Down
Loading

0 comments on commit 06d046c

Please sign in to comment.