[ABD] Create user-group if missing (updated 09.01.2005)

A place for MOD Authors to post and receive feedback on MODs still in development. No MODs within this forum should be used within a live environment! No new topics are allowed in this forum.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

IMPORTANT: MOD Development Forum rules

On February 1, 2009 this forum will be set to read only as part of retiring of phpBB2.
Locked
User avatar
dkklein
Registered User
Posts: 741
Joined: Mon May 13, 2002 5:52 pm
Location: Copenhagen, Denmark

[ABD] Create user-group if missing (updated 09.01.2005)

Post by dkklein »

I am working on a script that can check if a user is missing the associated personal user group and create it. I would like someome to review the code I have sofar.

Code: Select all

<?php 
//*****  check users and user groups ****// 

define('IN_PHPBB', true); 
$phpbb_root_path = './'; 
include($phpbb_root_path . 'extension.inc'); 
include($phpbb_root_path . 'common.'.$phpEx); 
include($phpbb_root_path . 'includes/functions_search.'.$phpEx); 

// Start session management 
$userdata = session_pagestart($user_ip, PAGE_SEARCH); 
init_userprefs($userdata); 
// End session management 

$sql = "SELECT user_id, username 
    FROM " . USERS_TABLE ."
    WHERE user_id > 0"; 
if ( !($result = $db->sql_query($sql)) ) 
{ 
    message_die(GENERAL_ERROR, 'Could not obtain user list', '', __LINE__, __FILE__, $sql); 
} 

$liste ='';
while ( $row = $db->sql_fetchrow($result) ) 
{ 
	$username = $row['username'];
	$user_id = $row['user_id'];
	$usergroup = '';
	
	$sql1 = "SELECT ug.group_id 
    		FROM " . USER_GROUP_TABLE ." ug, ". GROUPS_TABLE. " g  
    		WHERE ug.user_id = $user_id 
    		  AND ug.group_id = g.group_id
    		  AND g.group_single_user  = 1
    		  ";
    		   
	if ( ($result1 = $db->sql_query($sql1)) ) 
	{ 
	    $row1 = $db->sql_fetchrow($result1);
   	    $usergroup =( ( $row1['group_id'] != '' ) ? $row1['group_id'] : 'User has no user group'.$row1 );
   	    
	} 

   	    if (!($row1['group_id'] != ''))
   	    {
   	    	
			$sql2 = "SELECT MAX(group_id) AS total
				FROM " . GROUPS_TABLE;
			if ( !($result2 = $db->sql_query($sql2)) )
			{
				message_die(GENERAL_ERROR, 'Could not obtain next group_id information', '', __LINE__, __FILE__, $sq2l);
			}

			if ( !($row2 = $db->sql_fetchrow($result2)) )
			{
				message_die(GENERAL_ERROR, 'Could not obtain next group_id information', '', __LINE__, __FILE__, $sql2);
			}
			$group_id = $row2['total'] + 1;

$sql3 = "INSERT INTO " . GROUPS_TABLE . " (group_id, group_name, group_description, group_single_user, group_moderator)
				VALUES ($group_id, '', 'Personal User', 1, 0)";
			if ( !($result3 = $db->sql_query($sql3, BEGIN_TRANSACTION)) )
			{
				message_die(GENERAL_ERROR, 'Could not insert data into groups table', '', __LINE__, __FILE__, $sql3);
			}

			$sql4 = "INSERT INTO " . USER_GROUP_TABLE . " (user_id, group_id, user_pending)
				VALUES ($user_id, $group_id, 0)";
			if( !($result4 = $db->sql_query($sql4, END_TRANSACTION)) )
			{
				message_die(GENERAL_ERROR, 'Could not insert data into user_group table', '', __LINE__, __FILE__, $sql4);
			}

   	    	
   	    	$usergroup = $usergroup.', adding user group '.$group_id;
   	    }


	$liste .= ( ( $liste != '' ) ? '<br> ' : '' ) . $username.' <b>'.$usergroup.'</b>';
} 

message_die(GENERAL_MESSAGE,'Users:<br>'.$liste); 

?> 

EDIT: Changed the code.

EDIT 09.01.2005: For the first time since I wrote this script I got the problem myself. It occurred after I had moved the forum to a new server and had to do a restore of the tables.

See here for a description of the solution.
Last edited by dkklein on Sun Jan 09, 2005 10:38 am, edited 1 time in total.
Best regards Tommy
User avatar
dkklein
Registered User
Posts: 741
Joined: Mon May 13, 2002 5:52 pm
Location: Copenhagen, Denmark

Post by dkklein »

Can a moderator please move this to "MOD Beta Development" forum.
Best regards Tommy
User avatar
dkklein
Registered User
Posts: 741
Joined: Mon May 13, 2002 5:52 pm
Location: Copenhagen, Denmark

Post by dkklein »

Any comments please?
Best regards Tommy
Trollet
Registered User
Posts: 17
Joined: Fri Nov 01, 2002 8:00 pm

Post by Trollet »

hmm.. how do I use this script? I did a fix.php and pasted in all text but it doesn't say anything when I am running it or solve my problem...
Trollet
Registered User
Posts: 17
Joined: Fri Nov 01, 2002 8:00 pm

Post by Trollet »

ghaa.. I didn't put in phpBB2/ wonder why it didn't work before :wink:
rkneeshaw
Registered User
Posts: 20
Joined: Thu Sep 26, 2002 1:49 pm
Contact:

Post by rkneeshaw »

I get this error when trying this script:

Warning: SQL error: [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot insert explicit value for identity column in table 'phpbb_groups' when IDENTITY_INSERT is set to OFF., SQL state 23000 in SQLExecDirect in c:\inetpub\wwwroot\forums\db\mssql-odbc.php on line 154

Something I missed?
User avatar
dkklein
Registered User
Posts: 741
Joined: Mon May 13, 2002 5:52 pm
Location: Copenhagen, Denmark

Post by dkklein »

Probably not but the script is specifically for MySQL. I dont' know how to use the phpBB database layer.
Best regards Tommy
User avatar
smithy_dll
Former Team Member
Posts: 7630
Joined: Tue Jan 08, 2002 6:27 am
Location: Australia
Name: Lachlan Smith
Contact:

Post by smithy_dll »

moved to Mod beta development forum
Systems Engineering
User avatar
santiniuk
Registered User
Posts: 21
Joined: Sat Apr 20, 2002 10:46 am
Contact:

Post by santiniuk »

Many thanks dkklein !

This code corrected a number of problems I had with my groups.

Nice one.....
User avatar
gussie
Registered User
Posts: 92
Joined: Wed Nov 27, 2002 1:26 am

Post by gussie »

Thank you!

Corrected a problem I had with Niel's Delete Inactive Users mod
User avatar
dkklein
Registered User
Posts: 741
Joined: Mon May 13, 2002 5:52 pm
Location: Copenhagen, Denmark

Post by dkklein »

Glad it was usefull.
Best regards Tommy
mztykal
Registered User
Posts: 10
Joined: Mon Aug 05, 2002 10:23 pm

Post by mztykal »

How do you use that script? Thanks!
User avatar
Devoraz
Registered User
Posts: 7
Joined: Tue Feb 05, 2002 9:02 pm
Location: The Netherlands
Contact:

Post by Devoraz »

This code works perfectly. At least it did for me.

How to use it? Do the following...

1) Copy the code from dkklein's message at the top
2) Paste it in your Windows Notepad.
3) Save as FIX.PHP
4) Copy this file into your phpBB/ directory
5) Run it through your browser

It should indicate any users that didn't had a personal group, and were assigned a new one. Your error shouldn't return anymore.

Thanks dkklein for this very useful fix. It really worked! Now all user permissions can be changed again :) :)
Man is just a part of nature, not the other way around.
User avatar
Carpe Diem
Registered User
Posts: 580
Joined: Tue Dec 10, 2002 11:19 pm

YES!!!

Post by Carpe Diem »

MANY MANY MANY thx for this GREAT script!

I had a similar problem, my users somehow didn't have a usergroup (I dunno why but almost 95% of my users didn't have a user group) and I couldn't assign anybody to become a mod.

So I ran this great script and everything fell back to its place and suddenly all my problems were gone!

THANK YOU dkklein! Worked like a charm!

:D
User avatar
dkklein
Registered User
Posts: 741
Joined: Mon May 13, 2002 5:52 pm
Location: Copenhagen, Denmark

Post by dkklein »

I am glad this script was usefull.
Best regards Tommy
Locked

Return to “[2.0.x] MODs in Development”