A BUG FOUND in "has_capability" function

A BUG FOUND in "has_capability" function

by Ahmed Eissa -
Number of replies: 10

Hi All,

I am using Moodle 2.7.I am creating a block that could display some information for teachers and stuff only, and display another text for students. After searching in Google, I found this code that could do the job:

            $context = get_context_instance(CONTEXT_COURSE, $courseid);

            if(has_capability('moodle/legacy:student', $context, $sel_user_id))
            {
               $UserType[$recNo] = "Student";
            }
            if(has_capability('moodle/legacy:teacher', $context, $sel_user_id))
            {
               $UserType[$recNo] = "Staff"; //"Assitent Teacher";
            }
            if(has_capability('moodle/legacy:editingteacher', $context, $sel_user_id))
            {
               $UserType[$recNo] = "Staff"; //"Teacher";
            }
            if(has_capability('moodle/legacy:admin', $context, $sel_user_id))
            {
               $UserType[$recNo] = "Staff"; //"ADMIN";
            }

 

Where $sel_user_id is the selected user id been fetched from the database.

 

After running the code, I got this error message:

Legacy capabilities can not be used any more!

 

After searching in Moodle source code for what generating this error, I found in the "\moodle\lib\accesslib.php" at line 367 this code:

    if (strpos($capability, 'moodle/legacy:') === 0) {
        throw new coding_exception('Legacy capabilities can not be used any more!');
    }

 

After commenting this code, the code works fine.

 

Is anyone have any idea about this, and why Moodle developers stopped legacy capabilities?

 

Thanks in advance ;)

 

Best regards,

 

Ahmed

 

 

Average of ratings: -
In reply to Ahmed Eissa

Re: A BUG FOUND in "has_capability" function

by Guillermo Madero -

Hi Ahmed,

Please avoid posting the same subject more than once. This thread is a duplicate of:

https://moodle.org/mod/forum/discuss.php?d=253754#p1100617

In reply to Guillermo Madero

Re: A BUG FOUND in "has_capability" function

by Ahmed Eissa -

I am very sorry... As I am desperate for a solution.

Thanks.

In reply to Ahmed Eissa

Re: A BUG FOUND in "has_capability" function

by Valery Fremaux -
Picture of Plugin developers

Moreover, you cannot qualify your post as something related to a bug. This is a natural evolution of Moodle that is progressing to a cleaner implementatiion structure (always ? evil

The way in the moodle 2.6 code a role is attached to its archetype has changed. So there was no reason these capabilities still be continued for a long time. 

Of course this raises a problem of code maintenance for all contributors in some cases, although outside of very major architecture changes (such the 2.0 introduction), the shift effort often keeps being small.

Average of ratings: Useful (1)
In reply to Valery Fremaux

Re: A BUG FOUND in "has_capability" function

by Ahmed Eissa -

Hi Valery,

Thanks for your reply; If it's not a bug, could you please tell me (in code example) how could I find the role of each user? could you please update my code and make it works?

I am really desperate for a solution.

 

Thanks.

 

Ahmed

In reply to Ahmed Eissa

Re: A BUG FOUND in "has_capability" function

by Séverin Terrier -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators

Hi Ahmed,

I'm not a Moodle programmer, and won't give you a direct technical solution.

But you should take time to understand the way Moodle handles "rights" for people. It's all based on capabilities!

And you should test and rely on these rights : the rights the user has (or not) on a precise capability, in a specific context (your block).

Roles are only a way to group rights on capabilities, and affect them on specific contexts...

Hope this helps,
Séverin

 

 

Average of ratings: Useful (1)
In reply to Séverin Terrier

Re: A BUG FOUND in "has_capability" function

by Ahmed Eissa -

DEAR ADMIN,

 

COULD YOU PLEASE DELETE THIS THREAD?

 

THANKS.

AHMED

In reply to Ahmed Eissa

Re: A BUG FOUND in "has_capability" function

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

Dear Ahmed,

This thread contains a number of useful replies; thus it is not necessary for it to be deleted.

Average of ratings: Useful (1)
In reply to Helen Foster

Re: A BUG FOUND in "has_capability" function

by Ahmed Eissa -

Dear Admin,

 

There are a duplicated thread at this URL: A BUG FOUND in "has_capability" function.

 

Thanks.

 

Ahmed

In reply to Ahmed Eissa

Re: A BUG FOUND in "has_capability" function

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

That thread also contains a number of useful replies; thus it is not necessary for it to be deleted.

If duplicate posts are noticed soon, before they have any replies, it's fine for them to be deleted. However, it's not always possible for moderators to notice them soon, since they may be in a different time zone.