Make list_users() methods public in several plugins.
[mailshears.git] / lib / common / davical_plugin.rb
1 require 'common/plugin'
2
3 module DavicalPlugin
4 # Code that all Davical plugins (Prune, Rm, Mv...) will share. That
5 # is, we implement the Plugin interface.
6 include Plugin
7
8 def initialize(cfg)
9 @db_host = cfg.davical_dbhost
10 @db_port = cfg.davical_dbport
11 @db_opts = cfg.davical_dbopts
12 @db_tty = cfg.davical_dbtty
13 @db_name = cfg.davical_dbname
14 @db_user = cfg.davical_dbuser
15 @db_pass = cfg.davical_dbpass
16 end
17
18
19 def describe_domain(domain)
20 # DAViCal doesn't have a concept of domains.
21 return domain
22 end
23
24
25 def describe_user(user)
26 principal_id = self.get_principal_id(user)
27
28 if principal_id.nil?
29 return 'User not found'
30 else
31 return "Principal ID: #{principal_id}"
32 end
33 end
34
35
36 def list_users()
37 #
38 # Produce a list of DAViCal users. This is public because it's
39 # useful for testing.
40 #
41 usernames = []
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 # User #1 is the super-user, and not tied to an email address.
53 sql_query = "SELECT username FROM usr WHERE user_no > 1"
54
55 connection.query(sql_query) do |result|
56 usernames = result.field_values('username')
57 end
58
59 connection.close()
60 rescue PGError => e
61 # Pretend like we're database-agnostic in case we ever are.
62 raise DatabaseError.new(e)
63 end
64
65 return usernames
66 end
67
68
69 protected;
70
71 def get_principal_id(user)
72 principal_id = nil
73
74 begin
75 connection = PGconn.connect(@db_host,
76 @db_port,
77 @db_opts,
78 @db_tty,
79 @db_name,
80 @db_user,
81 @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]) 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 rescue PGError => e
97 # Pretend like we're database-agnostic in case we ever are.
98 raise DatabaseError.new(e)
99 end
100
101 return principal_id
102 end
103
104 end