Fix SQL query clobbering in PostfixadminMv.
[mailshears.git] / lib / mv / plugins / postfixadmin.rb
1 require 'pg'
2
3 require 'common/postfixadmin_plugin'
4 require 'mv/mv_plugin'
5
6 class PostfixadminMv
7
8 include PostfixadminPlugin
9 include MvPlugin
10
11
12 def mv_user(user_from, user_to)
13 raise NonexistentUserError.new(user_from) if not user_exists(user_from)
14
15 user_to_parts = user_to.split('@')
16 localpart_to = user_to_parts[0]
17 domain_to = user_to_parts[1]
18
19 sql_queries = ['UPDATE mailbox SET username=$1,
20 domain=$2,
21 maildir=$2/$3/,
22 local_part=$3
23 WHERE username=$4;']
24
25 sql_queries << 'UPDATE alias SET address=$1,
26 domain=$2,
27 goto=REPLACE(goto, $4, $1)
28 WHERE address=$4;'
29
30 sql_queries << 'UPDATE alias SET goto=REPLACE(GOTO, $4, $1);'
31
32 begin
33 connection = PGconn.connect(@db_host,
34 @db_port,
35 @db_opts,
36 @db_tty,
37 @db_name,
38 @db_user,
39 @db_pass)
40
41 sql_queries.each do |sql_query|
42 connection.query(sql_query, [user_to,
43 domain_to,
44 localpart_to,
45 user_from])
46 end
47
48 connection.close()
49
50 rescue PGError => e
51 # Pretend like we're database-agnostic in case we ever are.
52 raise DatabaseError.new(e)
53 end
54
55 end
56
57
58 def mv_domain(domain_from, domain_to)
59 raise NotImplementedError
60 end
61
62 end