[vhffs-dev] [svn] commit: r287 - /branches/vhffs_4.1/vhffs-api/src/Vhffs/User.pm

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


Author: beuss
Date: Tue Nov  7 07:32:47 2006
New Revision: 287

Log:
User creation now uses sequences.
Some queries have been rewritten to use placeholders instead of concatenation.

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

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 Tue Nov  7 07:32:47 2006
@@ -240,33 +240,26 @@
     $rows = $request->execute;
     return -1 if ( $rows != 0 );
 
-    #compute the UID, set the minuid if not found 
-    $uid = $conf->{'minuid'};
-    $resultuid = $self->{'db'}->{'DB_READ'}->selectall_arrayref("SELECT MAX(uid) , MAX(gid) FROM vhffs_users");
-    $uid = $resultuid->[0][0] if ( defined $resultuid->[0][0] );
-    $uid++;
-
-    $self->{'uid'} = $uid;
-    $self->{'owner_uid'} = $uid;
-    
-	my $oid = $self->SUPER::create();
-    return -1 if ( $oid lt 0 );
+    # First we create the object corresponding to this user 
+    my $oid = $self->SUPER::create();
+    return -1 if ( $oid lt 0 ); # Creation failed
    
 	$self->{'admin'} = 0 if ( ! defined $self->{'admin'} );
    
-    # get UID and GID for the current user. If gid is not given in parameters, we take the max of gid and create a group with the username
-    
 	$homedir = Vhffs::Functions::hash_homename( $self->{'username'}  , $self->{'main'} );
 
-    $request = $self->{'db'}->{'DB_WRITE'}->prepare("INSERT INTO vhffs_users VALUES('" . $self->{'uid'} ."' , '3' , '$self->{'username'}' , '".$conf->{'shell'}."' , '' , '$homedir' , '".$self->{'admin'}."' , '$oid')");
-
-    $request->execute() or return -2;
-    
-
-    $request = $self->{'db'}->{'DB_WRITE'}->prepare( "INSERT INTO vhffs_user_info VALUES( '". $self->{'uid'} ."' , now() , NULL , NULL , NULL , NULL , NULL, NULL, NULL , NULL)" );
-    $request->execute or return -2;
-
-    
+    $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'} );
@@ -276,10 +269,10 @@
 	$self->{'group'}->commit;
     $self->{'gid'} = $gid;
 
-
-    $request = $self->{'db'}->{'DB_WRITE'}->prepare("UPDATE vhffs_users SET gid = '" . $self->{'gid'} . "' WHERE username='" . $self->{'username'}."'") or return -2;
-    $request->execute or return -3;
-
+    $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;
     
 	return $self->{'uid'};
@@ -344,25 +337,17 @@
     $self->{'gpg_key'} = " " if( ( ! defined $self->{'gpg_key'} ) || ( $self->{'gpg_key'} eq "" ) );
     $self->{'country'} = $self->{'main'}->get_config->get_users->{'default_country'} if ( !defined $self->{'country'} );
     
-    my $v;
-    foreach $v qw(firstname lastname address zipcode city country)
-    {
-	$self->{$v} = quotemeta ( $self->{$v} );
-    }
-    
-    my $request_info = "UPDATE vhffs_user_info SET firstname='".$self->{'firstname'} ."', lastname='".$self->{'lastname'}."', address='".$self->{'address'}."', zipcode='".$self->{'zipcode'}."', country='".$self->{'country'}."', mail='".$self->{'mail'}."', city='".$self->{'city'}."', gpg_key='".$self->{'gpg_key'}."' WHERE uid='".$self->{'uid'}."'";
-    
-#	print $request_info;
-    
-    my $request_unix = "UPDATE vhffs_users SET shell='".$self->{'shell'}."', passwd='".$self->{'passwd'}."', admin=".$self->{'admin'}." WHERE uid=".$self->{'uid'};
-    
-    
     return -1 if( $self->SUPER::commit < 0 );
     
-    my $result_unix = $self->{'db'}->{'DB_WRITE'}->prepare($request_unix) or return -1;
-    $result_unix->execute or return -4;
-    my $result_info = $self->{'db'}->{'DB_WRITE'}->prepare($request_info) or return -2;
-    $result_info->execute or return -3;
+    # Update unix related stuff
+    my $result_unix = $self->{'db'}->{'DB_WRITE'}->prepare('UPDATE vhffs_users SET shell=?, passwd=?, admin=? WHERE uid=?') or return -1;
+    $result_unix->execute($self->{'shell'}, $self->{'passwd'}, $self->{'admin'}, $self->{'uid'}) or return -4;
+
+    # Update user infos
+    my $result_info = $self->{'db'}->{'DB_WRITE'}->prepare('UPDATE vhffs_user_info SET firstname=?, lastname=?, address=?, '.
+	'zipcode=?, country=?, mail=?, city=?, gpg_key=? WHERE uid=?') or return -2;
+    $result_info->execute($self->{'firstname'}, $self->{'lastname'}, $self->{'address'}, $self->{'zipcode'},
+        $self->{'country'}, $self->{'mail'}, $self->{'city'}, $self->{'gpg_key'}, $self->{'uid'}) or return -3;
     
     return 1;
 }




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