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

XF 1.1 User group change (secondary_groups) via SQL

#1
Hi,

I want to know what needs to be done to be able to add/remove a secondary_group to a user via SQL. I need it because we are using the XenForo forum to sync the users to our minecraft server. And we do not want to use callback to run a .php file on the webserver because of security and stability.

Thanks
 

Jake Bunce

XenForo moderator
Staff member
#2
This:

http://xenforo.com/community/thread...-nodes-updating-usergroups.28811/#post-333967

Rebuilding the cache is necessary to update the permissions of the user after changing their group with a query.

However, you want to query the secondary groups which isn't so easy because the secondary groups are stored as a comma list. You would have to query the list and manipulate it using PHP code (explode on the comma, manage the array elements, implode using a comma, then set that value in the user record). Then you still need to rebuild the cache afterwards.
 
#3
This:

http://xenforo.com/community/thread...-nodes-updating-usergroups.28811/#post-333967

Rebuilding the cache is necessary to update the permissions of the user after changing their group with a query.

However, you want to query the secondary groups which isn't so easy because the secondary groups are stored as a comma list. You would have to query the list and manipulate it using PHP code (explode on the comma, manage the array elements, implode using a comma, then set that value in the user record). Then you still need to rebuild the cache afterwards.
Okay as I suspected then thank you again for the quick and perfect answer Jake!
 
#5
Hi! Did you find a solution?
Hey,

Well we couldn't find a good solution without having to call a .php script from the website so we made our own plugin to do just that. And then we just made a php file to update the users using $_GET with a key.

I'll post our PHP code here if you want to use or get the basic idea, I'm presuming that you know all the necessary stuff about XenForo's system.

PHP:
<?php
require_once('../../forum/library/XenForo/Autoloader.php');
include('../functions.php');
XenForo_Autoloader::getInstance()->setupAutoloader('../../forum/library');
XenForo_Application::initialize('../../forum/library', '../../forum/');
XenForo_Application::set('page_start_time', time());
XenForo_Application::disablePhpErrorHandler();
XenForo_Application::setDebugMode(false);

$allowed_ip = "1.1.1.1.1"; //Make sure only the server can access this file

if($_SERVER['REMOTE_ADDR'] == $allowed_ip){

$key = "somebigrandomkey";
$groups = array(3,4,6,36); //The Secondary Groups you want to use
$ranks = array(7,38,39,40); //The forum ranks you want to use

    if(isset($_GET['key']) == $key && $_GET['username'] && $_GET['rank']){
        $userName = $_GET['username'];
        $userRank = $_GET['rank'];

        $mu = XenForo_Model::create('XenForo_Model_User');
        $user = $mu->getUserByName($userName);
        $changeRank = $session->promote_user($userName, $userRank);

        if($mu->getUserByName($userName) != false && is_numeric($userRank) == true){
            if(!in_array($user['secondary_group_ids'], $groups)){
              if($user['secondary_group_ids'] != $userRank){
                if($changeRank == 1){
                    echo 0;
                }else{
                    echo 1;
                }
              }else{
                echo 2;
              }
            }else{
                echo 3;
            }
        }else{
            echo 4;
        }
    }
}else{
    //header('Location: http://www.google.com');
}


?>