Simple Machines Community Forum

SMF Support => SMF 1.1.x Support => Topic started by: vypergt on August 08, 2008, 11:15:45 PM

Title: Restrict access
Post by: vypergt on August 08, 2008, 11:15:45 PM
I added this code to parts of my site:

if ($context['user']['is_admin'])
      {
         echo '
            <div>
               Some other stuff here that you want to protect.
            </div>';
      }


and the only user command I know is ['is_admin'] how can find out what the others are for registered members, mods, etc.?
Title: Re: Restrict access
Post by: Phil Serna on August 08, 2008, 11:24:57 PM
I don't get what your trying to do.. what are you trying to do?
Title: Re: Restrict access
Post by: vypergt on August 08, 2008, 11:27:32 PM
Trying to restrict certain content from guests. Now the only group access I know is the ['is_admin'] I don't know what the others are for just regular members, mods, and any other groups I have.

http://docs.simplemachines.org/index.php?topic=789.msg1688#post_restrict

thats where i got the coding from.
Title: Re: Restrict access
Post by: ccbtimewiz on August 08, 2008, 11:31:34 PM
Welcome to the world of PHP. :P

Try doing this.

Load up $user_info into your global declarations.

Input all the groups you want to show into an array.

$x = array(1, 2, 3); <= Group ID numbers you want to show the info to.

Then set a variable false. This is for a later step.

$y = false;

Then actually check out the $user_info variable and grab the data.

foreach ($x as $group) {
if( in_array( $group, $user_info['groups'] ) ) <= If the user is in a group defined in $y and that is defined in the groups context, then proceed.
$y = true; } <= If the data can be grabbed, then that variable is true.

Now, for the actual template part. Let's first check if the variable we set was true.

if( $y ) { <= Basically means that if $y is true, the continue

From this point, do your code.

echo '
            <div>
               Some other stuff here that you want to protect.
            </div>'; }

So it would look like:

global $user_info;

$x = array(1, 2, 3);
$y = false;

foreach ($x as $group) {
if( in_array( $group, $user_info['groups'] ) )
$y = true; }

if( $y ) {
echo '
            <div>
               Some other stuff here that you want to protect.
            </div>';
}

That's the advanced way of checking out a group. SMF comes with default values as well.

$context['user']['is_guest'] <= This checks if the user is in group 0.
$context['user']['is_logged'] <= This checks if the user is not in group 0
allowedTo('moderate_forum') <= This checks if the user has any moderator permissions. Eg, in groups 2 or 3.
$context['user']['is_admin'] <= This checks if the user is permitted to administrate the forum. Eg, in group 1.
Title: Re: Restrict access
Post by: Phil Serna on August 08, 2008, 11:33:22 PM
Quote from: ccbtimewiz on August 08, 2008, 11:31:34 PM
Welcome to the world of PHP. :P

Try doing this.

Load up $user_info into your global declarations.

Input all the groups you want to show into an array.

$x = array(1, 2, 3); <= Their ID numbers.

Then set a variable false. This is for a later step.
$y = false;

Then let's actually check out the $user_info variable and grab the data.

foreach ($x as $group) {
if( in_array( $group, $user_info['groups'] ) )
$y = true; } <= If the data can be grabbed, then that variable is true.

Now, for the actual template part. Let's first check if the variable we set was true.
if( $y ) {

From this point, do your code.

echo '
            <div>
               Some other stuff here that you want to protect.
            </div>'; }

So it would look like:

global $user_info;

$x = array(1, 2, 3);
$y = false;

foreach ($x as $group) {
if( in_array( $group, $user_info['groups'] ) )
$y = true; }

if( $y ) {
echo '
            <div>
               Some other stuff here that you want to protect.
            </div>';
}

talk about welcome to the world of php
Title: Re: Restrict access
Post by: vypergt on August 09, 2008, 03:30:31 PM
Wow. o.k so what if I wanted to use this method. If I put "$context['user']['is_logged']" would that allow access to anyone thats logged in?


$context['user']['is_guest'] <= This checks if the user is in group 0.
$context['user']['is_logged'] <= This checks if the user is not in group 0
allowedTo('moderate_forum') <= This checks if the user has any moderator permissions. Eg, in groups 2 or 3.
$context['user']['is_admin'] <= This checks if the user is permitted to administrate the forum. Eg, in group 1.


Title: Re: Restrict access
Post by: Fustrate on August 09, 2008, 03:40:29 PM
Yes it would.
Title: Re: Restrict access
Post by: ccbtimewiz on August 09, 2008, 03:47:28 PM
Correct. :)

Likewise, !$context['user']['is_guest'] would work as well. The "!" means false. So, if the user isn't a guest.
Title: Re: Restrict access
Post by: vypergt on August 09, 2008, 04:03:48 PM
Alright thanks  :)