General developer forum

check if user is admin

 
 
Picture of Ricky de Klerck
check if user is admin
 
Hi,

I'm trying to achieve the following:

We have different schools with different students. When a school logs in and tries to enroll a student into a course, we only want to them to see the students from that school. We achieved this by checking the institution. But now there's the problem. An admin must see all student so we want to build an IF construction:

if (user = admin)
{
// show all
}
else
{
// check institution
}

Is there a nice and clean way to check the roll (admin/student etc), so we can build this if construction?

Thanks in advance.
 
Average of ratings: -
Picture of Mark Johnson
Re: check if user is admin
Group DevelopersGroup Particularly helpful Moodlers
First, you'll want to identify a capability that only admin users have, by looking at Administration->Users->Permissions->Define Roles
moodle/site:config might be a good one to use.

Then, you'll need to use get_context_instance to get the context of the course, and use has_capability to work out if the user is an admin. The code will look something like:

$coursecontext = get_context_instance(CONTEXT_COURSE,
$COURSE->id);
if(has_capaibility('moodle/site:config', $coursecontext)) {
//show all
} else {
//check institution
}
 
Average of ratings: -
Picture of Ricky de Klerck
Re: check if user is admin
 
Thanks for the answer. I used your code and it works perfectly!
 
Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: check if user is admin
Group DevelopersGroup Documentation writersGroup Particularly helpful Moodlers
An even better option is to create a new cabability plugintype/myplugin:seeallthings, and then do has_capaibility('plugintype/myplugin:seeallthings', ...).

That way, if you later what to change which roles can see all things, you easily can.
 
Average of ratings: -
Picture of Mark Johnson
Re: check if user is admin
Group DevelopersGroup Particularly helpful Moodlers
Ah yes, my assumption was that he was modifying the core code rather than writing a plugin, but if it is a plugin, then that's the way forward.
 
Average of ratings: -
Picture of Iñaki Arenaza
Re: check if user is admin
Group DevelopersGroup Documentation writersGroup Particularly helpful Moodlers

Better if you use the official way of getting the list of admin users (get_admins()) and check if the current user is one of those smile

This works in 1.x (from 1.7 on) and 2.0. On the other hand the admin users no longer have special capabilities in 2.0 to mark them as such (so your code will not work in 2.0). So something like this could do it:

$admins = get_admins(); $isadmin = false; foreach ($admins as $admin) { if ($USER->id == $admin->id) { $isadmin = true; break; } } if ($isadmin) { // Show all } else { // Check institution }

[Edit: I think Tim's idea is even better smile]

Saludos. Iñaki.

 
Average of ratings: -
Picture of Aaron Leggett
Re: check if user is admin
 

why not just use .. is_siteadmin()

 

if (is_siteadmin())
{
// show all
}
else
{
// check institution
}

 
Average of ratings:Useful (4)
Picture of Hubert Chathi
Re: check if user is admin
 

Why are you resurrecting a 2.5 year old thread?

Anyways, the reason to not do this is that it is better to user the finer-grained capabilities checks than to just check if the user is an "admin".  In security, it's the principle of least priviledge.  You may want someone to see all users, but not necessarily have access to all the other admin stuff; siteadmin on a Moodle site can do pretty much anything.

 
Average of ratings: -