1. 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()) ?!

Discussion in 'XenForo Development Discussions' started by TBDragon, Mar 20, 2014.

  1. TBDragon

    TBDragon Active Member

    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 ^_^
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    XenForo_Visitor::getInstance()->is_moderator;
     
    TBDragon likes this.
  3. TBDragon

    TBDragon Active Member

    ahh i see
    all the time is was try isModerator =_=
    where i can find all type of instance available to use ? (ex register - not registered )
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    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 likes this.
  5. TBDragon

    TBDragon Active Member

    ya i think i will do this
    =_= as my sublime text dont have this ability =( i will use phpstorm for XF

    thanks again ^_^
     
  6. TBDragon

    TBDragon Active Member

    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
     
  7. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  8. TBDragon

    TBDragon Active Member

    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 ?!
     
  9. Chris D

    Chris D XenForo Developer Staff Member

    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 likes this.
  10. TBDragon

    TBDragon Active Member

    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 ^^
     
  11. Daniel Hood

    Daniel Hood Well-Known Member

    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.
     
    Chris D likes this.
  12. TBDragon

    TBDragon Active Member

    thanks @Daniel Hood , is it possible to make it as global ?! or it will not work in this way !?
     
  13. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  14. TBDragon

    TBDragon Active Member

    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
     
  15. Chris D

    Chris D XenForo Developer Staff Member

    It does not matter how many functions you use it in.
     
    TBDragon likes this.

Share This Page