]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/rm/plugins/davical.rb
b8c8203eb28fc5596c238e9752ef62858e4f6011
[mailshears.git] / lib / rm / plugins / davical.rb
1 require 'pg'
2
3 require 'common/plugin'
4 require 'rm/rm_plugin'
5
6 class DavicalDb
7 #
8 # DAViCal only supports Postgres, so even if we ever are
9 # database-agnostic, this plugin can't be.
10 #
11 include Plugin
12 include RmPlugin
13
14 def initialize()
15 cfg = Configuration.new()
16 @db_host = cfg.davical_dbhost
17 @db_port = cfg.davical_dbport
18 @db_opts = cfg.davical_dbopts
19 @db_tty = cfg.davical_dbtty
20 @db_name = cfg.davical_dbname
21 @db_user = cfg.davical_dbuser
22 @db_pass = cfg.davical_dbpass
23 end
24
25
26 def describe_domain(domain)
27 # DAViCal doesn't have a concept of domains.
28 return 'N/A'
29 end
30
31
32 def describe_account(account)
33 principal_id = self.get_principal_id(account)
34
35 if principal_id.nil?
36 return 'User not found'
37 else
38 return "Principal ID: #{principal_id}"
39 end
40 end
41
42
43 def delete_domain(domain)
44 # DAViCal doesn't have a concept of domains.
45 end
46
47
48 def delete_account(account)
49 # Delete the given username. DAViCal uses foreign keys properly
50 # and only supports postgres, so we let the ON DELETE CASCADE
51 # trigger handle most of the work.
52 sql_queries = ['DELETE FROM usr WHERE username = $1']
53
54 begin
55 connection = PGconn.connect(@db_host,
56 @db_port,
57 @db_opts,
58 @db_tty,
59 @db_name,
60 @db_user,
61 @db_pass)
62
63 sql_queries.each do |sql_query|
64 connection.query(sql_query, [account])
65 end
66
67 connection.close()
68
69 rescue PGError => e
70 # Pretend like we're database-agnostic in case we ever are.
71 raise DatabaseError.new(e)
72 end
73
74 end
75
76
77 def get_leftover_domains(db_domains)
78 # AgenDAV doesn't have a concept of domains.
79 return []
80 end
81
82
83 def get_leftover_accounts(db_accounts)
84 # Get a list of all users who have logged in to DAViCal.
85 davical_accounts = self.get_davical_usernames()
86 return davical_accounts - db_accounts
87 end
88
89
90 protected;
91
92 def get_principal_id(account)
93 principal_id = nil
94
95 begin
96 connection = PGconn.connect(@db_host,
97 @db_port,
98 @db_opts,
99 @db_tty,
100 @db_name,
101 @db_user,
102 @db_pass)
103
104 sql_query = "SELECT principal.principal_id "
105 sql_query += "FROM (principal INNER JOIN usr "
106 sql_query += " ON principal.user_no = usr.user_no) "
107 sql_query += "WHERE usr.username = $1;"
108
109 connection.query(sql_query, [account]) do |result|
110 if result.num_tuples > 0
111 principal_id = result[0]['principal_id']
112 end
113 end
114
115 connection.close()
116
117 rescue PGError => e
118 # Pretend like we're database-agnostic in case we ever are.
119 raise DatabaseError.new(e)
120 end
121
122 return principal_id
123 end
124
125
126 def get_davical_usernames()
127 usernames = []
128
129 begin
130 connection = PGconn.connect(@db_host,
131 @db_port,
132 @db_opts,
133 @db_tty,
134 @db_name,
135 @db_user,
136 @db_pass)
137
138 # User #1 is the super-user, and not tied to an email address.
139 sql_query = 'SELECT username FROM usr WHERE user_no > 1;'
140
141 connection.query(sql_query) do |result|
142 usernames = result.field_values('username')
143 end
144
145 connection.close()
146 rescue PGError => e
147 # Pretend like we're database-agnostic in case we ever are.
148 raise DatabaseError.new(e)
149 end
150
151 return usernames
152 end
153
154 end