MDL-32082 message: added a function to make user checks more readable

This commit is contained in:
Andrew Davis 2012-04-04 09:58:22 +07:00
parent 93fb7b528b
commit 85427358fc
2 changed files with 23 additions and 6 deletions

View file

@ -118,12 +118,7 @@ unset($user2id);
// Is the user involved in the conversation? // Is the user involved in the conversation?
// Do they have the ability to read other user's conversations? // Do they have the ability to read other user's conversations?
// There will always be a $user1 if (!message_current_user_is_involved($user1, $user2) && !has_capability('moodle/site:readallmessages', $context)) {
// but $user2 may be null. For example, if viewing $user1's recent conversations
if ($user1->id != $USER->id
&& (empty($user2) || $user2->id != $USER->id)
&& !has_capability('moodle/site:readallmessages', $context)){
print_error('accessdenied','admin'); print_error('accessdenied','admin');
} }

View file

@ -2418,3 +2418,25 @@ function translate_message_default_setting($plugindefault, $processorname) {
function message_page_type_list($pagetype, $parentcontext, $currentcontext) { function message_page_type_list($pagetype, $parentcontext, $currentcontext) {
return array('messages-*'=>get_string('page-message-x', 'message')); return array('messages-*'=>get_string('page-message-x', 'message'));
} }
/**
* Is $USER one of the supplied users?
*
* $user2 will be null if viewing a user's recent conversations
*
* @param stdClass the first user
* @param stdClass the second user or null
* @return bool True if the current user is one of either $user1 or $user2
*/
function message_current_user_is_involved($user1, $user2) {
global $USER;
if (empty($user1->id) || (!empty($user2) && empty($user2->id))) {
throw new coding_exception('Invalid user object detected. Missing id.');
}
if ($user1->id != $USER->id && (empty($user2) || $user2->id != $USER->id)) {
return false;
}
return true;
}