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

Hook Usergroup change

Discussion in 'XenForo Development Discussions' started by silence, Jul 15, 2013.

  1. silence

    silence Well-Known Member

    What would be the least intensive way to hook when a usergroup is changed, since making a crontab job to constantly check would be overkill.
     
  2. Nobita.Kun

    Nobita.Kun Well-Known Member

    I think this function can help for you.
    XenForo_Model_User::_applyUserGroupChanges
     
    silence likes this.
  3. Jeremy

    Jeremy XenForo Moderator Staff Member

    Add a listener to load_class_model to extend XenForo_Model_User. Call parent::_applyUserGroupChanges in your overwritten _applyUserGroupChanges function, do what you need, modify the return, and return what parent::_applyUserGroupChanges() returned (with your changes).
     
    silence likes this.
  4. silence

    silence Well-Known Member

    Uh that didnt seem to work whatsoever :C

    PHP:
    <?php

    class Teamspeak_Model_User extends XFCP_Teamspeak_Model_User
    {
        protected function 
    _applyUserGroupChanges($userId, array $oldGroupStrings, array $newGroupStrings)
        {
            
    parent::_applyUserGroupChanges($userId$oldGroupStrings$newGroupStrings)
            
    $auth_id $this->_getAccountModel()->getAuthID($userId);
            if (
    $auth_id)
            {
                
    $stored_info $this->_getAccountModel()->checkIdentities($userId$auth_id);
            }
           
            return 
    parent::_applyUserGroupChanges();
        }
        protected function 
    _getAccountModel()
        {
            return 
    $this->getModelFromCache('Teamspeak_Model_Account');
        }
    }
    And I did extend it correctly, since if I put a typo in there it spits out an error for the entire site.
     
  5. Nobita.Kun

    Nobita.Kun Well-Known Member

    PHP:
    <?php

    class Teamspeak_Model_User extends XFCP_Teamspeak_Model_User
    {
        protected function 
    _applyUserGroupChanges($userId, array $oldGroupStrings, array $newGroupStrings)
        {
            
    $response parent::_applyUserGroupChanges($userId$oldGroupStrings$newGroupStrings)
            
    $auth_id $this->_getAccountModel()->getAuthID($response['user_id']);
            if (
    $auth_id)
            {
                
    $stored_info $this->_getAccountModel()->checkIdentities($userId$auth_id);
            }
         
            return 
    $response;
        }
        protected function 
    _getAccountModel()
        {
            return 
    $this->getModelFromCache('Teamspeak_Model_Account');
        }
    }
    Try it
     
    silence likes this.
  6. silence

    silence Well-Known Member

    Thanks but the bigger issue is I never added the rest of the logic to do my modification :|
    My bad lol.
    EDIT
    Actually maybe not. I edited it as the same as my controllerpublic, zero errors and acts like nothing is being executed. I tried var_dumping info but nothing shows up.

    PHP:
    <?php

    class Teamspeak_Model_User extends XFCP_Teamspeak_Model_User
    {
        protected function 
    _applyUserGroupChanges($userId, array $oldGroupStrings, array $newGroupStrings)
        {
            
    $response parent::_applyUserGroupChanges($userId$oldGroupStrings$newGroupStrings);
            
    $auth_id $this->_getAccountModel()->getAuthID($response['user_id']);
            if (
    $auth_id)
            {
                
    $stored_info $this->_getAccountModel()->checkIdentities($userId$auth_id);
                
    $teamspeak $this->getHelper("Teamspeak_Helper_Query");
                
    $teamspeak->_updateServerGroup($user_id$auth_id$stored_info['auth_id']);
            }
      
            return 
    $response;
        }
        protected function 
    _getAccountModel()
        {
            return 
    $this->getModelFromCache('Teamspeak_Model_Account');
        }  
    }
    Also I'm changing the usergroup from the admin cp.
     
  7. Jeremy

    Jeremy XenForo Moderator Staff Member

    That function isn't called when manually updating user groups. You'll want to look into modifying the DataWriter using load_class_datawriter and XenForo_DataWriter_User.

    It may also be worth while to checkout XenForo_ControllerAdmin_User::actionSave() to see the process, functions used, etc.
     
  8. silence

    silence Well-Known Member

    Ah but is there one single function that is called whenever the usergroup is changed regardless of where?
    DERP read your entire post.
     
  9. Jeremy

    Jeremy XenForo Moderator Staff Member

    The DataWriter. If you are ever looking for a place that does everything, my best suggestion would be to read code (I personally feel it is the best way to learn how to code, learn a specific piece of software, will make anyone a better developer, and build self-reliance). If you read XenForo_Model_User::_applyUserGroupChanges() and XenForo_ControllerAdmin_User::actionSave() as two places that its possible to change a user's user group, you can trace where it's changing the group and begin to understand where to change. I would give both of those a read through to determine where you should be extending and modifying.
     
    silence likes this.
  10. silence

    silence Well-Known Member

    Alright will do :) Thanks mate!
     

Share This Page