InChapter 1, Moodle Architecture, we covered an overview of Moodle's role and permission system. Now we will get a chance to see the system in action. In a later section, we will also add our own access permissions to Moodle.
We use two Moodle core functions to test for a capability, in this section of code. First, we call the get_context_instance()
function with the site or global context, CONTEXT_SYSTEM
. The function get_context_instance()
will return a context object to us for the requested context type, in this case the system context. Once the context is loaded, we use the require_capability
function to test if the user has the moodle/site:doanything
capability (essentially, the ability to do anything and everything on the site). require_capability
tests the current user's capabilities to see if they have the specified capability in the specified context. If they don't, the page is redirected to an error page:
function...