Access API
From MoodleDocs
The Access API gives you functions so you can determine what the current user is allow to do, and it allows modules to extend Moodle with new capabilities.
Overview
Determining that a user has a given capability
has_capability() and friends
contexts
How to define new capabilities
overview of how to create new capabilities (including string names and help strings)
How to write SQL just for enrolled users
get_enrolled_sql()
....