Code question regarding capabilities and roles

Code question regarding capabilities and roles

by Florin Go -
Number of replies: 4

I have a button that I want only those that have teachers role to see. 

I have set the following via code and admin area and the changes done in the admin area seem to be ignored.


For the first image, all people get to see the button, which is as expected.

For the second image, nobody sees the button (not talking about admin).

I also tried to set from CONTEXT_MODULE to CONTEXT_SYSTEM (and change to version to force update) with not result.

I also tried to set prevent for registered user and allow for teacher with no result.


Any idea what I am doing wrong? Or a method / piece of code to do the "show button only to some roles"?


Thank you in advance (will leave the existing code and images below).


////////

in access.php

////////

$capabilities = [

'local/myplugin:access' => [

'riskbitmask' => RISK_PERSONAL,

'captype' => 'read ',

'contextlevel' => CONTEXT_MODULE,

'archetypes' => [

'user' => CAP_ALLOW

]

   ]

];

////////

in somefile.php

////////


if (!has_capability('local/myplugin:access', context_system::instance()){

echo 'Cannot access';

} else{

echo 'Can access';

}

////////

capabilities table with only authenticated user set as allowed


capabilities table with only teacher role set as allowed


Average of ratings: -
In reply to Florin Go

Re: Code question regarding capabilities and roles

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Your capability check is set to check in the system context (i.e. across the entire site), however the 'teacher' role is usually assigned per-course.

So you have two options - 1. assign the 'teacher' role at the site level (very bad idea, unless you really want them to be a teacher for every course on the site), 2. generate a context for the course / activity where you want to check the button and do a capability check against that context.
In reply to Davo Smith

Re: Code question regarding capabilities and roles

by Florin Go -
I forgot to mention in the above thread that I have set the test user as a teacher (and a manager). See picture; did I omit a place where I should have set it up?

I want the first case that you described to happen. What steps do I need to do (if steps, code or link is provided, it will help a lot; please).



In reply to Florin Go

Re: Code question regarding capabilities and roles

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Instructions for assigning system roles can be found here: https://docs.moodle.org/en/Assign_roles#System_context

You appear (unless I'm misreading the screenshot) to have assigned roles on the front-page course, not at the system level.

The teacher role is not normally assigned at the system level (and I cannot stress enough, this is almost _never_ what you actually want to do), so you may need to edit the role itself, before Moodle will allow you to assign it at the system level (when you edit a role definition, you can tick/untick the levels at which it can be assigned).

If you want to assign a role programmatically, you can use the role_assign($roleid, $userid, $contextid) function to do it - https://github.com/moodle/moodle/blob/master/lib/accesslib.php#L1539
In reply to Davo Smith

Re: Code question regarding capabilities and roles

by Florin Go -
Thanks. That fixed it.

The plugin will not use the teacher or other per course roles but more system wide roles so it works fine with me.

Thank you once more for your reply.