Adding a capability to a role during clean installation

Adding a capability to a role during clean installation

by James Brisland -
Number of replies: 2

Hi people,

I'm currently adding a new capability to Moodle but I cannot get it to assign the capability on a clean install.

I have put the following into lib/db/access.php

'moodle/my:manageblocks' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM
)

This adds the capability into the <prefix>_capability table, but I cannot find the correct place to put the assign_capability() call for clean installation, although I have got it working for upgrade - I put the following into lib/db/upgrade.php

[code]
if ($result && $oldversion < [VERSONDATE]) {
// Add in the new moodle/my:manageblocks capibility to the default user role
$context = get_context_instance(CONTEXT_SYSTEM, SITEID); assign_capability('moodle/my:manageblocks', CAP_ALLOW, $CFG->defaultuserroleid, $context->id);
}
[/code]

I'm assuming there is a similar place to put this for clean install, but I cannot find it.


Could someone please point me in the right direction?

Average of ratings: -
In reply to James Brisland

Re: Adding a capability to a role during clean installation

by James Brisland -
--- UPDATE (copy and pasted from my tracker entry - http://tracker.moodle.org/browse/MDL-9802) ---
I think I have figured it out, but what to confirm what I am doing is correct.

The code in access.php was as follows

'moodle/my:manageblocks' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM
)

But I found out that if I don't but the legacy array in it doesn't actually assign the capability to any role, so I changed it to the following.

'moodle/my:manageblocks' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'legacy' => array(
'user' => CAP_ALLOW
)
)

This seems to work correctly, but is it the correct way to assign the new capability to the default user role (which is "user"), and is it ok to assume that on install the role "user" will always be the default role, or can I get the id of the default user role as I do in the upgrade.php script through $CFG->defaultuserroleid?

Hope this all makes sense!

Regards,

James
In reply to James Brisland

Re: Adding a capability to a role during clean installation

by Helen Foster -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Hi James,

It seems like you had a useful discussion with Yu in MDL-9802 rather than in this thread. wink Never mind, I'm glad you sorted things out and thanks for your work. smile