[vhffs-dev] [586] Fixed a bug which caused bots to add only one user at a time for a given group (thanks to crafty).

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 586
Author:   beuss
Date:     2007-04-21 09:30:00 +0000 (Sat, 21 Apr 2007)

Log Message:
-----------
Fixed a bug which caused bots to add only one user at a time for a given group (thanks to crafty).

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Robots/Group.pm
    trunk/vhffs-robots/src/user_group.pl


Modified: trunk/vhffs-api/src/Vhffs/Robots/Group.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Group.pm	2007-04-21 07:43:54 UTC (rev 585)
+++ trunk/vhffs-api/src/Vhffs/Robots/Group.pm	2007-04-21 09:30:00 UTC (rev 586)
@@ -92,29 +92,51 @@
 }
 
 
+=pod
 
+=head2 getall_user_group_to_delete
 
+    my $quits = Vhffs::Robots::Group::getall_user_group_to_delete($main);
+
+Returns an array ref containing all users to delete from a group. Each rows is
+itself an array ref wich contains group id from which user should be deleted
+in [0] and uid in [1].
+
+=cut
+
 sub getall_user_group_to_delete
 {
 	my $main = shift;	
-	my $query = "SELECT * FROM vhffs_user_group WHERE state='".Vhffs::Constants::TO_DELETE."'";
+	my $query = 'SELECT gid, uid FROM vhffs_user_group WHERE state = '.Vhffs::Constants::TO_DELETE;
 
-	$request = $main->{'db'}->prepare( $query ) or return -2;
-	return undef if ( $request->execute() <= 0 );
+	$request = $main->get_db_object()->prepare( $query ) or return -1;
+	return -1 unless( $request->execute() );
 
-	return( $request->fetchall_hashref('gid') );
+	return( $request->fetchall_arrayref() );
 }
 
 
+=pod
+
+=head2 getall_user_group_to_delete
+
+    my $joins = Vhffs::Robots::Group::getall_user_group_to_create($main);
+
+Returns an array ref containing all users to add to a group. Each rows is
+itself an array ref wich contains group id to which user should be added
+in [0] and uid in [1].
+
+=cut
+
 sub getall_user_group_to_create
 {
 	my $main = shift;	
-	my $query = "SELECT * FROM vhffs_user_group WHERE state='".Vhffs::Constants::WAITING_FOR_CREATION."'";
+	my $query = 'SELECT gid, uid FROM vhffs_user_group WHERE state='.Vhffs::Constants::WAITING_FOR_CREATION;
 
-	$request = $main->{'db'}->prepare( $query ) or return -2;
-	return undef if ( $request->execute() <= 0 );
+	$request = $main->{'db'}->prepare( $query ) or return -1;
+	return -1 unless ( $request->execute() );
 
-	return( $request->fetchall_hashref('gid') );
+	return( $request->fetchall_arrayref() );
 }
 
 sub del_user

Modified: trunk/vhffs-robots/src/user_group.pl
===================================================================
--- trunk/vhffs-robots/src/user_group.pl	2007-04-21 07:43:54 UTC (rev 585)
+++ trunk/vhffs-robots/src/user_group.pl	2007-04-21 09:30:00 UTC (rev 586)
@@ -47,73 +47,55 @@
 
 my $joins = Vhffs::Robots::Group::getall_user_group_to_create( $vhffs );
 
-if( defined $joins )
-{
-foreach( keys %{$joins} )
-{
-	$group = Vhffs::Group::get_by_gid( $vhffs , $_ );
-	if( defined $group )
-	{
-        $user = Vhffs::User::get_by_uid($vhffs, $joins->{$_}{'uid'} );
-
-		if( defined $user )
-		{
-			if( Vhffs::Robots::Group::add_user( $user , $group , $vhffs ) < 0 )
-			{
-				Vhffs::Robots::vhffs_log( sprintf( "Error adding user %s to group %s" , $user->get_username , $group->get_groupname ), $vhffs);
-			}
-			else
-			{
-				Vhffs::Robots::vhffs_log( sprintf( "Add user %s to group %s" , $user->get_username , $group->get_groupname ), $vhffs);
-			}
-			
-		}
-		else
-		{
-			Vhffs::Robots::vhffs_log( sprintf( "Cannot get user %s" , $_ ), $vhffs);
-		}
-	}
-	else
-	{
-		Vhffs::Robots::vhffs_log( sprintf( "Cannot get group %s" , $_ ), $vhffs);
-	}
+if(!ref($joins)) {
+    Vhffs::Robots::vhffs_log( 'Database error while trying to add new users to groups' );
+} else {
+    foreach(@{$joins}) {
+        $group = Vhffs::Group::get_by_gid( $vhffs , $_->[0] );
+        if( defined $group ) {
+            $user = Vhffs::User::get_by_uid($vhffs, $_->[1] );
+    
+            if( defined $user ) {
+                if( Vhffs::Robots::Group::add_user( $user , $group , $vhffs ) < 0 ) {
+                    Vhffs::Robots::vhffs_log( sprintf( "Error while adding user %s to group %s" , $user->get_username , $group->get_groupname ), $vhffs);
+                } else {
+                    Vhffs::Robots::vhffs_log( sprintf( "Added user %s to group %s" , $user->get_username , $group->get_groupname ), $vhffs);
+                }			
+            } else {
+                Vhffs::Robots::vhffs_log( sprintf( "Cannot get user %s" , $_->[1] ), $vhffs);
+            }
+        } else {
+            Vhffs::Robots::vhffs_log( sprintf( "Cannot get group %s" , $_->[0] ), $vhffs);
+        }
+    }
 }
-}
 
 
 
-$joins = Vhffs::Robots::Group::getall_user_group_to_delete( $vhffs );
+my $quits = Vhffs::Robots::Group::getall_user_group_to_delete( $vhffs );
 
-if( ! defined $joins )
-{
-	Vhffs::Robots::unlock( $vhffs );
-	exit( 1 );
+if(!ref($quits)) {
+    Vhffs::Robots::vhffs_log( 'Database error while trying to delete users from groups' );
+} else {
+    foreach(@{$quits}) {
+        $group = Vhffs::Group::get_by_gid( $vhffs , $_->[0] );
+        if( defined $group ) {
+            $user = Vhffs::User::get_by_uid( $vhffs , $_->[1] );
+            if( defined $user ) {
+                if( Vhffs::Robots::Group::del_user( $user , $group , $vhffs ) > 0 ) {
+                    Vhffs::Robots::vhffs_log( sprintf( 'Removed user %s from group %s', $user->get_username, $group->get_groupname ), $vhffs);
+                } else {
+                    Vhffs::Robots::vhffs_log( sprintf( 'Error while removing user %s from group %s', $user->get_username, $group->get_groupname ), $vhffs);
+                }
+            } else {
+                Vhffs::Robots::vhffs_log( sprintf( 'Cannot get user %d', $_->[1] ), $vhffs);
+            }
+        } else {
+            Vhffs::Robots::vhffs_log( sprintf( 'Cannot get group %d' , $->[0] ), $vhffs);
+        }
+    }
 }
 
-foreach( keys %{$joins} )
-{
-	$group = Vhffs::Group::get_by_gid( $vhffs , $_ );
-	if( defined $group )
-	{
-		$user = Vhffs::User::get_by_uid( $vhffs , $joins->{$_}{'uid'} );
-
-		if( defined $user )
-		{
-			Vhffs::Robots::vhffs_log( sprintf( "Remove user %s from group %s" , $user->get_username , $group->get_groupname ), $vhffs);
-			Vhffs::Robots::Group::del_user( $user , $group , $vhffs );
-			
-		}
-		else
-		{
-			Vhffs::Robots::vhffs_log( sprintf( "Cannot get user %s" , $name ), $vhffs);
-		}
-	}
-	else
-	{
-		Vhffs::Robots::vhffs_log( sprintf( "Cannot get group %s" , $_ ), $vhffs);
-	}
-}
-
 Vhffs::Robots::unlock( $vhffs );
 
 exit 0;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/