XF 1.5 Removing a secondary group from a user via PHP

John Coles

Member
Hello all!

I am trying to write a PHP script that will add and remove a secondary user group to a user. The adding seems to work reliably but I can not get removing to work. I also can't find an example of removing a specific group ID.

Here's the code I have (mainly taken from https://xenforo.com/community/threads/change-user-group-via-php.42233/#post-1016279):

Adding the group
<?php $startTime = microtime(true); $fileDir = '/srv/www/myforum.net/www'; require($fileDir . '/library/XenForo/Autoloader.php'); XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library'); XenForo_Application::initialize($fileDir . '/library', $fileDir); $user_id = 1337; $upgrade_id = "group_upgrade_".date(U); $userModel = XenForo_Model::create('XenForo_Model_User'); $userModel->addUserGroupChange($user_id, $upgrade_id, 22); echo "Added -> ".$upgrade_id; ?>

Removing the group
<?php $startTime = microtime(true); $fileDir = '/srv/www/myforum.net/www'; require($fileDir . '/library/XenForo/Autoloader.php'); XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library'); XenForo_Application::initialize($fileDir . '/library', $fileDir); $user_id = 1337; $downgrade_id = "group_remove_".date(U); $userModel = XenForo_Model::create('XenForo_Model_User'); $userModel->removeUserGroupChange($user_id, $downgrade_id); echo "Removed -> ".$downgrade_id; ?>

I'd appreciate any pointers in the right direction. I had planned to do this in SQL directly but am aware that using the Xenforo API is a far far better way to do it!
 
It's been awhile since playing with 1.5 but the thread you point to has something important you are missing in regards to the bridge; that is, the dependencies. It might help if you separate out the connector part of your code.

Use this code in its own method, and call it, so that you are not repeating it in multiple places.

PHP:
$startTime = microtime(true);
$fileDir = dirname(__FILE__);

require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);

$dependencies = new XenForo_Dependencies_Public();
$dependencies->preLoadData();
 
Also you can write actionDeleteUsergroup function in XenForo_ControllerPublic_Member class.

PHP:
public function actionDeleteUsergroup()
    {
        $userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);
        $userGroupId = $this->_input->filterSingle('usergroup_id', XenForo_Input::UINT);

        $upgrade_id = "group_upgrade_".date('U');

        /** @var XenForo_Model_User $userModel */
        $userModel = XenForo_Model::create('XenForo_Model_User');
        $userModel->addUserGroupChange($userId, $upgrade_id, $userGroupId);
        
        $viewParams = [
            
        ];
        return $this->responseView('XenForo_ViewPublic_Member_DeleteUsergroup', 'member_deleteusergroup', $viewParams);
    }
 
Also you can write actionDeleteUsergroup function in XenForo_ControllerPublic_Member class.

PHP:
public function actionDeleteUsergroup()
    {
        $userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);
        $userGroupId = $this->_input->filterSingle('usergroup_id', XenForo_Input::UINT);

        $upgrade_id = "group_upgrade_".date('U');

        /** @var XenForo_Model_User $userModel */
        $userModel = XenForo_Model::create('XenForo_Model_User');
        $userModel->addUserGroupChange($userId, $upgrade_id, $userGroupId);
       
        $viewParams = [
           
        ];
        return $this->responseView('XenForo_ViewPublic_Member_DeleteUsergroup', 'member_deleteusergroup', $viewParams);
    }

True I could! Although that function is adding a group in a function called delete group :P I get what you mean though. Currently to test it I just have two separate scripts, an add and a remove.

It's been awhile since playing with 1.5 but the thread you point to has something important you are missing in regards to the bridge; that is, the dependencies. It might help if you separate out the connector part of your code.

Use this code in its own method, and call it, so that you are not repeating it in multiple places.

PHP:
$startTime = microtime(true);
$fileDir = dirname(__FILE__);

require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);

$dependencies = new XenForo_Dependencies_Public();
$dependencies->preLoadData();
Tried this, still not working :D

I have a feeling that in one of the pervious topics about this there was talk in that if a user has the group edited by an admin it won't then work pragmatically after.
 
True I could! Although that function is adding a group in a function called delete group :p I get what you mean though. Currently to test it I just have two separate scripts, an add and a remove.


Tried this, still not working :D

I have a feeling that in one of the pervious topics about this there was talk in that if a user has the group edited by an admin it won't then work pragmatically after.
Sorry man, replace on it

PHP:
public function actionDeleteUsergroup()
    {
        $userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);
        $userGroupId = $this->_input->filterSingle('usergroup_id', XenForo_Input::UINT);

       $downgrade_id = "group_remove_".date('U');

        /** @var XenForo_Model_User $userModel */
       $userModel = XenForo_Model::create('XenForo_Model_User');
       $userModel->removeUserGroupChange($userId, $downgrade_id);

      
        $viewParams = [
          
        ];
        return $this->responseView('XenForo_ViewPublic_Member_DeleteUsergroup', 'member_deleteusergroup', $viewParams);
    }

Use: members/123/delete-usergroup/?usergroup_id=7
If you will use it on production, remember about checkingCsrfFromToken(), checking input vars on zero and checking permissions to do it.
 
Sorry man, replace on it


Use: members/123/delete-usergroup/?usergroup_id=7
If you will use it on production, remember about checkingCsrfFromToken(), checking input vars on zero and checking permissions to do it.

Ah! I get what you mean now. However I am planning on this being called from a cron job server side.
 
Back
Top Bottom