## General developer forum

### check if user is admin

This discussion has been locked because a year has elapsed since the last post. Please start a new discussion topic.
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: -
Re: check if user is admin
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: - Re: check if user is admin Thanks for the answer. I used your code and it works perfectly! Average of ratings: - Re: check if user is admin 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: - Re: check if user is admin 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: - Re: check if user is admin 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 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 ]

Saludos. Iñaki.

Average of ratings: -
Re: check if user is admin

why not just use .. is_siteadmin()

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

Average of ratings: Useful (6)
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: -
Re: check if user is admin

Hi

I am new as web developper and administrator for Moodle 3.0

This solutions works for me but .....

there is a plugin who makes that ?

What could happend when i update moodle version ?

Thank You

Average of ratings: -