/** * Enures the given closur is executed within a PDO transaction. * * @param Closure $callback * @return void */ public function ensureTransaction(Closure $callback) { if (!$this->connection->getPdo()->inTransaction()) { $this->connection->transaction($callback); } else { $callback($this->connection); } }
/** * Request a password reset for a given email. * * @param string $email * * @return bool */ public function requestPasswordResetForEmail($email) { if ($this->reminders === null) { throw new \RuntimeException('Password reset service not set.'); } return $this->db->transaction(function () use($email) { $user = $this->users->findByCredentials(['email' => $email]); if (!$user) { throw new Reminders\ReminderException(); } return $this->reminders->requestReset($user); }); }
/** * Execute a Closure within a transaction. * * @param \Closure $callback * @return mixed * * @throws \Exception */ public function transaction(Closure $callback) { if ($this->getDriverName() == 'sqlsrv') { return parent::transaction($callback); } $this->pdo->exec('BEGIN TRAN'); // We'll simply execute the given callback within a try / catch block // and if we catch any exception we can rollback the transaction // so that none of the changes are persisted to the database. try { $result = $callback($this); $this->pdo->exec('COMMIT TRAN'); } catch (Exception $e) { $this->pdo->exec('ROLLBACK TRAN'); throw $e; } return $result; }
/** * @param Connection $db * @param array $creds * @param string $fromServer * * @return bool */ protected function revokePrivileges($db, $creds, $fromServer) { return $db->transaction(function () use($db, $creds, $fromServer) { // Create users $_users = $this->getDatabaseUsers($creds, $fromServer); try { foreach ($_users as $_user) { // Grants for instance database if (!($_result = $db->statement('REVOKE ALL PRIVILEGES ON ' . $creds['database'] . '.* FROM ' . $_user))) { $this->error('[provisioning:database] error revoking privileges from "' . $_user . '"'); continue; } $this->debug('[provisioning:database] grants revoked - complete'); if (!($_result = $db->statement('DROP USER ' . $_user))) { $this->error('[provisioning:database] error dropping user "' . $_user . '"'); } $_result && $this->debug('[provisioning:database] users dropped > ', $_users); } return true; } catch (\Exception $_ex) { $this->error('[provisioning:database] revoke grants - failure: ' . $_ex->getMessage()); return false; } }); }