• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

is there a method for isModerator ? (XenForo_Visitor::getInstance()) ?!

TBDragon

Active member
#1
hi how r u

i just saw in some addon

XenForo_Visitor::getInstance()->isSuperAdmin()

so it can check whether the visitor is super admin or no

but what if i need to check of moderator or some group id ?!
which class i need to extend so i can use that
or its same in the XenForo_ControllerPublic_Abstract ?!

and thank ^_^
 

Chris D

XenForo developer
Staff member
#4
You should be using an IDE that will suggest functions and properties that are available:

upload_2014-3-20_18-40-4.png

As it happens, you have access to the full user record from the Visitor object. is_moderator is a field in xf_user.

To find out if a visitor is registered or not, just use XenForo_Visitor::getInstance()->getUserId() or XenForo_Visitor::getInstance()->user_id.
 

TBDragon

Active member
#5
You should be using an IDE that will suggest functions and properties that are available:

View attachment 70022

As it happens, you have access to the full user record from the Visitor object. is_moderator is a field in xf_user.

To find out if a visitor is registered or not, just use XenForo_Visitor::getInstance()->getUserId() or XenForo_Visitor::getInstance()->user_id.
ya i think i will do this
=_= as my sublime text dont have this ability =( i will use phpstorm for XF

thanks again ^_^
 

TBDragon

Active member
#6
i have small issue

PHP:
  if (!((XenForo_Visitor::getInstance()->isSuperAdmin()) || (XenForo_Visitor::getInstance()->is_moderator)))
     {
       throw $this->getNoPermissionResponseException();
     }
its give me the getNoPermissionResponseException()
when iam loging as moderator

but if i use the id for example

PHP:
if (!((XenForo_Visitor::getInstance()->isSuperAdmin()) || (XenForo_Visitor::getInstance()->getUserId(31635))))
its work fine y that no permission appear for me >_< and when i used the getUserId($id) work fine ?

btw even the isMemberOf(4) its work fine <<< as the number 4 is the moderator group id !!

y with is_moderator not working

and thanks
 

Chris D

XenForo developer
Staff member
#7
PHP:
if (!((XenForo_Visitor::getInstance()->isSuperAdmin()) || (XenForo_Visitor::getInstance()->getUserId(31635))))
The usage of this is wrong.

getUserId returns the logged in user's ID that's all. It doesn't help you get another user's details.
 

TBDragon

Active member
#8
PHP:
if (!((XenForo_Visitor::getInstance()->isSuperAdmin()) || (XenForo_Visitor::getInstance()->getUserId(31635))))
The usage of this is wrong.

getUserId returns the logged in user's ID that's all. It doesn't help you get another user's details.
yes i know but i just to try the options i have for the usage i need
the isMemberOf(4) is great as this is what i need for moderator but the confusing is y is_moderator is not working ?!
 

Chris D

XenForo developer
Staff member
#9
Being in the Moderating user group does not automatically make you a moderator.

A moderator is a user who has been created as a moderator on this page: admin.php?moderators/

If your user does not appear there, they are not a moderator and therefore is_moderator would be 0.

Also you don't need to set up a new instance every time. Also most of your parenthesises are unnecessary.

PHP:
$visitor = XenForo_Visitor::getInstance();

if (!$visitor->isSuperAdmin() || $visitor->is_moderator)
{
    // Your code
}
 

TBDragon

Active member
#10
Being in the Moderating user group does not automatically make you a moderator.

A moderator is a user who has been created as a moderator on this page: admin.php?moderators/

If your user does not appear there, they are not a moderator and therefore is_moderator would be 0.

Also you don't need to set up a new instance every time. Also most of your parenthesises are unnecessary.

PHP:
$visitor = XenForo_Visitor::getInstance();

if (!$visitor->isSuperAdmin() || $visitor->is_moderator)
{
    // Your code
}
i see thats great then ^_^

so even if i need to use it several time on the same php file i just define it one time only then use it
< its not my code i just modify it to fit my need but i will send this to the one who make it to know about it

thanks again brother ^^
 

Daniel Hood

Well-known member
#11
so even if i need to use it several time on the same php file i just define it one time only then use it
It has to be within the same function. For example

PHP:
class Your_Class
{
 function test()
 {
   $visitor = XenForo_Visitor::getInstance();
   var_dump(isset($visitor)); // returns bool(true);
   $this->test2();
 }
 function test2()
 {
  var_dump(isset($visitor)); // returns bool(false);
 }
}
You can use $visitor however much you want within the first function, but you'll have to re-assign it in the second function for it to work.
 

TBDragon

Active member
#12
It has to be within the same function. For example

PHP:
class Your_Class
{
function test()
{
   $visitor = XenForo_Visitor::getInstance();
   var_dump(isset($visitor)); // returns bool(true);
   $this->test2();
}
function test2()
{
  var_dump(isset($visitor)); // returns bool(false);
}
}
You can use $visitor however much you want within the first function, but you'll have to re-assign it in the second function for it to work.
thanks @Daniel Hood , is it possible to make it as global ?! or it will not work in this way !?
 

Chris D

XenForo developer
Staff member
#13
It's not necessary to make it global. Just use it when you need it. If you need to use XenForo_Visitor::getInstance() more than once in a function, that's when you should consider assigning it to a variable, e.g. $visitor.
 

TBDragon

Active member
#14
It's not necessary to make it global. Just use it when you need it. If you need to use XenForo_Visitor::getInstance() more than once in a function, that's when you should consider assigning it to a variable, e.g. $visitor.
hmmm the problem the addon its not mine so idk what every thing happen
but its used in 4 function so its just let the one who have the permission to do the actions