Give capability x access to local plugin settings page

Give capability x access to local plugin settings page

by Joanna Beaver -
Number of replies: 4
Picture of Plugin developers

Hello

I have a local plugin + settings page and I need a person other than admin with a capability x granted at system level to be able to view it.

I have followed the advice given here https://moodle.org/mod/forum/discuss.php?d=376316 except I had to change 'root' in this line to be 'localplugins' in order for it to show (even to admin).
$ADMIN->add('root', new admin_category('local_myplugin', new lang_string('pluginname', 'local_myplugin')));

Here is my code:

$systemcontext = context_system::instance();
$ismanager = has_capability('local/myplugin:dosomething', $systemcontext);

if ($hassiteconfig || $ismanager) {
$ADMIN->add('localplugins', new admin_category('local_myplugin', new lang_string('pluginname', 'local_myplugin')));
$ADMIN->add('local_myplugin', new admin_externalpage('mypluginsettings', new lang_string('mypluginsettings', 'local_myplugin'), new moodle_url('/local/pluginname/mypluginsettings.php'),'local/myplugin:dosomething'));
}

$capabilities = array(
'local/myplugin:dosomething' => [
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array( 'manager' => CAP_ALLOW )
] );

It is recognising that the testuser has the capability but neither the category nor page are visible to this person. (It is a role with archetype Manager that has this capability allowed, and the role is assigned at system level).

A few questions,

  1. any idea why it is not working? (Moodle 3.9)
  2. does it always have to be an admin_externalpage to make it work with a defined capability?
  3. if 2, does this mean the usual settings page syntax cannot be used and I have to revert to an mform?

Best wishes

Jo

Average of ratings: -
In reply to Joanna Beaver

Re: Give capability x access to local plugin settings page

by Benjamin Ellis -
Picture of Particularly helpful Moodlers
Hi,

Just provide another way (link) for that user to access the local plugin - maybe a block on the Dashboard or use the navigation callbacks - https://docs.moodle.org/dev/Navigation_API#Local_Plugins - to add a new navigation item for users with that capability
Average of ratings: Useful (1)
In reply to Benjamin Ellis

Re: Give capability x access to local plugin settings page

by Joanna Beaver -
Picture of Plugin developers
That's a good idea, I'll give it a go.

Thanks!
In reply to Joanna Beaver

Re: Give capability x access to local plugin settings page

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
I thought that $ADMIN is an 'admin' only global or such that settings will only work for admins. Could be better off with your own custom page and use get_config() etc. to get the value of the setting.
In reply to Gareth J Barnard

Re: Give capability x access to local plugin settings page

by Joanna Beaver -
Picture of Plugin developers
That was the conclusion I came to, that there was some other permissions check going on for moodle/site:config, but what confused me is there are quite a few historical forum posts (not too old, 2018 ish) asking same thing and the above code seemed to work for them. Perhaps something has changed in Moodle since then.