]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/mailshears/plugins/agendav.rb
Don't cast the username to an int in the AgenDAV plugin.
[mailshears.git] / lib / mailshears / plugins / agendav.rb
1 require 'pg'
2
3 class AgendavDb
4
5 include Plugin
6
7 def initialize()
8 cfg = Configuration.new()
9 @db_host = cfg.agendav_dbhost
10 @db_port = cfg.agendav_dbport
11 @db_opts = cfg.agendav_dbopts
12 @db_tty = cfg.agendav_dbtty
13 @db_name = cfg.agendav_dbname
14 @db_user = cfg.agendav_dbuser
15 @db_pass = cfg.agendav_dbpass
16 end
17
18
19 def describe_domain(domain)
20 # AgenDAV doesn't have a concept of domains.
21 return 'N/A'
22 end
23
24 def describe_account(account)
25 if self.user_exists(account)
26 return "Username: #{account}"
27 else
28 return 'User not found'
29 end
30 end
31
32 def delete_domain(domain)
33 # AgenDAV doesn't have a concept of domains.
34 end
35
36 def delete_account(account)
37 # Delete the given username and any records in other tables
38 # belonging to it.
39
40 sql_queries = ['DELETE FROM prefs WHERE username = $1;']
41 sql_queries << 'DELETE FROM shared WHERE user_from = $1;'
42
43 begin
44 connection = PGconn.connect(@db_host,
45 @db_port,
46 @db_opts,
47 @db_tty,
48 @db_name,
49 @db_user,
50 @db_pass)
51
52 sql_queries.each do |sql_query|
53 connection.query(sql_query, [account])
54 end
55
56 connection.close()
57
58 rescue PGError => e
59 # Pretend like we're database-agnostic in case we ever are.
60 raise DatabaseError.new(e)
61 end
62
63 end
64
65
66 def get_leftover_domains(db_domains)
67 # AgenDAV doesn't have a concept of domains.
68 return []
69 end
70
71
72 def get_leftover_accounts(db_accounts)
73 # Get a list of all users who have logged in to AgenDAV.
74 ad_accounts = self.get_agendav_usernames()
75 return ad_accounts - db_accounts
76 end
77
78
79 protected;
80
81 def user_exists(account)
82 ad_users = get_agendav_usernames()
83 return ad_users.include?(account)
84 end
85
86 def get_agendav_usernames()
87 usernames = []
88
89 # Just assume PostgreSQL for now.
90 begin
91 connection = PGconn.connect(@db_host,
92 @db_port,
93 @db_opts,
94 @db_tty,
95 @db_name,
96 @db_user,
97 @db_pass)
98
99 sql_query = '(SELECT username FROM prefs)'
100 sql_query += 'UNION'
101 sql_query += '(SELECT user_from FROM shared);'
102
103 connection.query(sql_query) do |result|
104 usernames = result.field_values('username')
105 end
106
107 connection.close()
108 rescue PGError => e
109 # Pretend like we're database-agnostic in case we ever are.
110 raise DatabaseError.new(e)
111 end
112
113 return usernames
114 end
115
116 end