Save profile within a SQL transaction in order to avoid inconsistent
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Mon, 11 Oct 2010 14:38:40 +0000 (16:38 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Mon, 11 Oct 2010 14:38:40 +0000 (16:38 +0200)
states if a SQL error occurs while saving the changes.

This change should not have any consequence if the site behave normally
but may improve robustness in case of code errors.

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
modules/profile/page.inc.php

index d8c89de..aad3b96 100644 (file)
@@ -262,7 +262,7 @@ abstract class ProfilePage implements PlWizardPage
     {
     }
 
-    protected function saveData()
+    public function saveData()
     {
         require_once 'notifs.inc.php';
         $changedFields = array();
@@ -309,6 +309,7 @@ abstract class ProfilePage implements PlWizardPage
                              $this->pid(), $user->id(), $field, $values[0], $values[1]);
             }
         }
+        return true;
     }
 
     protected function checkChanges()
@@ -384,7 +385,13 @@ abstract class ProfilePage implements PlWizardPage
         }
         if ($global_success) {
             if ($this->checkChanges()) {
-                $this->saveData();
+                /* Save changes atomically to avoid inconsistent state
+                 * in case of error.
+                 */
+                if (!XDB::runTransaction(array($this, 'saveData'))) {
+                    $global_success = false;
+                    return PlWizard::CURRENT_PAGE;
+                }
                 $this->markChange();
             }
             return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;