]>
gitweb.michael.orlitzky.com - mailshears.git/blob - lib/rm/plugins/postfixadmin.rb
3 require 'common/postfixadmin_plugin'
7 # Handle the removal of users and domains from the Postfixadmin database.
11 include PostfixadminPlugin
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
21 # @param user [User] the user to remove.
24 raise NonexistentUserError
.new(user
.to_s()) if not user_exists(user
)
26 # Remove aliases FROM our user to some other address.
27 sql_queries
= ['DELETE FROM alias WHERE address = $1;']
29 # Also delete aliases that point SOLELY TO our user.
30 sql_queries
<< "DELETE FROM alias WHERE goto = $1;"
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
37 sql_queries
<< "UPDATE alias SET goto=REPLACE(goto, $1, '');"
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;'
44 # Should be handled by a trigger, according to PostfixAdmin code.
45 sql_queries
<< 'DELETE FROM vacation_notification WHERE on_vacation = $1;'
47 connection
= PG
::Connection.new(@db_hash)
50 sql_queries
.each
do |sql_query
|
51 connection
.query(sql_query
, [user
.to_s()])
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
)
61 # Make sure the connection gets closed even if a query explodes.
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.
74 # Log entries (from the "log" table) are not removed since they may
75 # still contain valuable information (although they won't mention
78 # @param domain [Domain] the domain to remove.
80 def remove_domain(domain
)
81 raise NonexistentDomainError
.new(domain
.to_s()) if not domain_exists(domain
)
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
86 users
= list_domains_users([domain
])
87 users
.each
{ |u
| remove_user(u
) }
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;']
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;'
101 connection
= PG
::Connection.new(@db_hash)
104 sql_queries
.each
do |sql_query
|
105 connection
.query(sql_query
, [domain
.to_s()])
108 # Make sure the connection gets closed even if a query explodes.