]>
gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/davical_plugin.rb
1 require 'common/plugin'
4 # Code that all DAViCal plugins ({DavicalPrune}, {DavicalRm}, and
5 # {DavicalMv}) will share.
9 # We implement the Plugin "interface."
12 # Initialize this DAViCal {Plugin} with values in *cfg*.
14 # @param cfg [Configuration] the configuration for this plugin.
18 :host => cfg
.davical_dbhost
,
19 :port => cfg
.davical_dbport
,
20 :options => cfg
.davical_dbopts
,
21 :dbname => cfg
.davical_dbname
,
22 :user => cfg
.davical_dbuser
,
23 :password => cfg
.davical_dbpass
}
27 # Describe the given DAViCal user who is assumed to exist.
29 # @param user [User] the {User} object whose description we want.
31 # @return [String] a String describing the given *user* in terms
32 # of his DAViCal "Principal ID".
34 def describe_user(user
)
35 principal_id
= self.get_principal_id(user
)
36 return "Principal ID: #{principal_id}"
41 # Produce a list of DAViCal users.
43 # This method remains public for use in testing.
45 # @return [Array<User>] an array of {User} objects, one for each
46 # user found in the DAViCal database.
51 connection
= PG
::Connection.new(@db_hash)
53 # User #1 is the super-user, and not tied to an email address.
54 sql_query
= 'SELECT username FROM usr WHERE user_no > 1;'
57 connection
.sync_exec(sql_query
) do |result
|
58 usernames
= result
.field_values('username')
61 # Make sure the connection gets closed even if the query explodes.
65 return usernames
.map
{ |u
| User
.new(u
) }
72 # Find the "Principal ID" of the given user.
74 # @param user [User] the user whose Principal ID we want.
76 # @return [Fixnum] an integer representing the user's Principal ID
77 # that we obtained from the DAViCal database.
79 def get_principal_id(user
)
82 connection
= PG
::Connection.new(@db_hash)
84 sql_query
= 'SELECT principal.principal_id '
85 sql_query +
= 'FROM (principal INNER JOIN usr '
86 sql_query +
= ' ON principal.user_no = usr.user_no) '
87 sql_query +
= 'WHERE usr.username = $1;'
90 connection
.sync_exec_params(sql_query
, [user
.to_s()]) do |result
|
91 if result
.num_tuples
> 0
92 principal_id
= result
[0]['principal_id']
96 # Make sure the connection gets closed even if the query explodes.