]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/rm/plugins/roundcube_db.rb
3e18e8e4b8a5c2bbb8db7555ba8745773ed8f5b5
[mailshears.git] / lib / rm / plugins / roundcube_db.rb
1 require 'pg'
2
3 require 'common/roundcube_db_plugin'
4 require 'rm/rm_plugin'
5
6 class RoundcubeDbRm
7
8 include RoundcubeDbPlugin
9 include RmPlugin
10
11 def delete_domain(domain)
12 # Roundcube doesn't have a concept of domains.
13 end
14
15 def delete_account(account)
16 # Delete the given username and any records in other tables
17 # belonging to it.
18 user_id = self.get_user_id(account)
19
20 # The Roundcube developers were nice enough to include
21 # DBMS-specific install and upgrade scripts, so Postgres can take
22 # advantage of ON DELETE triggers. Here's an example:
23 #
24 # ...
25 # user_id integer NOT NULL
26 # REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE CASCADE
27 #
28 # This query is of course necessary with any DBMS:
29 sql_queries = ['DELETE FROM users WHERE user_id = $1::int;']
30
31 begin
32 connection = PGconn.connect(@db_host,
33 @db_port,
34 @db_opts,
35 @db_tty,
36 @db_name,
37 @db_user,
38 @db_pass)
39
40 sql_queries.each do |sql_query|
41 connection.query(sql_query, [user_id])
42 end
43
44 connection.close()
45
46 rescue PGError => e
47 # Pretend like we're database-agnostic in case we ever are.
48 raise DatabaseError.new(e)
49 end
50
51 end
52
53
54 def get_leftover_domains(db_domains)
55 # Roundcube doesn't have a concept of domains.
56 return []
57 end
58
59
60 def get_leftover_accounts(db_accounts)
61 # Get a list of all users who have logged in to Roundcube.
62 rc_accounts = self.get_roundcube_usernames()
63 return rc_accounts - db_accounts
64 end
65
66
67 protected;
68
69 def get_roundcube_usernames()
70 usernames = []
71
72 # Just assume PostgreSQL for now.
73 begin
74 connection = PGconn.connect(@db_host,
75 @db_port,
76 @db_opts,
77 @db_tty,
78 @db_name,
79 @db_user,
80 @db_pass)
81
82 sql_query = "SELECT username FROM users;"
83 connection.query(sql_query) do |result|
84 usernames = result.field_values('username')
85 end
86
87 connection.close()
88 rescue PGError => e
89 # Pretend like we're database-agnostic in case we ever are.
90 raise DatabaseError.new(e)
91 end
92
93 return usernames
94 end
95
96 end