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

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


Author: beuss
Date: Sat Nov 11 14:20:06 2006
New Revision: 295

Log:
ACLs now use sequences
done some cleanup in sql

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

Modified: branches/vhffs_4.1/vhffs-api/src/Vhffs/Acl.pm
==============================================================================
--- branches/vhffs_4.1/vhffs-api/src/Vhffs/Acl.pm (original)
+++ branches/vhffs_4.1/vhffs-api/src/Vhffs/Acl.pm Sat Nov 11 14:20:06 2006
@@ -59,26 +59,28 @@
 
 	$perm = Vhffs::Constants::ACL_DENIED;
 	
-	$query = "SELECT perm FROM vhffs_acl WHERE oid_src='".$entity->{'object_id'}."' AND oid_dst='".$object->{'object_id'} ."'";
-	$request = $main->{'db'}->{'DB_READ'}->prepare( $query );
-	$rows = $request->execute;
+    $query = 'SELECT perm FROM vhffs_acl WHERE oid_src=? AND oid_dst=?';
+	$request = $main->{'db'}->{'DB_READ'}->prepare( $query );
+	$rows = $request->execute($entity->{'object_id'}, $object->{'object_id'});
 
 	if( $rows != 0 )
 	{
-		$result = $main->{'db'}->{'DB_READ'}->selectall_arrayref( $query );
-		$perm = $result->[0][0];
+        ($perm) = $request->fetchrow();
 	}
 	else
 	{
-		$query = "SELECT acl.perm FROM vhffs_acl acl, vhffs_groups groups, vhffs_user_group ug WHERE acl.oid_dst='".$object->{'object_id'} ."' AND ug.uid='".$entity->get_uid."' AND ug.gid=groups.gid AND groups.object_id=acl.oid_src";
+        $query = 'SELECT acl.perm FROM vhffs_acl acl 
+            INNER JOIN vhffs_groups g ON g.object_id = acl.oid_src 
+            INNER JOIN vhffs_user_group ug ON ug.gid = g.gid
+            WHERE acl.oid_dst = ? AND ug.uid = ?';
 		$request = $main->{'db'}->{'DB_READ'}->prepare( $query );
-		$rows = $request->execute;
+		$rows = $request->execute( $object->{'object_id'}, $entity->get_uid );
 
 		if( $rows != 0 )
 		{
-			while( my @lol = $request->fetchrow_array )
+			while( my ($lol) = $request->fetchrow_array )
 			{
-				$perm = $lol[0] if( $perm < $lol[0] );
+				$perm = $lol if( $perm < $lol );
 			}
 		}
 	}
@@ -101,15 +103,15 @@
 
 	return -1 if( ( $entity->fetch < 0 ) || ( $object->fetch < 0 ) );
 	
-	$query = "SELECT perm FROM vhffs_acl WHERE oid_src='".$entity->{'object_id'}."' AND oid_dst='".$object->{'object_id'} ."'";
-
-	$request = $main->{'db'}->{'DB_READ'}->prepare( $query );
-	$rows = $request->execute;
+	$query = 'SELECT perm FROM vhffs_acl WHERE oid_src = ? AND oid_dst = ?';
+
+	$request = $main->{'db'}->{'DB_READ'}->prepare( $query );
+	$rows = $request->execute( $entity->{'object_id'}, $object->{'object_id'} );
 
 	if ( $rows != 0 )
 	{
-		$result = $main->{'db'}->{'DB_READ'}->selectall_arrayref( $query );
-		return $result->[0][0];
+        ($result) = $request->fetchrow();
+		return $result;
 	}
 	else
 	{
@@ -125,31 +127,20 @@
 
 	return -1 if( ( $entity->fetch < 0 ) || ( $object->fetch < 0 ) );
 
-	my $query = "SELECT perm FROM vhffs_acl WHERE oid_src='".$entity->{'object_id'}."' AND oid_dst='".$object->{'object_id'} ."'";
-
-	my $request = $main->{'db'}->{'DB_READ'}->prepare( $query );
-	my $rows = $request->execute;
-
-	#If the ACL already exists, we just update it
-	if ( $rows > 0 )
-	{
-		$query = "UPDATE vhffs_acl SET perm='".$perm."' WHERE oid_src='".$entity->{'object_id'}."' AND oid_dst='".$object->{'object_id'} ."'";
-	}	
-	else
-	{
-		#If not, we create a new ACL in the database
-		my $acl_id = 1;
-		$query = "SELECT MAX(acl_id) FROM vhffs_acl";
-		my $result = $main->{'db'}->{'DB_READ'}->selectall_arrayref( $query );
-		$acl_id = $result->[0][0] if ( defined $result->[0][0] );
-		$acl_id++;
-
-		$query = "INSERT INTO vhffs_acl VALUES('$acl_id' ,'".$entity->{'object_id'}."','$perm','".$object->{'object_id'}."')";
-	}
-	$request = $main->{'db'}->{'DB_WRITE'}->prepare( $query );
-	$request->execute;
-
-	return 1;
+    my $query = 'UPDATE vhffs_acl SET perm = ? WHERE oid_src = ? AND oid_dst = ?';
+    my $request = $main->{'db'}->{'DB_WRITE'}->prepare( $query );
+    my $result = $request->execute($perm, $entity->{'object_id'}, $object->{'object_id'}) or return -1;
+    if($result && $result == 0) {
+        # If update fails for a connection error, $result would be false
+        # If no line are updated, $result is true but $result == 0 is true
+        # too ($result contains "0E0")
+        # No line updated, INSERT ACL
+        $query = 'INSERT INTO vhffs_acl(oid_src, perm, oid_dst) VALUES(?, ?, ?)';
+        $request = $main->{'db'}->{'DB_WRITE'}->prepare($query);
+        $request->execute($entity->{'object_id'}, $perm, $object->{'object_id'}) or return -1;
+        
+    }
+    return 1;
 }
 
 




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