[vhffs-dev] [svn] commit: r291 - in /branches/vhffs_4.1/vhffs-api/src/Vhffs: Object.pm Panel/Confirmation.pm User.pm

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


Author: beuss
Date: Thu Nov  9 21:39:02 2006
New Revision: 291

Log:
Object and user creation now use sequences
Corrected a bug in Object.pm (an history entry was added to an uncreated object causing 6 or 7 log lines for each object creation)
If user creation fails, the transaction is rollbacked.
Suppressed a warning in Confirmation.pm

Modified:
    branches/vhffs_4.1/vhffs-api/src/Vhffs/Object.pm
    branches/vhffs_4.1/vhffs-api/src/Vhffs/Panel/Confirmation.pm
    branches/vhffs_4.1/vhffs-api/src/Vhffs/User.pm

Modified: branches/vhffs_4.1/vhffs-api/src/Vhffs/Object.pm
==============================================================================
--- branches/vhffs_4.1/vhffs-api/src/Vhffs/Object.pm (original)
+++ branches/vhffs_4.1/vhffs-api/src/Vhffs/Object.pm Thu Nov  9 21:39:02 2006
@@ -144,17 +144,17 @@
 
 	$self->{'owner_uid'} = $user->get_uid if( defined $user );
 	$self->{'owner_gid'} = $group->get_gid if( defined $group );
-
-	$self->set_default_state;
-
-    $id = 0;
-    $request = $self->{'db'}->{'DB_READ'}->selectall_arrayref("SELECT MAX(object_id) FROM vhffs_object") or return -2;
-    $id = $request->[0][0] if( defined $request->[0][0] );
-    $id++;
-    $self->{'object_id'} = $id;
-	$request = $self->{'db'}->{'DB_WRITE'}->prepare("INSERT INTO vhffs_object VALUES ( ? , ? , NOW(), ? , ? )");
-
-    $request->execute( $self->{'object_id'} , $self->{'owner_uid'} , $self->{'state'} , $self->{'description'} )  or return -2;
+    
+	# NO! IF YOU DO THAT, YOU ADD AN HISTORY ENTRY FOR A NON EXISTENT OBJECT !!!!
+    #$self->set_default_state;
+    
+
+    $self->{'state'} =( $self->{'main'}->get_config->get_moderation == 1 ? Vhffs::Constants::WAITING_FOR_VALIDATION : Vhffs::Constants::WAITING_FOR_CREATION);
+ 
+	$request = $self->{'db'}->{'DB_WRITE'}->prepare("INSERT INTO vhffs_object(owner_uid, date_creation, state, description) VALUES ( ? , NOW(), ? , ? )");
+
+    $request->execute( $self->{'owner_uid'} , $self->{'state'} , $self->{'description'} )  or return -2;
+    $self->{'object_id'} = $self->{'db'}->{'DB_WRITE'}->last_insert_id(undef, undef, 'vhffs_object', undef);
 
 	$self->add_history( "Object created" );
 
@@ -348,17 +348,11 @@
 	my $message = shift;
 	$message = quotemeta( $message );
 
-	my $id = 0;
-	my $query = "SELECT MAX(history_id) FROM vhffs_history";
-	my $request = $self->{'db'}->{'DB_READ'}->selectall_arrayref( $query ) or return -2;
-	$id = $request->[0][0] if( defined $request->[0][0] );
-	$id++;
-
-	$query = "INSERT INTO vhffs_history VALUES('".$id."','".$self->{'object_id'}."',NOW(),'".$message."')";
+	$query = "INSERT INTO vhffs_history(object_id, date, message)  VALUES(?, NOW(), ?)";
 	$request = $self->{'db'}->{'DB_WRITE'}->prepare( $query );
-	$request->execute or return -2;
-
-	return $id;
+	$request->execute( $self->{'object_id'}, $message ) or return -2;
+
+	return $self->{'db'}->{'DB_WRITE'}->last_insert_id(undef, undef, 'vhffs_history', undef);;
 }
 
 # Returns a hashref wich contains all the history

Modified: branches/vhffs_4.1/vhffs-api/src/Vhffs/Panel/Confirmation.pm
==============================================================================
--- branches/vhffs_4.1/vhffs-api/src/Vhffs/Panel/Confirmation.pm (original)
+++ branches/vhffs_4.1/vhffs-api/src/Vhffs/Panel/Confirmation.pm Thu Nov  9 21:39:02 2006
@@ -84,12 +84,12 @@
 
 	$vhffs		=	shift;
 	$cid		=	shift;
-	$query 		= 	"SELECT code from vhffs_confirmation WHERE cid='".$cid."'";
+	$query 		= 	"SELECT code from vhffs_confirmation WHERE cid=?";
 	$request 	= 	$vhffs->{'db'}->{'DB_READ'}->prepare( $query );
-	return undef if( $request->execute == 0 );
+	return undef if( $request->execute($cid) == 0 );
 
-	$result 	= $request->fetchrow_arrayref;
-	return $result->[0];
+	($result) 	= $request->fetchrow();
+	return $result;
 }
 
 1;

Modified: branches/vhffs_4.1/vhffs-api/src/Vhffs/User.pm
==============================================================================
--- branches/vhffs_4.1/vhffs-api/src/Vhffs/User.pm (original)
+++ branches/vhffs_4.1/vhffs-api/src/Vhffs/User.pm Thu Nov  9 21:39:02 2006
@@ -234,47 +234,57 @@
  
  	$self->{'username'} =~ tr/A-Z/a-z/;
 
-
-    #verify if a user already exists
-    $request = $self->{'db'}->{'DB_WRITE'}->prepare("SELECT * FROM vhffs_users WHERE username='" . $self->{'username'} . "'");
-    $rows = $request->execute;
-    return -1 if ( $rows != 0 );
-
-    # First we create the object corresponding to this user 
-    my $oid = $self->SUPER::create();
-    return -1 if ( $oid lt 0 ); # Creation failed
+    $self->{'db'}->{'DB_WRITE'}->begin_work;
+    # Localize RaiseError so it get restored after we finish
+    # With this enabled, DBI automagically call die if a
+    # query goes wrong.
+    local $self->{'db'}->{'DB_WRITE'}->{'RaiseError'} = 1;
+    
+    eval {
+
+        # First we create the object corresponding to this user 
+        my $oid = $self->SUPER::create();
+        die if ( $oid lt 0 ); # Creation failed
    
-	$self->{'admin'} = 0 if ( ! defined $self->{'admin'} );
+    	$self->{'admin'} = 0 if ( ! defined $self->{'admin'} );
    
-	$homedir = Vhffs::Functions::hash_homename( $self->{'username'}  , $self->{'main'} );
-
-    $request = $self->{'db'}->{'DB_WRITE'}->prepare('INSERT INTO vhffs_users (gid, username, shell, passwd, homedir, admin, object_id) VALUES(?, ?, ?, ?, ?, ?, ?)');
-
-    $request->execute(3 , $self->{'username'}, $conf->{'shell'}, '' , $homedir, $self->{'admin'}, $oid) or return -2;
-    
-    $self->{'uid'} = $self->{'db'}->{'DB_WRITE'}->last_insert_id(undef, undef, 'vhffs_users', undef);
-    $self->SUPER::set_owner($self->{'uid'});
-    $self->SUPER::commit();
-
-    # FIXME This two step creation is *ugly* we should consider using a kind
-    # of factory for the whole user creation.
-    $request = $self->{'db'}->{'DB_WRITE'}->prepare( "INSERT INTO vhffs_user_info(uid, date_creation) VALUES(?, now())" );
-    $request->execute($self->{'uid'}) or return -2;
-    
-    #Now, create the GID
-    $self->{'group'} = new Vhffs::Group( $self->{'main'} , $self->{'username'} , $self->{'uid'} );
-    $gid = $self->{'group'}->create;
-    return -1 if ( $gid < 0 );
-	$self->{'group'}->set_status( Vhffs::Constants::ACTIVATED );
-	$self->{'group'}->commit;
-    $self->{'gid'} = $gid;
-
-    $request = $self->{'db'}->{'DB_WRITE'}->prepare('UPDATE vhffs_users SET gid = ? WHERE username=?') or return -2;
-    $request->execute($self->{'gid'}, $self->{'username'}) or ($self->{'db'}->rollback(), return -3);
-
-    # Fill vhffs_user_info
-	$self->commit;
-    
+	    $homedir = Vhffs::Functions::hash_homename( $self->{'username'}  , $self->{'main'} );
+
+        $request = $self->{'db'}->{'DB_WRITE'}->prepare('INSERT INTO vhffs_users (gid, username, shell, passwd, homedir, admin, object_id) VALUES(?, ?, ?, ?, ?, ?, ?)');
+
+        $request->execute(3 , $self->{'username'}, $conf->{'shell'}, '' , $homedir, $self->{'admin'}, $oid);
+    
+        $self->{'uid'} = $self->{'db'}->{'DB_WRITE'}->last_insert_id(undef, undef, 'vhffs_users', undef);
+        $self->SUPER::set_owner($self->{'uid'});
+        $self->SUPER::commit();
+
+        # FIXME This two step creation is *ugly* we should consider using a kind
+        # of factory for the whole user creation.
+        $request = $self->{'db'}->{'DB_WRITE'}->prepare( "INSERT INTO vhffs_user_info(uid, date_creation) VALUES(?, now())" );
+        $request->execute($self->{'uid'});
+    
+        #Now, create the GID
+        $self->{'group'} = new Vhffs::Group( $self->{'main'} , $self->{'username'} , $self->{'uid'} );
+        $gid = $self->{'group'}->create;
+        die if ( $gid < 0 );
+	    $self->{'group'}->set_status( Vhffs::Constants::ACTIVATED );
+    	$self->{'group'}->commit;
+        $self->{'gid'} = $gid;
+
+        $request = $self->{'db'}->{'DB_WRITE'}->prepare('UPDATE vhffs_users SET gid = ? WHERE username=?');
+        $request->execute($self->{'gid'}, $self->{'username'});
+
+        # Fill vhffs_user_info
+    	$self->commit;
+        # If we are here all was fine so make changes permanent
+        $self->{'db'}->{'DB_WRITE'}->commit;
+    };
+    
+    if($@) {
+        # An error occured => cancel transaction
+        $self->{'db'}->{'DB_WRITE'}->rollback;
+        return -1;
+    }
 	return $self->{'uid'};
 }
 




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