Create user and add to group


(Marcos Conceição) #1

All users are created via sso.

How can i add this users to a group after user created via sso?

My script looks like:

`

$api = new DiscourseAPI(XXXXXXX, XXXXXXX, XXXXX);
$sso_secret = 'XXXXXXXX';
$nonce = base64_decode($_GET['sso']);
if (hash_hmac('sha256', $_GET['sso'], $sso_secret) != $_GET['sig']){
    header("Location: https://www.mysite.com");
    die();
}elseif(validaUsuario()){
    $avatar = "";

$payload = base64_encode($nonce. "&email=" . $_SESSION['email'] . $avatar . "&external_id=" . $_SESSION['user_id'] . "&name=" . $_SESSION['name'] . "");
$return_sig = hash_hmac('sha256', $payload, $sso_secret);

//Liberando acesso aos grupos
$COD_USUARIO = $_SESSION['user_id'];
$buscarusuario = $api->getMemberByExternal($COD_USUARIO);
if($buscarusuario->http_code == 200){
	$username = $buscarusuario->apiresult->user->username;
	
	//Consultamos os cursos adquiridos pelo usuario, que ainda nao foram liberados no forum
	$sql = "SELECT COD_ITEM, COD_CURSO 
	        FROM XXXXXX 
	        WHERE 
	            COD_USUARIO = '".$COD_USUARIO."' 
	            AND PROC_DISCOURSE_ITEM IS NULL";
	$query = mysql_query($sql);
	
	//Para cada curso pendente por liberar, pesquisamos o grupo referente
	while($linha = mysql_fetch_array($query)){
	    $groupname = retornaGrupoDiscourse($linha['COD_CURSO']);
	    //Caso o grupo exista, inserimos o usuario
	    if($groupname){
	        $execute = $api->joinGroup($groupname, $username);
	        
	        //se tudo ocorrer normalmente, atualizamos o PROC_DISCOURSE_ITEM
	        if($execute->http_code == 200 OR $execute->http_code == 422){
	            $nsql = "UPDATE XXXXXXXXX SET PROC_DISCOURSE_ITEM='OK' WHERE COD_ITEM = '".$linha['COD_ITEM']."' LIMIT 1";
	            $run = mysql_query($nsql);
	        }
	    }
	   
	}
}

header("Location: http://forum.mysite.com/session/sso_login?sso=". $payload ."&sig=". $return_sig);
}else{
header("Location: https://www.mysite.com");
die();
}
?>

`

If a user login for the first time, script will create his access to the forum, but will not give access to the group.
If the user login again, the script will grant the access to the groups.

What can i do to give access to the groups after create the user?