]> gitweb.michael.orlitzky.com - mailshears.git/blobdiff - lib/mv/plugins/postfixadmin.rb
lib,test: replace connection query() method with sync_exec{,_params}.
[mailshears.git] / lib / mv / plugins / postfixadmin.rb
index 82ddf055c4b1bd747fb6f1319e344ad8b26c3055..1623118109a9033fd9e01bd75ee5ba737dfcfa8f 100644 (file)
@@ -3,30 +3,35 @@ require 'pg'
 require 'common/postfixadmin_plugin'
 require 'mv/mv_plugin'
 
 require 'common/postfixadmin_plugin'
 require 'mv/mv_plugin'
 
+
+# Handle moving (renaming) of users in the Postfixadmin database.
+#
 class PostfixadminMv
 
   include PostfixadminPlugin
   include MvPlugin
 
 class PostfixadminMv
 
   include PostfixadminPlugin
   include MvPlugin
 
-  def mv_user(user_from, user_to)
-    if not user_exists(user_from)
-      raise NonexistentUserError.new("source user #{user_from} does not exist")
-    end
-
-    user_to_parts = user_to.split('@')
-    localpart_to = user_to_parts[0]
-    domain_to = user_to_parts[1]
 
 
-    if domain_to.nil?
-      # There was no "@" in the destination user.
-      msg = "the destination user #{user_to} is not valid"
-      raise InvalidUserError.new(msg)
+  # Move the user *src* to *dst* within the Postfixadmin
+  # database. This should "rename" him in _every_ table where he is
+  # referenced. Unfortunately that must be done manually.
+  #
+  # This can fail is *src* does not exist, or if *dst* already exists
+  # before the move. It will also fail if the domain associated with
+  # the user *dst* does not exist.
+  #
+  # @param src [User] the source user to be moved.
+  #
+  # @param dst [User] the destination user being moved to.
+  #
+  def mv_user(src, dst)
+    raise NonexistentUserError.new(src.to_s()) if not user_exists(src)
+
+    if not domain_exists(dst.domain())
+      raise NonexistentDomainError.new(dst.domain.to_s())
     end
 
     end
 
-    if not domain_exists(domain_to)
-      msg = "destination domain #{domain_to} does not exist"
-      raise NonexistentDomainError.new(msg)
-    end
+    raise UserAlreadyExistsError.new(dst.to_s()) if user_exists(dst)
 
     mailbox_query  = 'UPDATE mailbox SET '
     mailbox_query += '  username=$1,'
 
     mailbox_query  = 'UPDATE mailbox SET '
     mailbox_query += '  username=$1,'
@@ -46,31 +51,20 @@ class PostfixadminMv
 
     sql_queries = [mailbox_query, alias_query1, alias_query2]
 
 
     sql_queries = [mailbox_query, alias_query1, alias_query2]
 
+    connection = PG::Connection.new(@db_hash)
     begin
     begin
-      connection = PGconn.connect(@db_host,
-                                  @db_port,
-                                  @db_opts,
-                                  @db_tty,
-                                  @db_name,
-                                  @db_user,
-                                  @db_pass)
-
       sql_queries.each do |sql_query|
       sql_queries.each do |sql_query|
-          varchar = 1043 # from pg_type.h
-          params = [{:value => user_to, :type => varchar},
-                    {:value => domain_to, :type => varchar},
-                    {:value => localpart_to, :type => varchar},
-                    {:value => user_from, :type => varchar}]
-        connection.query(sql_query, params)
+        varchar = 1043 # from pg_type.h
+        params = [{:value => dst.to_s(), :type => varchar},
+                  {:value => dst.domainpart(), :type => varchar},
+                  {:value => dst.localpart(), :type => varchar},
+                  {:value => src.to_s(), :type => varchar}]
+        connection.sync_exec_params(sql_query, params)
       end
       end
-
+    ensure
+      # Make sure the connection gets closed even if a query explodes.
       connection.close()
       connection.close()
-
-    rescue PGError => e
-      # Pretend like we're database-agnostic in case we ever are.
-      raise DatabaseError.new(e)
     end
   end
 
     end
   end
 
-
 end
 end