]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/davical_plugin.rb
8684cca9e830a5005e98ef82c7ac916f511decf6
[mailshears.git] / lib / common / davical_plugin.rb
1 require 'common/plugin'
2 require 'common/user'
3
4 # Code that all DAViCal plugins ({DavicalPrune}, {DavicalRm}, and
5 # {DavicalMv}) will share.
6 #
7 module DavicalPlugin
8
9 # We implement the Plugin "interface."
10 include Plugin
11
12 # Initialize this DAViCal {Plugin} with values in *cfg*.
13 #
14 # @param cfg [Configuration] the configuration for this plugin.
15 #
16 def initialize(cfg)
17 @db_host = cfg.davical_dbhost
18 @db_port = cfg.davical_dbport
19 @db_opts = cfg.davical_dbopts
20 @db_tty = cfg.davical_dbtty
21 @db_name = cfg.davical_dbname
22 @db_user = cfg.davical_dbuser
23 @db_pass = cfg.davical_dbpass
24 end
25
26
27 # Describe the given DAViCal user who is assumed to exist.
28 #
29 # @param user [User] the {User} object whose description we want.
30 #
31 # @return [String] a String describing the given *user* in terms
32 # of his DAViCal "Principal ID".
33 #
34 def describe_user(user)
35 principal_id = self.get_principal_id(user)
36 return "Principal ID: #{principal_id}"
37 end
38
39
40 #
41 # Produce a list of DAViCal users.
42 #
43 # This method remains public for use in testing.
44 #
45 # @return [Array<User>] an array of {User} objects, one for each
46 # user found in the DAViCal database.
47 #
48 def list_users()
49 usernames = []
50
51 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
52 @db_name, @db_user, @db_pass)
53
54 # User #1 is the super-user, and not tied to an email address.
55 sql_query = 'SELECT username FROM usr WHERE user_no > 1'
56
57 connection.query(sql_query) do |result|
58 usernames = result.field_values('username')
59 end
60
61 connection.close()
62
63 return usernames.map{ |u| User.new(u) }
64 end
65
66
67 protected;
68
69
70 # Find the "Principal ID" of the given user.
71 #
72 # @param user [User] the user whose Principal ID we want.
73 #
74 # @return [Fixnum] an integer representing the user's Principal ID
75 # that we obtained from the DAViCal database.
76 #
77 def get_principal_id(user)
78 principal_id = nil
79
80 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
81 @db_name, @db_user, @db_pass)
82
83 sql_query = 'SELECT principal.principal_id '
84 sql_query += 'FROM (principal INNER JOIN usr '
85 sql_query += ' ON principal.user_no = usr.user_no) '
86 sql_query += 'WHERE usr.username = $1;'
87
88 connection.query(sql_query, [user.to_s()]) do |result|
89 if result.num_tuples > 0
90 principal_id = result[0]['principal_id']
91 end
92 end
93
94 connection.close()
95
96 return principal_id
97 end
98
99 end