News:

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

Main Menu

Help needed to use user membergroup instead of user id

Started by virsa punjabi, January 10, 2014, 12:44:31 AM

Previous topic - Next topic

virsa punjabi

Hi everyone,
I am integrating a comment system to my forum and i want to give premission to different member groups to be able to delete the comment. Blow is the code which uses member id where it says 2 but i want to use different member groups there.
?php
//$userip = $_SERVER['REMOTE_ADDR'];
if($_SESSION['mc']['id'] == 2){?>
&nbsp;&nbsp;<a href="domain.com/*****/****/****/delete_comment.php?c_id=<?php  echo $rows['c_id'];?>" id="CID-<?php  echo $rows['c_id'];?>" class="c_delete">Delete</a>

SoLoGHoST

First off, what version of SMF?  Second, $_SESSION['mc'] is not a normal variable, so questioning where is this created at?  Which leads me to Third, Seems like you haven't provided all code.

In SMF 2.0.x versions:

$user_info['groups'] will give you an array of all group ids that the user is a member of.

So, if you want to perform an if statement on a group with an id of 20, than you could do this:

global $user_info;

if (in_array(20, $user_info['groups']))
// Perform Action
else
// You do not have access to this...


Source for $user_info variable here:  http://wiki.simplemachines.org/smf/$user_info

virsa punjabi

i m using smf version 2.0.6.
can you please describe which int corresponds to the membergroups value. for example : id int Corresponds to the member's database value 'id_member'.

SoLoGHoST

$user_info['id'] = id_member of the user from the SMF members table
$user_info['groups'] = array of all groups that have been assigned to that user.  They keys are unimportant, but the values of the array are the integer values for each group that the user is assigned to.

Let me know if you need further explanation...

ILUXA

Quote from: SoLoGHoST on January 10, 2014, 03:17:05 AM
First off, what version of SMF?  Second, $_SESSION['mc'] is not a normal variable, so questioning where is this created at?  Which leads me to Third, Seems like you haven't provided all code.

In SMF 2.0.x versions:

$user_info['groups'] will give you an array of all group ids that the user is a member of.

So, if you want to perform an if statement on a group with an id of 20, than you could do this:

global $user_info;

if (in_array(20, $user_info['groups']))
// Perform Action
else
// You do not have access to this...


Source for $user_info variable here:  http://wiki.simplemachines.org/smf/$user_info
Still works very well in 2.0.15!
I tried to "google" how to add custom code to check user groups, also I tried to "grep" some
examples for several hours and only this solution helped, thanks! I had to add an ad in custom place
for new users and guests and I've added it successfully! I've added "$user_info" to "global"
of function, where I had to add an ad, then I've added this code

// Show an ad to guests and new users
if (in_array(4, $user_info['groups']) || $context['user']['is_guest'])
        echo '
        <span style="padding-left: 6%;"><a href="ad_link" target="_blank"><img src="img_link"/></a></span>';

Advertisement: