VirtualMailManager/Account.py
author Pascal Volk <neverseen@users.sourceforge.net>
Mon, 14 Apr 2008 02:21:46 +0000
changeset 16 3388b96fd3ed
parent 9 e3d3dbeb5b84
child 17 fe9be0081e5f
permissions -rw-r--r--
* 'VirtualMailManager/VirtualMailManager.py' - implemented: * VirtualMailManager.__getSalt() * VirtualMailManager.__pwCrypt() * VirtualMailManager.__pwSHA1() * VirtualMailManager.__pwMD5() * VirtualMailManager.__pwMD4() - updated VirtualMailManager.__pwhash() * 'VirtualMailManager/constants/VERSION.py' - set version to 0.4-dev * 'INSTALL' - added hint for python-crypto - updated user_query

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

"""Virtual Mail Manager's Account class to manage e-mail accounts."""

from constants.VERSION import VERSION

__author__ = 'Pascal Volk <p.volk@veb-it.de>'
__version__ = VERSION
__revision__ = 'rev '+'$Rev$'.split()[1]
__date__ = '$Date$'.split()[1]

from Exceptions import VMMAccountException
from Domain import Domain
from Transport import Transport
from MailLocation import MailLocation
import constants.ERROR as ERR

class Account:
    """Class to manage e-mail accounts."""
    def __init__(self, dbh, address, password=None):
        self._dbh = dbh
        self._base = None
        self._addr = address
        self._localpart = None
        self._name = None
        self._uid = 0
        self._gid = 0
        self._mid = 0
        self._tid = 0
        self._passwd = password
        self._setAddr(address)
        self._exists()
        if self._isAlias():
            raise VMMAccountException(
            ('There is already an alias with address »%s«' % address,
                ERR.ALIAS_EXISTS))

    def _exists(self):
        dbc = self._dbh.cursor()
        dbc.execute("SELECT uid, mid, tid FROM users \
WHERE gid=%s AND local_part=%s",
                self._gid, self._localpart)
        result = dbc.fetchone()
        dbc.close()
        if result is not None:
            self._uid, self._mid, self._tid = result
            return True
        else:
            return False

    def _isAlias(self):
        dbc = self._dbh.cursor()
        dbc.execute("SELECT gid FROM alias WHERE gid=%s AND address=%s",
                self._gid, self._localpart)
        gid = dbc.fetchone()
        dbc.close()
        if gid is not None:
            return True
        else:
            return False

    def _setAddr(self, address):
        self._localpart, d = address.split('@')
        dom = Domain(self._dbh, d)
        self._gid = dom.getID()
        if self._gid == 0:
            raise VMMAccountException(("Domain »%s« doesn't exist." % d,
                ERR.NO_SUCH_DOMAIN))
        self._base = dom.getDir()
        self._tid = dom.getTransportID()

    def _setID(self):
        dbc = self._dbh.cursor()
        dbc.execute("SELECT nextval('users_uid')")
        self._uid = dbc.fetchone()[0]
        dbc.close()

    def _prepare(self, maillocation):
        self._setID()
        self._mid = MailLocation(self._dbh, maillocation=maillocation).getID()

    def _switchState(self, state):
        if not isinstance(state, bool):
            return False
        if self._uid < 1:
            raise VMMAccountException(("Account doesn't exists",
                ERR.NO_SUCH_ACCOUNT))
        dbc = self._dbh.cursor()
        dbc.execute("""UPDATE users SET disabled=%s WHERE local_part=%s\
 AND gid=%s""", state, self._localpart, self._gid)
        if dbc.rowcount > 0:
            self._dbh.commit()
        dbc.close()

    def getUID(self):
        return self._uid

    def getGID(self):
        return self._gid

    def getDir(self, directory):
        if directory == 'domain':
            return '%s' % self._base
        elif directory == 'home':
            return '%s/%i' % (self._base, self._uid)

    def enable(self):
        self._switchState(False)

    def disable(self):
        self._switchState(True)

    def save(self, maillocation):
        if self._uid < 1:
            self._prepare(maillocation)
            dbc = self._dbh.cursor()
            dbc.execute("""INSERT INTO users (local_part, passwd, uid, gid,\
 mid, tid) VALUES (%s, %s, %s, %s, %s, %s)""", self._localpart, self._passwd,
                    self._uid, self._gid, self._mid, self._tid )
            self._dbh.commit()
            dbc.close()
        else:
            raise VMMAccountException(('Account already exists.',
                ERR.ACCOUNT_EXISTS))
       
    def modify(self, what, value):
        if self._uid == 0:
            raise VMMAccountException(("Account doesn't exists",
                ERR.NO_SUCH_ACCOUNT))
        if what not in ['name', 'password']:
            return False
        dbc = self._dbh.cursor()
        if what == 'password':
            dbc.execute("UPDATE users SET passwd=%s WHERE local_part=%s AND\
 gid=%s", value, self._localpart, self._gid)
        else:
            dbc.execute("UPDATE users SET name=%s WHERE local_part=%s AND\
 gid=%s", value, self._localpart, self._gid)
        if dbc.rowcount > 0:
            self._dbh.commit()
        dbc.close()

    def getInfo(self):
        dbc = self._dbh.cursor()
        dbc.execute("SELECT name, uid, gid, mid, tid, disabled FROM users\
 WHERE local_part=%s AND gid=%s", self._localpart, self._gid)
        info = dbc.fetchone()
        dbc.close()
        if info is None:
            raise VMMAccountException(("Account doesn't exists",
                ERR.NO_SUCH_ACCOUNT))
        else:
            keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'disabled']
            info = dict(zip(keys, info))
            if bool(info['disabled']):
                info['disabled'] = 'Yes'
            else:
                info['disabled'] = 'No'
            info['address'] = self._addr
            info['maildir'] = '%s/%s/%s' % (self._base, info['uid'],
                    MailLocation(self._dbh,
                        mid=info['maildir']).getMailLocation())
            info['transport'] = Transport(self._dbh,
                    tid=info['transport']).getTransport()
            return info

    def delete(self):
        if self._uid > 0:
            dbc = self._dbh.cursor()
            dbc.execute("DELETE FROM users WHERE gid=%s AND local_part=%s",
                    self._gid, self._localpart)
            if dbc.rowcount > 0:
                self._dbh.commit()
            dbc.close()
        else:
            raise VMMAccountException(("Account doesn't exists",
                ERR.NO_SUCH_ACCOUNT))