Fix quoting issues with the new AgenDAV "with" column.
[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 # @param user [User] the user to remove.
20 #
21 def remove_user(user)
22 raise NonexistentUserError.new(user.to_s()) if not user_exists(user)
23
24 sql_queries = ['DELETE FROM prefs WHERE username = $1;']
25
26 # The "shares" table contains principal URLs, and the "@" symbol
27 # is usually encoded to "%40". These queries do a regex match on
28 # the username after replacing the "%40" with a "@".
29 #
30 # As a precaution, I haven chosen not to delete based on the
31 # "calendar" field here. Nobody should have a calendar named
32 # "user%40example.com", but it's not impossible -- and we don't
33 # want to delete that calendar when the not-necessarily-related
34 # "user@example.com" account is removed. And the usual appearance
35 # of the user's email address in the "calendar" field happens when
36 # he is also the owner, so the calendar does get deleted in the
37 # normal situation.
38 sql_queries << "DELETE FROM shares WHERE REPLACE(owner,'%40','@') ~ $1;"
39 sql_queries << "DELETE FROM shares WHERE REPLACE(\"with\",'%40','@') ~ $1;"
40
41 connection = PG::Connection.new(@db_hash)
42 begin
43 sql_queries.each do |sql_query|
44 connection.query(sql_query, [user.to_s()])
45 end
46 ensure
47 # Make sure the connection gets closed even if a query explodes.
48 connection.close()
49 end
50 end
51
52 end