Stop pretending that we'll ever work with another DBMS.
[mailshears.git] / lib / common / davical_plugin.rb
1 require 'common/plugin'
2 require 'common/user'
3
4 module DavicalPlugin
5 # Code that all Davical plugins (Prune, Rm, Mv...) will share. That
6 # is, we implement the Plugin interface.
7 include Plugin
8
9 def initialize(cfg)
10 @db_host = cfg.davical_dbhost
11 @db_port = cfg.davical_dbport
12 @db_opts = cfg.davical_dbopts
13 @db_tty = cfg.davical_dbtty
14 @db_name = cfg.davical_dbname
15 @db_user = cfg.davical_dbuser
16 @db_pass = cfg.davical_dbpass
17 end
18
19
20 def describe_user(user)
21 principal_id = self.get_principal_id(user)
22 return "Principal ID: #{principal_id}"
23 end
24
25
26 def list_users()
27 #
28 # Produce a list of DAViCal users. This is public because it's
29 # useful for testing.
30 #
31 usernames = []
32
33 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
34 @db_name, @db_user, @db_pass)
35
36 # User #1 is the super-user, and not tied to an email address.
37 sql_query = 'SELECT username FROM usr WHERE user_no > 1'
38
39 connection.query(sql_query) do |result|
40 usernames = result.field_values('username')
41 end
42
43 connection.close()
44
45 return usernames.map{ |u| User.new(u) }
46 end
47
48
49 protected;
50
51 def get_principal_id(user)
52 principal_id = nil
53
54 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
55 @db_name, @db_user, @db_pass)
56
57 sql_query = 'SELECT principal.principal_id '
58 sql_query += 'FROM (principal INNER JOIN usr '
59 sql_query += ' ON principal.user_no = usr.user_no) '
60 sql_query += 'WHERE usr.username = $1;'
61
62 connection.query(sql_query, [user.to_s()]) do |result|
63 if result.num_tuples > 0
64 principal_id = result[0]['principal_id']
65 end
66 end
67
68 connection.close()
69
70 return principal_id
71 end
72
73 end