]>
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.
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}, '');"
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;'
50 # Should be handled by a trigger, according to PostfixAdmin code.
51 sql_queries
<< 'DELETE FROM vacation_notification WHERE on_vacation = $1;'
53 connection
= PG
::Connection.new(@db_hash)
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
)
62 # Make sure the connection gets closed even if a query explodes.
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.
75 # Log entries (from the "log" table) are not removed since they may
76 # still contain valuable information (although they won't mention
79 # @param domain [Domain] the domain to remove.
81 def remove_domain(domain
)
82 raise NonexistentDomainError
.new(domain
.to_s()) if not domain_exists(domain
)
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
87 users
= list_domains_users([domain
])
88 users
.each
{ |u
| remove_user(u
) }
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;']
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;'
102 connection
= PG
::Connection.new(@db_hash)
105 sql_queries
.each
do |sql_query
|
106 connection
.sync_exec_params(sql_query
, [domain
.to_s()])
109 # Make sure the connection gets closed even if a query explodes.