has_capability database query

has_capability database query

by Petr Skoda -
Number of replies: 2

At present finding out where user has given capability is very expensive because capability checks have to be done in PHP code. This is a problem in areas such as autocomplete form elements or reports.

A new API would accept userid and capability parameters and it would return a subquery that selects all contexts where user has the capability.

Examples of use:

  1. Find all cohorts where I have view cohort capability.
  2. Is there any course where I have update course capability?
  3. Find all courses where I am enrolled or have course view capability.

New database table "context map" will need to be implemented to map parent-descendant relationships of all contexts and their distance. This will require different highly optimised code for each SQL family. Core context classes should be later tweaked to keep the map data up-to-date.

Using this new context map table instead of context.path column is what makes all this possible. It may be necessay to add/update indexes in existing core tables. Adding new context.parent column is another option to improve the map maintenance and context.path building performance.

The optimised map maintenance queries could be later re-used for mapping of other tree structures.

In reply to Petr Skoda

Re: has_capability database query

by Marina Glancy -
This sounds cool! Could it also help with finding a list of users who have access to the activity? This is a combination of capability checks, enrollments, group membership and availability checks. Currently there's a function to "filter list of users" in the availability API and afaik it checks students one by one.
In reply to Marina Glancy

Re: has_capability database query

by Petr Skoda -
In theory it can be used for anything, we just need to figure out how well it performs in MySQL and PostgreSQL - which should be possible to do with an early prototype. I am not worried about the context map building part, that can be later done in adhoc task if slow.