Advertisement:

Author Topic: Help needed to use user membergroup instead of user id  (Read 3983 times)

virsa punjabi

  • Guest
Help needed to use user membergroup instead of user id
« on: January 10, 2014, 12:44:31 AM »
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.
Code: [Select]
?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>

Offline SoLoGHoST

  • SMF Hero
  • ******
  • Posts: 1,796
  • Gender: Male
  • Real coders do not need to comment their code!
    • Dream Portal
Re: Help needed to use user membergroup instead of user id
« Reply #1 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:

Code: [Select]
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

  • Guest
Re: Help needed to use user membergroup instead of user id
« Reply #2 on: January 10, 2014, 04:41:21 AM »
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'.

Offline SoLoGHoST

  • SMF Hero
  • ******
  • Posts: 1,796
  • Gender: Male
  • Real coders do not need to comment their code!
    • Dream Portal
Re: Help needed to use user membergroup instead of user id
« Reply #3 on: January 10, 2014, 06:09:50 PM »
$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...

Offline ILUXA

  • Semi-Newbie
  • *
  • Posts: 17
Re: Help needed to use user membergroup instead of user id
« Reply #4 on: September 21, 2018, 11:36:30 PM »
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:

Code: [Select]
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
Code: [Select]
// 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>';
« Last Edit: September 21, 2018, 11:55:53 PM by ILUXA »