4d8196461ca570a8e51a5aa0880365dda8d950f5
[mailshears.git] / lib / mv / plugins / roundcube.rb
1 require 'pg'
2
3 require 'common/roundcube_plugin'
4 require 'mv/mv_plugin'
5
6 # Handle moving (renaming) of users in the Roundcube
7 # database. Roundcube has no concept of domains.
8 #
9 class RoundcubeMv
10
11 include RoundcubePlugin
12 include MvPlugin
13
14
15 # Move the user *src* to *dst* within the Roundcube database. This
16 # should "rename" him in _every_ table where he is referenced.
17 # Roundcube uses foreign keys properly, so we let the ON UPDATE
18 # CASCADE trigger handle most of the work.
19 #
20 # This can fail is *src* does not exist, or if *dst* already exists
21 # before the move. It should also be an error if the destination
22 # domain doesn't exist. But Roundcube doesn't know about domains, so
23 # we let that slide.
24 #
25 # @param src [User] the source user to be moved.
26 #
27 # @param dst [User] the destination user being moved to.
28 #
29 def mv_user(src, dst)
30 raise NonexistentUserError.new(src.to_s()) if not user_exists(src)
31 raise UserAlreadyExistsError.new(dst.to_s()) if user_exists(dst)
32
33 sql_queries = ['UPDATE users SET username = $1 WHERE username = $2;']
34
35 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
36 @db_name, @db_user, @db_pass)
37
38 sql_queries.each do |sql_query|
39 connection.query(sql_query, [dst.to_s(), src.to_s()])
40 end
41
42 connection.close()
43 end
44
45 end