views:

199

answers:

4

I am having tons of fun working on a big project that was, for reasons hard to justify, based on Joomla! (which I don't mean to criticise, Joomla! is great, just not for the task I am faced with currently) and when I googled for a way of determining whether the currently logged-in user is an Admin, I found a post that quite boldly recommends using the following code:

$user =& JFactory::getUser();
if($user->usertype == "Super Administrator" || $user->usertype == "Administrator"){ ... }

To me, this looks like a rather strange way of checking for Admin users. I would appreciate a $user->isAdmin() method to do this rather than a couple of hard-coded strings.

I fail to find a more elegant solution to checking for admin users within the Joomla! framework. Can anyone help?

+1  A: 

Peter,

I concur on the joomla sentiments, we use .net/php here as well and have a few projects that were started on joomla for some unknown reason !!

amnyway, another finer grained approach may be to examine the actual rights that the user has, rather than them being suoper admin etc. you can get to this info along the following lines:

    $user =& JFactory::getUser();

    if ($user->authorize('com_content', 'edit', 'content', 'all')) {
            echo "<p>You may edit all content.</p>";
    } else {
            echo "<p>You may not edit all content.</p>";
    }

    if ($user->authorize('com_content', 'publish', 'content', 'own')) {
            echo "<p>You may publish your own content.</p>";
    } else {
            echo "<p>You may not publish your own content.</p>";
    }

i know it's still hardcoded but at least it's user specific, rather than priviledge specific. this approach does however allow you to target specific 'component' related priviledges, so might be useful for you.

I'll track the replies to see if there's a 'proper' answer as it definately is an omission.

jim

jim
cheers, jim. I was just looking at the authorize() function but it's not quite what I need. I often need to check specifically for the one and only super administrator and then filter or not filter some of entries in the back-end administration panel. I often need to check whether the logged in user is 'Super Admin' or 'Admin'.
Peter Perháč
peter - in that case, the search continues :)
jim
I guess I'll have to offer a bounty :-) but only a very small one
Peter Perháč
A: 

Hi Peter,

The following could be a hack on Joomla. I tried it and got worked.

Take the case of sessions

$_SESSION[__default][user]->usertype;

This will give the type of user logged in you can use this in any conditional statements

srinivas
sorry but I know how to get to the current user's usertype attribute, as you could have noticed in the question itself:$user =if($user->usertype == ...But this still only returns a string representation of the user type and then I have to do string comparisons...
Peter Perháč
A: 

If ur joomla administrator username is admin then u can use below code aspeter described before

$user =& JFactory::getUser();
if($user->usertype == "Super Administrator" && $user->username == "admin")
{
  //... do what ever
}
JustLearn
Oh, no, no, no. Don't like the looks of this. The moment someone configures the admin to use a different name, the whole application will break in various places. Plus, it's recommended to change the default admin user name to something else for the production system.
Peter Perháč
sorry Peter, i m also newbie, i just suggest, thanks to updating my knowledge
JustLearn
Better still, use the admin user only to create another Super admin user and deactivate the first one. That's like renaming the administrator-Folder (which is less easier to do) to keep the 90% of easy attacks out.
giraff
@giraff, even better, yep! :-) btw, JustLearn, I see you're just learning, no worries. The down-vote is not from me, I believe even your contribution contributes to the topic., so I'll even that out by an up-vote
Peter Perháč
@Peter Thanks Dear
JustLearn
+2  A: 
giraff
nice answer indeed. Thank you.
Peter Perháč