Forums

access control in V4

darrenhallinan 24 Nov, 2011
I used to use this code to control access in V3
<?php

$user =& JFactory::getUser();
if ( !$user->gid ) {
  $mainframe =& JFactory::getApplication();
  $mainframe->redirect('index.php', 'You are not allowed to access this page', 'error');
}
?>


it worked great but now when i use it in V4 it just acts as a redirect, so even when a user is logged in it still redirects them.

any one have a fix for this?
GreyHead 24 Nov, 2011
Hi darrenhallinan ,

It looks as though the code has changed in Joomla! 1.6/1,7 because users can now belong to more than one group. I think $user->groups or $user->getAuthorisedGroups() will get you an array of groups. So count($user->groups) will be zero if they are not authorised in any group.

On the other hand maybe you could just check $user->id - that will be zero for guests.

Bob
darrenhallinan 24 Nov, 2011
Hi bob

thanks for your replay as always, I really wish I could follow what you were explaining. . .
are you suggesting something like:
    <?php

    $user =& JFactory:getAuthorisedGroups();
    if ( !$user->groups ) {
      $mainframe =& JFactory::getApplication();
      $mainframe->redirect('index.php', 'You are not allowed to access this page', 'error');
    }
    ?>

The above code does not work! - (for anyone looking for the same fix)

I have to say I am getting into the swing of V4, slowly but the one thing I loved about CC was the way it delt with users and groups. I wonder why max never brought that over to V4?
darrenhallinan 24 Nov, 2011
I found what I was looking for .. .

in the wizard go to: Actions > Security > Authenticator

And add it to the onload. you can config to set to what groups you want to access it and what to do if they are not in that group.

That will do the job! Sweeeeet!
GreyHead 25 Nov, 2011
Hi darrenhallinan,

Perfect - I'd forgotten that was there and focused on the code.

Bob

PS for the code to work you need to check count($user->groups) because it's an array it will always exist. The count checks for an empty array.
This topic is locked and no more replies can be posted.