Description
Despite the permissions that are explicitly afforded to a role in a course forum, Moodle will not permit the user to reply to a forum discussion if the user is not explicitly enrolled in the course. While it stands to reason that 99% of the time this behavior is desirable, it's arguable that the truly elegant permissions system in Moodle should accommodate the unique needs of some organizations to have non-enrolees reply to posts in a course forum. The code in /mod/forum/lib.php explicitly probibits non-enrollees from replying. What is most noteworthy is that this hard-coded restriction does not apply to all actions within the forum; namely, once properly permissioned, the non-enrolled user can successfully view discussions, start new discussions, edit any post, or delete any post. It is just replying that seems to be explicitly prohibited via the code.
I believe that this code is probably "left over" from the days when Moodle did not have such an elegant, granular permissions system - at which point there probably were hard and fast rules dictating that only course enrolees could participate in forum discussions.
I think that this code ought be considered for removal from /mod/forum/lib.php within forum_user_can_post():
if (!is_viewing($context, $user->id) and !is_enrolled($context, $user->id, '', true))
A detailed discussion is contained here:
https://moodle.org/mod/forum/discuss.php?d=168181#p961132