update_tables_0.4-dev_r24.py
author Pascal Volk <neverseen@users.sourceforge.net>
Mon, 21 Apr 2008 04:56:01 +0000
changeset 20 55146c78b3fb
permissions -rwxr-xr-x
* 'create_tables.pgsql' - Replaced column 'disabled' with columns smpt, pop3, imap and managesieve - updated view, added service columns smpt, pop3, imap and managesieve * 'update_tables_0.3.x-0.4.py' - Updated to consider the points above mentioned * 'vmm.cfg' - Added section »services« with options: smtp, pop3, imap and managesieve * 'update_config_0.3.x-0.4.py' * 'VirtualMailManager/Config.py' - Updated, to add new section »services« * 'VirtualMailManager/Account.py' * 'VirtualMailManager/VirtualMailManager.py' * 'vmm' - Modified, to fit new database structure * 'UPGRADE' * 'INSTALL' - Updated information * 'update_tables_0.4-dev_r24.py' - Added temporary to the repository

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Copyright 2008 VEB IT
# See COPYING for distribution information.
# $Id$

from ConfigParser import ConfigParser
from pyPgSQL import PgSQL

cff = file('/usr/local/etc/vmm.cfg', 'r')
cf = ConfigParser()
cf.readfp(cff)
cff.close()

dbh = PgSQL.connect(database=cf.get('database', 'name'),
        user=cf.get('database', 'user'), host=cf.get('database', 'host'),
        password=cf.get('database', 'pass'), client_encoding='utf8',
        unicode_results=True)
dbc = dbh.cursor()
dbc.execute("SET NAMES 'UTF8'")

for service in ['smtp', 'pop3', 'imap', 'managesieve']:
    dbc.execute(
            "ALTER TABLE users ADD %s boolean NOT NULL DEFAULT TRUE" % service)
    dbh.commit()

dbc.execute("SELECT uid FROM users WHERE disabled")
res = dbc.fetchall()
if len(res):
    for uid in res:
        dbc.execute("UPDATE users SET smtp = FALSE, pop3 = FALSE, imap = FALSE, managesieve = FALSE WHERE uid = %s", uid[0])
    dbh.commit()
dbc.execute("ALTER TABLE users DROP disabled")
dbh.commit()

dbc.execute("DROP VIEW dovecot_password")
dbh.commit()
dbc.execute("""CREATE OR REPLACE VIEW dovecot_password AS
    SELECT local_part || '@' || domains.domainname AS "user",
           passwd AS "password", smtp, pop3, imap, managesieve
      FROM users
           LEFT JOIN domains USING (gid)""")
dbh.commit()
dbh.close()

# print importnat information
print 
print "* set permissions for replaced views:"
print "connect to your database [psql %s] and execute:" % cf.get('database',
'name')
print "GRANT SELECT ON dovecot_password TO your_dovecot_dbuser;"