News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

using smf to restrict menu options

Started by thechronic2001, November 20, 2011, 07:59:40 PM

Previous topic - Next topic

thechronic2001

Hello
I'm looking for some help on how to expand this code, its currently set if member groups 1 or 12 log into forum the see a different menu form others, would it be possible to add onto that for another few usergroups, I would like to add another 3 separate sections like the 1st section onto it, ill try and explain it

groups 1 & 12 can see, (for talking sake) 10/10 sections of a menu
groups 2 & 3 can see 8/10 sections of a menu
groups 4  can see 6/10 sections of a menu
group 5 can see 5/10 sections of a menu
then everyone else can see whats left of the menu

basically i have certain buttons for member groups to use that others cannot be trusted with etc, i currently have it working just now and it is excellent, is it possible to expand the code that far?
heres what i have just now.


<?php
require_once("/forum/SSI.php");
if (
$context['user']['is_guest'])
{
 
redirectexit('index.php');
}
else
{
$allowed_groups = array(1, 12);
$can_see = FALSE;
foreach ($allowed_groups as $allowed)
if (in_array($allowed, $user_info['groups']))
{
$can_see = TRUE;
break;
}

if ($can_see)
{
echo ' (content)
'
;
}
else
{
echo '(content)
'
;
}
}
?>

[/quote]


Kays

Hi, why not download my Custom Permissions mod, (link in my sig), assign a permission for each section and then you can control which member groups have permission to see each section.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

thechronic2001

will that work?,, i've integrated smf into my website and im using smf outside of the forum to restrict menu access on custom php pages

Kays

It should work if the member's permissions are loaded. Try it and see what happens. :)

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

thechronic2001

i cant get that package to install :(

tbh the code is probably better for me as its what i already know an understand lol (well to a certain extent)

Kays

Strange the mod won't install. ???

What errors are you getting and which version SMF are you using?

Otherwise, basically do it in a similar manner as you are you currently doing. Setup an array for each section with the user groups allowed access. Then loop through $user_info['groups'] to check.


<?php

$see_section_1
= array(1,6,9);
$see_section_2 = array(1,2,13);
$see_section_3 = array(1,2 9);

$can_see_section_1 = false;
$can_see_section_2 = false;
$can_see_section_3 = false;

foreach (
$user_info['groups'] as $group)
{
 if (
in_array($group, $see_section_1))
   
$can_see_section_1 = true;

if (
in_array($group, $see_section_2))
   
$can_see_section_2 = true;

 if (
in_array($group, $see_section_3))
   
$can_see_section_3 = true;
}

// Put the menu together.
if ($can_see_section_1)
 echo
'the code for menu section 1';
if (
$can_see_section_2)
 echo
'the code for menu section 2';
if (
$can_see_section_3)
 echo
'the code for menu section 3';
?>

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Oldiesmann

#6
You could also just use array_intersect to avoid having to loop through the list of groups (slightly less code - not likely much of any speed difference between the two). The array_intersect function returns an array of all the values from the first array that are also in the other arrays passed to the function.

<?php

$see_section_1
= array(1,6,9);
$see_section_2 = array(1,2,13);
$see_section_3 = array(1,2 9);

$can_see_section_1 = !empty(array_intersect($user_info['groups'], $see_section_1));
$can_see_section_2 = !empty(array_intersect($user_info['groups'], $see_section_2));
$can_see_section_3 = !empty(array_intersect($user_info['groups'], $see_section_3));

// Put the menu together.
if ($can_see_section_1)
 echo
'the code for menu section 1';
if (
$can_see_section_2)
 echo
'the code for menu section 2';
if (
$can_see_section_3)
 echo
'the code for menu section 3';
?>
Michael Eshom
Christian Metal Fans

Kays

Thanks for that Oldies. :)

I haven't used array_intersect() before and was wondering how it was done.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

thechronic2001

thanks for you replies, i spoent a few hours on the first code and i think i understand it, well sort of, i dont understand the second code :(

what i did find with the first code is,, if any member has an additional group added the code will show 2 menus (1 for as it does even with admin members as obviously they will have full access to the menu, tbh i think ive just confused myself to the point of no return.

is it possible for any of those sections of code give me these options

if user is guest, redirect them to index.php (custom login page)
if user has group id 1- show them this menu  (the group ids are not actual group id's just using for this)
if user has group id 2 or 5 show this menu
if user has group id 7 or 9 show this menu
if user has none of those id's show this menu (but only display 1 menu)

Kays

It's hard to advise not seeing the actual menu.

The thing to do is to break down the menu into sections. If a section is only viewable by certain groups then add an "if" condition as in the posted examples above. If all can see a section, then just echo it out.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

fwitt

Quote from: Oldiesmann on November 21, 2011, 08:34:59 PM
You could also just use array_intersect to avoid having to loop through the list of groups (slightly less code - not likely much of any speed difference between the two). The array_intersect function returns an array of all the values from the first array that are also in the other arrays passed to the function.

<?php

$see_section_1
= array(1,6,9);
$see_section_2 = array(1,2,13);
$see_section_3 = array(1,2 9);

$can_see_section_1 = !empty(array_intersect($user_info['groups'], $can_see_section_1));
$can_see_section_2 = !empty(array_intersect($user_info['groups'], $can_see_section_2));
$can_see_section_3 = !empty(array_intersect($user_info['groups'], $can_see_section_3));

// Put the menu together.
if ($can_see_section_1)
 echo
'the code for menu section 1';
if (
$can_see_section_2)
 echo
'the code for menu section 2';
if (
$can_see_section_3)
 echo
'the code for menu section 3';
?>


Oldies didn't you mean


$can_see_section_1 = !empty(array_intersect($user_info['groups'], $see_section_1));



Oldiesmann

Quote from: fwitt on November 23, 2011, 01:57:51 PM
Quote from: Oldiesmann on November 21, 2011, 08:34:59 PM
You could also just use array_intersect to avoid having to loop through the list of groups (slightly less code - not likely much of any speed difference between the two). The array_intersect function returns an array of all the values from the first array that are also in the other arrays passed to the function.

<?php

$see_section_1
= array(1,6,9);
$see_section_2 = array(1,2,13);
$see_section_3 = array(1,2 9);

$can_see_section_1 = !empty(array_intersect($user_info['groups'], $can_see_section_1));
$can_see_section_2 = !empty(array_intersect($user_info['groups'], $can_see_section_2));
$can_see_section_3 = !empty(array_intersect($user_info['groups'], $can_see_section_3));

// Put the menu together.
if ($can_see_section_1)
 echo
'the code for menu section 1';
if (
$can_see_section_2)
 echo
'the code for menu section 2';
if (
$can_see_section_3)
 echo
'the code for menu section 3';
?>


Oldies didn't you mean


$can_see_section_1 = !empty(array_intersect($user_info['groups'], $see_section_1));




Yeah, that's what I meant. Thanks for pointing that out. Will fix my post above :)
Michael Eshom
Christian Metal Fans

thechronic2001

thanks for your help guys, all sorted now ;)

Advertisement: