Hook Usergroup change

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.
 
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).
 
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.
 
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
 
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
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.
 
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.
 
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.
Ah but is there one single function that is called whenever the usergroup is changed regardless of where?
DERP read your entire post.
 
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.
 
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.
Alright will do :) Thanks mate!
 
Top Bottom