Begin building the framework to rename accounts. A pile of crap right now.
[mailshears.git] / lib / mv / plugins / davical.rb
1 require 'pg'
2
3 require 'common/plugin'
4 require 'rm/rm_plugin'
5
6 class DavicalMv
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 MvPlugin
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 mv_domain(from, to)
44 # DAViCal doesn't have a concept of domains.
45 end
46
47
48 def mv_account(from, to)
49 # Delete the given username. DAViCal uses foreign keys properly
50 # and only supports postgres, so we let the ON UPDATE CASCADE
51 # trigger handle most of the work.
52 sql_queries = ['UPDATE usr SET username = $1 WHERE username = $2']
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, [to, from])
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 protected;
78
79 def get_principal_id(account)
80 principal_id = nil
81
82 begin
83 connection = PGconn.connect(@db_host,
84 @db_port,
85 @db_opts,
86 @db_tty,
87 @db_name,
88 @db_user,
89 @db_pass)
90
91 sql_query = "SELECT principal.principal_id "
92 sql_query += "FROM (principal INNER JOIN usr "
93 sql_query += " ON principal.user_no = usr.user_no) "
94 sql_query += "WHERE usr.username = $1;"
95
96 connection.query(sql_query, [account]) do |result|
97 if result.num_tuples > 0
98 principal_id = result[0]['principal_id']
99 end
100 end
101
102 connection.close()
103
104 rescue PGError => e
105 # Pretend like we're database-agnostic in case we ever are.
106 raise DatabaseError.new(e)
107 end
108
109 return principal_id
110 end
111
112 end