c8e44abd2d9140b5a75d969078e31b6eb836aa02
[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. If you're
35 # wondering about the leftover double-commas, look towards the end
36 # of the function.
37 sql_queries << "UPDATE alias SET goto=REPLACE(goto, $1, '');"
38
39 sql_queries << 'DELETE FROM mailbox WHERE username = $1;'
40 sql_queries << 'DELETE FROM quota WHERE username = $1;'
41 sql_queries << 'DELETE FROM quota2 WHERE username = $1;'
42 sql_queries << 'DELETE FROM vacation WHERE email = $1;'
43
44 # Should be handled by a trigger, according to PostfixAdmin code.
45 sql_queries << 'DELETE FROM vacation_notification WHERE on_vacation = $1;'
46
47 connection = PG::Connection.new(@db_hash)
48
49 begin
50 sql_queries.each do |sql_query|
51 connection.query(sql_query, [user.to_s()])
52 end
53
54 # The earlier alias update query will leave things like
55 # "foo@example.com,,bar@example.com" in the "goto" column. Now
56 # we fix it. We don't do it in the loop because query() craps
57 # out on the superfluous parameter.
58 sql_query = "UPDATE alias SET goto=REPLACE(goto, ',,', ',');"
59 connection.query(sql_query)
60 ensure
61 # Make sure the connection gets closed even if a query explodes.
62 connection.close()
63 end
64 end
65
66
67 # Remove *domain* from the Postfixadmin database. This should remove
68 # the domain from _every_ table in which it is referenced. It should
69 # also remove every user that belongs to the doomed domain
70 # Postfixadmin has some experimental support for triggers, but they
71 # don't do a very good job of cleaning up. Therefore we remove all
72 # users in the domain manually before removing the domain itself.
73 #
74 # Log entries (from the "log" table) are not removed since they may
75 # still contain valuable information (although they won't mention
76 # this removal).
77 #
78 # @param domain [Domain] the domain to remove.
79 #
80 def remove_domain(domain)
81 raise NonexistentDomainError.new(domain.to_s()) if not domain_exists(domain)
82
83 # First remove all users belonging to the domain. This will handle
84 # alias updates and all the sensitive crap we need to do when
85 # removing a user.
86 users = list_domains_users([domain])
87 users.each { |u| remove_user(u) }
88
89 # The domain_admins table contains one record per domain
90 # (repeating the user as necessary), so this really is sufficient.
91 sql_queries = ['DELETE FROM domain_admins WHERE domain = $1;']
92
93 # Some of the following queries should be redundant now that we've
94 # removed all users in the domain.
95 sql_queries << 'DELETE FROM alias WHERE domain = $1;'
96 sql_queries << 'DELETE FROM mailbox WHERE domain = $1;'
97 sql_queries << 'DELETE FROM alias_domain WHERE alias_domain = $1;'
98 sql_queries << 'DELETE FROM vacation WHERE domain = $1;'
99 sql_queries << 'DELETE FROM domain WHERE domain = $1;'
100
101 connection = PG::Connection.new(@db_hash)
102
103 begin
104 sql_queries.each do |sql_query|
105 connection.query(sql_query, [domain.to_s()])
106 end
107 ensure
108 # Make sure the connection gets closed even if a query explodes.
109 connection.close()
110 end
111 end
112
113 end