Implement user moving for Postfixadmin.
[mailshears.git] / lib / mv / plugins / postfixadmin.rb
index 5cc34e33c6f6134d7512581f639f8da59833639f..82ddf055c4b1bd747fb6f1319e344ad8b26c3055 100644 (file)
@@ -8,26 +8,43 @@ class PostfixadminMv
   include PostfixadminPlugin
   include MvPlugin
 
   include PostfixadminPlugin
   include MvPlugin
 
-
   def mv_user(user_from, user_to)
   def mv_user(user_from, user_to)
-    raise NonexistentUserError.new(user_from) if not user_exists(user_from)
+    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]
 
 
     user_to_parts = user_to.split('@')
     localpart_to = user_to_parts[0]
     domain_to = user_to_parts[1]
 
-    sql_queries = ['UPDATE mailbox SET username=$1,
-                                       domain=$2,
-                                       maildir=$2/$3/,
-                                       local_part=$3
-                    WHERE username=$4;']
+    if domain_to.nil?
+      # There was no "@" in the destination user.
+      msg = "the destination user #{user_to} is not valid"
+      raise InvalidUserError.new(msg)
+    end
+
+    if not domain_exists(domain_to)
+      msg = "destination domain #{domain_to} does not exist"
+      raise NonexistentDomainError.new(msg)
+    end
+
+    mailbox_query  = 'UPDATE mailbox SET '
+    mailbox_query += '  username=$1,'
+    mailbox_query += '  domain=$2,'
+    mailbox_query += "  maildir=CONCAT($2, '/', $3, '/'),"
+    mailbox_query += '  local_part=$3 '
+    mailbox_query += 'WHERE username=$4;'
+
+    alias_query1  = 'UPDATE alias SET '
+    alias_query1 += '  address=$1,'
+    alias_query1 += '  domain=$2,'
+    alias_query1 += '  goto=REPLACE(goto, $4, $1) '
+    alias_query1 += 'WHERE address=$4;'
 
 
-    sql_queries << 'UPDATE alias SET address=$1,
-                                     domain=$2,
-                                     goto=REPLACE(goto, $4, $1)
-                    WHERE address=$4;'
+    alias_query2  = 'UPDATE alias SET '
+    alias_query2 += 'goto=REPLACE(goto, $4, $1);'
 
 
-    sql_queries << 'UPDATE alias SET goto=REPLACE(GOTO, $4, $1);'
+    sql_queries = [mailbox_query, alias_query1, alias_query2]
 
     begin
       connection = PGconn.connect(@db_host,
 
     begin
       connection = PGconn.connect(@db_host,
@@ -39,10 +56,12 @@ class PostfixadminMv
                                   @db_pass)
 
       sql_queries.each do |sql_query|
                                   @db_pass)
 
       sql_queries.each do |sql_query|
-        connection.query(sql_query, [user_to,
-                                     domain_to,
-                                     localpart_to,
-                                     user_from])
+          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)
       end
 
       connection.close()
       end
 
       connection.close()
@@ -51,12 +70,7 @@ class PostfixadminMv
       # Pretend like we're database-agnostic in case we ever are.
       raise DatabaseError.new(e)
     end
       # Pretend like we're database-agnostic in case we ever are.
       raise DatabaseError.new(e)
     end
-
   end
 
 
   end
 
 
-  def mv_domain(domain_from, domain_to)
-    raise NotImplementedError
-  end
-
 end
 end