470 Commits

Author SHA1 Message Date
martinlanghoff
9f560a0ccb accesslib: fix SQL CAST()s to work on MySQL and PostgreSQL
CAST() target types aren't very portable. Use DECIMAL which works
for MySQL and Pg.

DECIMAL does seem to be supported in Oracle - but the syntax seems
different. We may still need a compat function.
2007-09-19 07:48:31 +00:00
martinlanghoff
9264620341 accesslib: remove has_capability_including_child_contexts()
No code is calling it, and it never appeared in any release.
2007-09-19 07:48:19 +00:00
martinlanghoff
41709a387f accesslib: replace join() and sql_intarray_to_in() with implode()
implode() is the PHP-ish way of saying this. join() is too Perlish and
may get confused (or misgrepped) with a SQL JOIN.
2007-09-19 07:47:11 +00:00
martinlanghoff
394074424a accesslib: Introducing is_siteadmin() to reliably check for siteadmins
is_siteadmin checks a few key capabilities to suss out if the user is
an admin. The main virtue of the function is that it does not use
the accesslib infrastructure -- it reads directly from the DB, which
is useful for the 1.9 accesslib upgrade.
2007-09-19 07:30:09 +00:00
martinlanghoff
d4bec85829 accesslib: get_user_courses_bycap() fix to deal with empty $order
If we are passed an empty string for $order, still create valid
SQL. Some callers in 1.9 seem to not care about order, passing
an explicit ''. Shocking! ;-)
2007-09-19 07:29:43 +00:00
martinlanghoff
1e460fdfc2 accesslib: remove references to deprecated context_rel table and insert_context_rel()
These references to the deprecated functions were erroring out. Remove
them.

Note however that other role related cleanups done as part of
MDL-10679 "improvement to context_rel table and load_user_capability()"
are kept.
2007-09-19 07:29:31 +00:00
martinlanghoff
ad4c7473a9 accesslib: build_context_path() is now much much cheaper
We now populate the context.path only where it's empty,
this means that we take 0.15s instead of 0.6s. More importantly,
we avoid thrashing the DB's indexes pointlessly.

We also support Oracle and its dirty hack here.

And the function now has a $force parameter that can be used to
actually overwrite the paths/depths in case they've been corrupted.
2007-09-19 07:29:07 +00:00
martinlanghoff
a72921ae68 accesslib: Use sql_compat() to remove unportable PostgreSQL-isms 2007-09-19 07:28:31 +00:00
martinlanghoff
2dff3a0681 accesslib: get_assignable_roles() reworked to be constant-queries
get_assignable_roles() was calling user_can_assign() (cost of 1~2 DBq)
once-per-role. Instead, we can do a single DB query that answers
all our questions in one go.

On a Moodle w 8 roles defined, saves 19 DB queries for the course page
for teachers/admins.

NOTE NOTE NOTE! With this patch we drop the insane strip/escape bit.
Only the caller knows if this is for display on html or for other uses,
so we'll be true and not mangle the data.

A review of all callers in 1.8 shows no problem - the strings were being
strip/escaped already.
2007-09-19 07:27:46 +00:00
martinlanghoff
3cdf0dd253 accesslib: get_parent_contexts() bugfix 2007-09-19 07:26:15 +00:00
martinlanghoff
420bfab156 accesslib: has_capability() now loads sub-course accessdata for $ACCESS
When querying capabilities of non-logged-in users, has_capability()
will now load accessdata for the subcontexts as needed.

Without this patch, below-the-course RAs and rdefs were ignored when
checking caps for a user different from $USER. I don't think it is
ever done in current moodle code, so the problem wasn't visible.

In any case - it's fixed ;-)
2007-09-19 07:26:02 +00:00
martinlanghoff
3c2dbf376e accesslib: Fix dirty paths checks on PHPv4
$DIRTYPATHS and its derivatives are all arrays. Not objects.

Drop the silly -> notation.
2007-09-19 07:25:37 +00:00
martinlanghoff
41017705c3 accesslib: phpdoc update get_child_contexts() 2007-09-19 07:25:25 +00:00
martinlanghoff
ad833c4283 accesslib: rm get_role_context_capability() and update phpdoc
Remove unused function.
2007-09-19 07:25:10 +00:00
martinlanghoff
7ac2bd55eb accesslib: remove role_add_lastaccess_entries(),role_remove_lastaccess_entries()
Both of them are dead code in 18_STABLE and HEAD. And if
role_add_lastaccess_entries() is ever used on a large site it will
hammer the DB to bits.

Remove before anyone is foolish enough to try it.
2007-09-19 07:24:57 +00:00
martinlanghoff
efd6fce585 accesslib: require_capability() phpdoc notes 2007-09-19 07:24:43 +00:00
martinlanghoff
0315aeb02f accesslib: get_parent_cats() goes away too...
Not used anymore, and very inefficient.

If you need something like it, use course_category.path
just like we use context.path .
2007-09-19 07:24:26 +00:00
martinlanghoff
8612e194e8 accesslib: remove get_role_caps() merge_role_caps()
Both unused, and not part of the external API.
2007-09-19 07:24:14 +00:00
martinlanghoff
03160d776c accesslib: remove validate_context()
No longer used anywhere. Not really useful outside of accesslib, so no
point in keeping it.
2007-09-19 07:24:02 +00:00
martinlanghoff
56743fabc9 accesslib: get_context_instance() - ignore clearcache
"cleacache" is no longer needed, and nothing is calling it.

Still - handle it as a noop to avoid erroring out with custom/contrib
code.
2007-09-19 07:23:41 +00:00
martinlanghoff
564870b7b8 accesslib: get_parent_contexts() reworked to use context.path
Now that we can use $context->path trivially, and avoid touching the db
for data we already have... Just Do It!
2007-09-19 07:23:30 +00:00
martinlanghoff
5f38222405 accesslib: remove insert_context_rel(), build_context_rel() and others
The context_rel table is not used anymore. Get rid of all references
to it.

TODO: Drop the orphaned table - but we'll do that on HEAD.
2007-09-19 07:23:15 +00:00
martinlanghoff
8e9fa6b1a2 accesslib: mark_context_dirty() - fix race condition
We had a 1s race condition where a user could get their rights loaded
at the exact time an admin is changing roles/caps and see the "old"
data. Or even see a half-updated view of the access controls.

Yuck.

So we fix the race condition backdating the dirtyness. Cheap, but
effective. And then we backdate it some more to cover for minor clock
flutter on clusters (you still need ntp however!).
2007-09-19 07:23:01 +00:00
martinlanghoff
16cb1ef759 accesslib: mark_context_dirty() don't mark it there is no path 2007-09-19 07:22:49 +00:00
martinlanghoff
d4c4ecb808 accesslib: get_user_access_sitewide() fix invalid SQL for users without RAs 2007-09-19 07:22:12 +00:00
martinlanghoff
62a7a32d79 accesslib: has_capability() - fix warnings about FULLME constant 2007-09-19 07:21:23 +00:00
martinlanghoff
1a9b67874f accesslib: has_capability() now supports fake $USER for forum cron
has_capability() can handle the fake user that forum cron sets up
and will load the appropriate accessdata into $USER->access.

This makes forum cron work again. A test comparison between before
this patchseries yields:

With 1 forum post, sent total 24 times
 - Before 11 000 DB queries (approx)
 - After     506 DB queries

With 6 forum posts, sent a total of 452 times
 - Before 47 876 DB queries
 - After   8 256 DB queries

There is a very high variability, but we are going from 100-500
queries per sent email to 18-21 queries per sent email. The
variability probably stems from 2 of the 6 posts being in a 200-user
forum.

Still huge - by the time we are sending the email, we should know
everything we need to know about the user, the forum/thread/post and
the form. The average should be well below 1 DB query per email sent!
2007-09-19 07:20:24 +00:00
martinlanghoff
956b2f102e accesslib: get_user_courses_bycap() also returns categorypath
Costs next to nothing according to testing, and allows us to walk
the categories very cheaply. We'll need this in get_my_courses()...

What a cheapskates I am...
2007-09-19 07:18:37 +00:00
martinlanghoff
3d034f77a8 accesslib: has_cap_fad() - merge switchrole with defaultuserrole
If you are a teacher in course X, you have at least
teacher-in-X + defaultloggedinuser-sitewide. So in the
course you'll have techer+defaultloggedinuser.
We try to mimic that in switchrole.

Thanks to Petr for pointing me to a similar fix in CVS.

Probably related: MDL-10945
2007-09-19 07:18:23 +00:00
martinlanghoff
ceaf847554 accesslib: remove unused static var 2007-09-19 07:18:10 +00:00
martinlanghoff
56d219eaef accesslib: remove deprecated load_defaultuser_role() 2007-09-19 07:17:57 +00:00
martinlanghoff
e7c8160b6f accesslib: delete_context() now appropriately marks the context dirty. 2007-09-19 07:17:34 +00:00
martinlanghoff
55e68c29c8 accesslib: load_all_capabilities() must clear USER->mycourses 2007-09-19 07:17:08 +00:00
martinlanghoff
7293b3c6bd accesslib: drop rdef mangling part two
We had quite a bit of leftover rdef and ra mangling.
Be more thorough and clear it up.

While at it, make load_user_accessdata() and load_all_capabilities()
more consistent.
2007-09-19 07:16:56 +00:00
martinlanghoff
343effbede accesslib: intro load_temp_role() for temporary guest access
Loads a "ghost" role assignment, and all the needed rdef data
into an accessinfo array.

Also minor phpdoc update on get_role_access()
2007-09-19 07:16:29 +00:00
martinlanghoff
3ac81bd159 accesslib: drop rdef mangling for default role - check in has_cap_fad()
There are some exceptions when checking for caps that are inherited
from the default role. Move the check into has_cap_fad() and stop
mangling the data we put in $ad[rdef].

We now also set $ad[dr] to record default roles added.

This will later allow us to share rdef across many users in $ACCESS.

Affects:
  load_user_accessdata()
  has_cap_fad()

While at it, document has_cap_fad() a bit.
2007-09-19 07:16:18 +00:00
martinlanghoff
33b6014f72 accesslib:load_all_capabilities() - fix guest user setup for multi-enrol
When setting things up for the guest user, the RA entry in accessdata
was not multi-enrol-friendly. Must have glossed it over in the
multi-enrol rework.
2007-09-19 07:16:04 +00:00
martinlanghoff
51be70d2e2 accesslib/moodlelib: check $USER->access[rsw] for roleswitches 2007-09-19 07:15:50 +00:00
martinlanghoff
92e531681d accesslib: Document accessdata, public api, stale accessdata 2007-09-19 07:15:27 +00:00
martinlanghoff
31c2de8234 accesslib: rename "sess" and "accessinfo" to accessdata
The name for new data structure holding access control data
is "accessdata". And we have a new moniker "fad", short for
"from accessdata".

So
 - has_cap_fromsess()    -> has_cap_fad()
 - access_inaccessdata() -> path_inaccessdata()
 - aggr_roles_fromsess() -> aggr_roles_fad()
 - $sess   -> $ad
 - $access -> $ad

Consistency, save typing, shorter codelines...
2007-09-19 07:15:12 +00:00
martinlanghoff
0c24aa1929 accesslib:require_capability() revamp to check for USER->access
$USER->capabilities is deprecated.
2007-09-19 07:14:59 +00:00
martinlanghoff
0d8a77c978 accesslib:context_moved() remove debugging msgs 2007-09-19 07:14:48 +00:00
martinlanghoff
b4ab7f2ec9 accesslib:content_moved() fix bug in depth handling
the depth was getting a bit out-of-control when moving
up the tree. Fix it...
2007-09-19 07:14:11 +00:00
martinlanghoff
ca92b39158 accesslib: create_context() fix to also populate the depth
Set the depth field correctly.
2007-09-19 07:13:45 +00:00
martinlanghoff
e4fec27340 accesslib: Introducing context_moved() - call after moving courses/coursecats
With the new accesslib, moving courses and categories has a major
impact on enrolments and unenrolments.

At _least_ we need to signal accesslib that it has happened. So here
is context_moved() for exactly that.

Open to refactoring later into something along the lines of

 - move_course()
 - move_category()

However, at this stage the most important of those two: move_course()
does not fit very well with the code in course/edit. So keep it simple
for now.
2007-09-19 07:13:33 +00:00
martinlanghoff
e40413befa accesslib: create_context() now populates the path of new contexts
... and it populates the context cache too.

Unfortunately, it needs an INSERT followed by an UPDATE. Other than
a stored procedure, I don't know how to deal with this better.

(We could save the SELECT though! that's a thought...)

OTOH, we are getting so much mileage out of the path field
that it's probably a hit we have to take in the chin and move on.
2007-09-19 07:13:08 +00:00
martinlanghoff
4881f2d329 accesslib: simplify and speedup create_context()
Callers _must_ do their homework before calling create_context().
This allows us to save 2/3 queries per call (!!!).

As it stands, callers are all in accesslib anyway.
2007-09-19 07:12:56 +00:00
martinlanghoff
54f9d9aee7 accesslib:reload_all_capabilities() - force a reload of mycourses
unset $USER->mycourses to force a reload of mycourses too.
2007-09-19 07:12:33 +00:00
martinlanghoff
a9d4ea7849 All interactive enrol/unenrol codepaths mark the context dirty
Manually enrolling and unenrolling self, and other users should
transparently set the context dirty. So walk all callers to
role_assign() and role_unassign() and mark the context dirty
where appropriate.

OTOH, most automated-backend enrol/unenrol mechanisms should not.
The backend lookups that happen when you login are well covered
by the login/enrolment process, and don't need to be marked dirty.
2007-09-19 07:11:42 +00:00
martinlanghoff
ef989bd95e accesslib: Introduce (and use) reload_all_capabilities()
If accessinfo is stale, we need to reload it without losing
out "interesting" state -- transparently for the end user.

That means preserving active role switches, loginas (site and course
level), etc. The logic for that is encapsulated in
reload_all_capabilities().

Also affected:
 - has_capability() which now calls reload_all_capabilities()
 - role_switch() - minor tidyup
2007-09-19 07:11:18 +00:00