Don't fail for nonexistent users in AgendavRm.
[mailshears.git] / lib / rm / plugins / agendav.rb
1 require 'pg'
2
3 require 'common/agendav_plugin'
4 require 'rm/rm_plugin'
5
6
7 # Handle the removal of Agendav users from its database. Agendav has
8 # no concept of domains.
9 #
10 class AgendavRm
11
12 include AgendavPlugin
13 include RmPlugin
14
15
16 # Remove *user* from the Agendav database. This should remove him
17 # from _every_ table in which he is referenced.
18 #
19 # We do not raise an error if the user doesn't exist. This is due to
20 # an unfortunate problem with the "user exists" check in AgenDAV.
21 # The AgenDAV "shares" table is not tied directly to a username, so
22 # we are forced to use a regexp match to decide what rows to delete
23 # from that table. We do so regardless of whether or not the username
24 # exists in the "prefs" table, because that table stores only non-
25 # default preferences -- not all users' preferences.
26 #
27 # @param user [User] the user to remove.
28 #
29 def remove_user(user)
30 sql_queries = ['DELETE FROM prefs WHERE username = $1;']
31
32 # The "shares" table contains principal URLs, and the "@" symbol
33 # is usually encoded to "%40". These queries do a regex match on
34 # the username after replacing the "%40" with a "@".
35 #
36 # As a precaution, I haven chosen not to delete based on the
37 # "calendar" field here. Nobody should have a calendar named
38 # "user%40example.com", but it's not impossible -- and we don't
39 # want to delete that calendar when the not-necessarily-related
40 # "user@example.com" account is removed. And the usual appearance
41 # of the user's email address in the "calendar" field happens when
42 # he is also the owner, so the calendar does get deleted in the
43 # normal situation.
44 sql_queries << "DELETE FROM shares WHERE REPLACE(owner,'%40','@') ~ $1;"
45 sql_queries << "DELETE FROM shares WHERE REPLACE(\"with\",'%40','@') ~ $1;"
46
47 connection = PG::Connection.new(@db_hash)
48 begin
49 sql_queries.each do |sql_query|
50 connection.query(sql_query, [user.to_s()])
51 end
52 ensure
53 # Make sure the connection gets closed even if a query explodes.
54 connection.close()
55 end
56 end
57
58 end