]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/rm/plugins/postfixadmin.rb
test/mailshears_test.rb: MiniTest -> Minitest
[mailshears.git] / lib / rm / plugins / postfixadmin.rb
1 require 'pg'
2
3 require 'common/postfixadmin_plugin'
4 require 'rm/rm_plugin'
5
6
7 # Handle the removal of users and domains from the Postfixadmin database.
8 #
9 class PostfixadminRm
10
11 include PostfixadminPlugin
12 include RmPlugin
13
14
15 # Remove *user* from the Postfixadmin database. This should remove
16 # him from _every_ table in which he is referenced. Unfortunately,
17 # Postfixadmin does not use foreign keys or ON DELETE CASCADE
18 # triggers so we need to delete the associated child table records
19 # ourselves.
20 #
21 # @param user [User] the user to remove.
22 #
23 def remove_user(user)
24 raise NonexistentUserError.new(user.to_s()) if not user_exists(user)
25
26 # Remove aliases FROM our user to some other address.
27 sql_queries = ['DELETE FROM alias WHERE address = $1;']
28
29 # Also delete aliases that point SOLELY TO our user.
30 sql_queries << "DELETE FROM alias WHERE goto = $1;"
31
32 # But aliases don't need to point to a single user! If our user
33 # was part of a multi-recipient alias, we want to remove our user
34 # from the alias and leave the other recipients.
35 #
36 # We want to delete the comma that precedes/follows the address,
37 # too. Since the address to be replaced can appear at either the
38 # beginning or the end of the list (as well as in the middle), we
39 # have to try to fix both cases: comma before, and comma after.
40 comma_before = "CONCAT(',', $1)"
41 comma_after = "CONCAT($1, ',')"
42 sql_queries << "UPDATE alias SET goto=REPLACE(goto, #{comma_before}, '');"
43 sql_queries << "UPDATE alias SET goto=REPLACE(goto, #{comma_after}, '');"
44
45 sql_queries << 'DELETE FROM mailbox WHERE username = $1;'
46 sql_queries << 'DELETE FROM quota WHERE username = $1;'
47 sql_queries << 'DELETE FROM quota2 WHERE username = $1;'
48 sql_queries << 'DELETE FROM vacation WHERE email = $1;'
49
50 # Should be handled by a trigger, according to PostfixAdmin code.
51 sql_queries << 'DELETE FROM vacation_notification WHERE on_vacation = $1;'
52
53 connection = PG::Connection.new(@db_hash)
54
55 begin
56 sql_queries.each do |sql_query|
57 varchar = 1043 # from pg_type.h
58 params = [{:value => user.to_s(), :type => varchar}]
59 connection.sync_exec_params(sql_query, params)
60 end
61 ensure
62 # Make sure the connection gets closed even if a query explodes.
63 connection.close()
64 end
65 end
66
67
68 # Remove *domain* from the Postfixadmin database. This should remove
69 # the domain from _every_ table in which it is referenced. It should
70 # also remove every user that belongs to the doomed domain
71 # Postfixadmin has some experimental support for triggers, but they
72 # don't do a very good job of cleaning up. Therefore we remove all
73 # users in the domain manually before removing the domain itself.
74 #
75 # Log entries (from the "log" table) are not removed since they may
76 # still contain valuable information (although they won't mention
77 # this removal).
78 #
79 # @param domain [Domain] the domain to remove.
80 #
81 def remove_domain(domain)
82 raise NonexistentDomainError.new(domain.to_s()) if not domain_exists(domain)
83
84 # First remove all users belonging to the domain. This will handle
85 # alias updates and all the sensitive crap we need to do when
86 # removing a user.
87 users = list_domains_users([domain])
88 users.each { |u| remove_user(u) }
89
90 # The domain_admins table contains one record per domain
91 # (repeating the user as necessary), so this really is sufficient.
92 sql_queries = ['DELETE FROM domain_admins WHERE domain = $1;']
93
94 # Some of the following queries should be redundant now that we've
95 # removed all users in the domain.
96 sql_queries << 'DELETE FROM alias WHERE domain = $1;'
97 sql_queries << 'DELETE FROM mailbox WHERE domain = $1;'
98 sql_queries << 'DELETE FROM alias_domain WHERE alias_domain = $1;'
99 sql_queries << 'DELETE FROM vacation WHERE domain = $1;'
100 sql_queries << 'DELETE FROM domain WHERE domain = $1;'
101
102 connection = PG::Connection.new(@db_hash)
103
104 begin
105 sql_queries.each do |sql_query|
106 connection.sync_exec_params(sql_query, [domain.to_s()])
107 end
108 ensure
109 # Make sure the connection gets closed even if a query explodes.
110 connection.close()
111 end
112 end
113
114 end