Skip to content

Commit

Permalink
MDL-65093 core_message: users can't block any user they want
Browse files Browse the repository at this point in the history
If blocking will have no effect (ie. you are attempting to block
the admin) the 'Block' button will not be shown and instead a
message will be shown explaining why you can not block the user.
  • Loading branch information
mdjnelson committed Jul 29, 2019
1 parent 06d046c commit 90403c5
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 16 deletions.
1 change: 1 addition & 0 deletions lang/en/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
$string['blockuserconfirmbutton'] = 'Block';
$string['blocknoncontacts'] = 'Prevent non-contacts from messaging me';
$string['cancelselection'] = 'Cancel message selection';
$string['cantblockuser'] = 'You are unable to block {$a} because they have a role with permission to message all users';
$string['contactableprivacy'] = 'Accept messages from:';
$string['contactableprivacy_onlycontacts'] = 'My contacts only';
$string['contactableprivacy_coursemember'] = 'My contacts and anyone in my courses';
Expand Down
2 changes: 1 addition & 1 deletion message/amd/build/message_drawer_view_conversation.min.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion message/amd/src/message_drawer_view_conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ function(
isblocked: null,
iscontact: null,
isdeleted: null,
canmessage: null,
canmessage: null,
canmessageevenifblocked: null,
requirescontact: null,
contactrequests: []
};
Expand Down
15 changes: 11 additions & 4 deletions message/amd/src/message_drawer_view_conversation_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1069,10 +1069,17 @@ function(
*/
var renderConfirmBlockUser = function(header, body, footer, user) {
if (user) {
return Str.get_string('blockuserconfirm', 'core_message', user.fullname)
.then(function(string) {
return showConfirmDialogue(header, body, footer, [SELECTORS.ACTION_CONFIRM_BLOCK], string, '', true, false);
});
if (user.canmessageevenifblocked) {
return Str.get_string('cantblockuser', 'core_message', user.fullname)
.then(function(string) {
return showConfirmDialogue(header, body, footer, [], string, '', true, false);
});
} else {
return Str.get_string('blockuserconfirm', 'core_message', user.fullname)
.then(function(string) {
return showConfirmDialogue(header, body, footer, [SELECTORS.ACTION_CONFIRM_BLOCK], string, '', true, false);
});
}
} else {
return hideConfirmDialogue(header, body, footer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ define(['jquery'], function($) {
isblocked: member.isblocked,
iscontact: member.iscontact,
isdeleted: member.isdeleted,
canmessage: member.canmessage,
canmessage: member.canmessage,
canmessageevenifblocked: member.canmessageevenifblocked,
requirescontact: member.requirescontact,
contactrequests: member.contactrequests || []
};
Expand Down
12 changes: 8 additions & 4 deletions message/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1869,9 +1869,11 @@ public static function can_post_message($recipient, $sender = null) {
*
* @param int $recipientid The recipient user id.
* @param int $senderid The sender user id.
* @param bool $evenifblocked This lets the user know, that even if the recipient has blocked the user
* the user is still able to send a message.
* @return bool true if user is permitted, false otherwise.
*/
public static function can_send_message(int $recipientid, int $senderid) : bool {
public static function can_send_message(int $recipientid, int $senderid, bool $evenifblocked = false) : bool {
$systemcontext = \context_system::instance();

if (!has_capability('moodle/site:sendmessage', $systemcontext, $senderid)) {
Expand All @@ -1883,7 +1885,7 @@ public static function can_send_message(int $recipientid, int $senderid) : bool
}

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

/**
Expand Down Expand Up @@ -2966,9 +2968,11 @@ public static function is_user_in_conversation(int $userid, int $conversationid)
*
* @param int $recipientid
* @param int $senderid
* @param bool $evenifblocked This lets the user know, that even if the recipient has blocked the user
* the user is still able to send a message.
* @return bool true if recipient hasn't blocked sender and sender can contact to recipient, false otherwise.
*/
protected static function can_contact_user(int $recipientid, int $senderid) : bool {
protected static function can_contact_user(int $recipientid, int $senderid, bool $evenifblocked = false) : bool {
if (has_capability('moodle/site:messageanyuser', \context_system::instance(), $senderid) ||
$recipientid == $senderid) {
// The sender has the ability to contact any user across the entire site or themselves.
Expand All @@ -2978,7 +2982,7 @@ protected static function can_contact_user(int $recipientid, int $senderid) : bo
// The initial value of $cancontact is null to indicate that a value has not been determined.
$cancontact = null;

if (self::is_blocked($recipientid, $senderid)) {
if (self::is_blocked($recipientid, $senderid) || $evenifblocked) {
// The recipient has specifically blocked this sender.
$cancontact = false;
}
Expand Down
Loading

0 comments on commit 90403c5

Please sign in to comment.