mailcnf_user.py 12.3 KB
#!/usr/bin/python3

# -*- coding: UTF-8 -*-# enable debugging
from mysql.connector import MySQLConnection, Error
from read_dbconfig import read_db_config, read_basic_config
import hashlib
import tornado.ioloop
import tornado.web
import  tornado.escape
import  tornado
import os.path
import crypt
from mailsrv.mailcnf_tools import mailcnf_tools
mt=mailcnf_tools()

from ncauth.ncauth_base import ncauth_base
class mailcnf_user(ncauth_base):
    
    def get(self):
         self.redirect("/")
    
    @tornado.web.authenticated
    def post(self): 

        nav=all
        subnav=all
        content=all
        msglvl=""  
        msg=False
        module= self.get_argument("module", None, False)
        mainfnc= self.get_argument("mainfnc", None, False)
        mainid= self.get_argument("id", None, False)
        fnc= self.get_argument("fnc", None, False)
       # table= self.get_argument("table", None, False)
        
        email= self.get_argument("email", None, False)
        sent_mail= self.get_argument("sent_mail", None, False) 
        active= self.get_argument("active", None, False)
        pw1= self.get_argument("pw1", None, False)
        pw2= self.get_argument("pw2", None, False)
        quota= self.get_argument("quota", None, False)
        group_id= self.get_argument("group_id", None, False)
        domaintld= self.get_argument("domaintld", None, False)
        domain_id= self.get_argument("domain_id", None, False)
        #self.write("Hello, "+str(module )+" "+str(mainfnc)+" id: "+str(mainid)+" fnc: "+str(fnc)+" "+str(table)+"<br>")
        uid=0
       
        ugroup=0
        combolist=mt.getDomainNamebyGuid(ugroup)
        msg=""
        loader =tornado. template.Loader(os.path.join(os.path.dirname(__file__), "../templates"))
        navigation=loader.load("navigation.html").generate(nav=nav)
        if fnc=="delete" and mainid :
            msg=self.update_virtual_users_deleted(mainid);
            msglvl="alert alert-danger"
        if fnc=="undelete" and mainid :
            msg=self.update_virtual_users_undelete(mainid);
            msglvl="alert alert-danger"
        if fnc=="submitnewuser"  :
            if pw1 ==pw2:
                msg=self.insert_virtual_users(email,domaintld, sent_mail,active,pw1,pw2,quota,group_id)
                msg="Emailkonto angelegt"
                msglvl="alert alert-danger"
              
            else:
                 msg="Passwörter stimnmen nicht überein"
        if fnc=="submitnewpw"  :
            if pw1 ==pw2:
                msg=self.update_virtual_users_by_id_chpw( mainid,pw1,pw2)
                msg="Passwort  angelegt"
                msglvl="alert alert-danger"
            else:
                 msg="Passwörter stimnmen nicht überein"         
        if fnc=="submitedit"  :
            self.update_virtual_users( mainid,domain_id,email,sent_mail,active,quota,group_id)
            msg="Emailkonto aktualisert"
            msglvl="alert alert-danger"
                       
                 
                
        if  fnc=="edit" and mainid :  
            
            subnavigation=loader.load("subnav_"+module+"_"+mainfnc+".html").generate(subnav=subnav)
            lists=self.edit_virtual_users_by_id(mainid, uid)
            print(lists)
            maincontent=loader.load("content_"+module+"_"+mainfnc+"_"+fnc+".html").generate(content=content, lists=lists, uid=uid, ugroup=ugroup, combolist=combolist)
            self.render('base.html', navigation=navigation, subnavigation=subnavigation, maincontent=maincontent, msg=msg, msglvl=msglvl)
        elif  fnc=="changepw" and mainid : 
            subnavigation=loader.load("subnav_"+module+"_"+mainfnc+".html").generate(subnav=subnav)
            lists=""
            maincontent=loader.load("content_"+module+"_"+mainfnc+"_"+fnc+".html").generate(content=content, lists=lists, mainid=mainid)
            self.render('base.html', navigation=navigation, subnavigation=subnavigation, maincontent=maincontent, msg=msg, msglvl=msglvl)

        elif  fnc=="new": 
            subnavigation=loader.load("subnav_"+module+"_"+mainfnc+".html").generate(subnav=subnav)
            maincontent=loader.load("content_"+module+"_"+mainfnc+"_"+fnc+".html").generate(content=content,uid=uid, ugroup=ugroup, combolist=combolist)
            self.render('base.html', navigation=navigation, subnavigation=subnavigation, maincontent=maincontent, msg=msg, msglvl=msglvl)
        else:
            subnavigation=loader.load("subnav_"+module+"_"+mainfnc+".html").generate(subnav=subnav)
            lists=self.print_virtual_users_by_domain_id('all','all',"all","0")
            maincontent=loader.load("content_"+module+"_"+mainfnc+".html").generate(content=content, lists=lists, combolist=combolist)
            self.render('base.html', navigation=navigation, subnavigation=subnavigation, maincontent=maincontent, msg=msg, msglvl=msglvl)
   
    def getUsers(self):
        self.write("Hello, ")

    def update_virtual_users_undelete(self, id):
        query = "UPDATE virtual_users SET deleted='0', active='1',sent_mail='1'    WHERE id='"+str(id)+"'"
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
            cursor.execute(query)
            conn.commit()
            return "ID "+str(id)+" wiederhergestellt"
        except Error as error:
            print(error)
        finally:
            cursor.close()
            conn.close()

    def update_virtual_users_deleted(self, id):
        query = "UPDATE virtual_users SET deleted='1', active='0',sent_mail='0'  WHERE id='"+str(id)+"'"
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
            cursor.execute(query)
            conn.commit()
            return "ID "+str(id)+" deleted"
        except Error as error:
            print(error)
        finally:
            cursor.close()
            conn.close()
    
    
    def update_virtual_users(self, id,domain_id,email,sent_mail,active,quota,group_id):
        query = "UPDATE virtual_users SET domain_id='"+domain_id+"' ,email='"+email+"' ,sent_mail='"+str(sent_mail)+"' ,active='"+str(active)+"' ,quota='"+str(quota)+"' ,group_id='"+str(group_id)+"'   WHERE id='"+str(id)+"'"
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
            cursor.execute(query)
            conn.commit()
            #self.write(query)
        except Error as error:
            print(error)
        finally:
            cursor.close()
            conn.close()

    def  update_virtual_users_by_id_chpw(self, id,pw1,pw2):
        #password= hashlib.md5(pw1.encode()).hexdigest()
        basic_config = read_basic_config()
        
        if basic_config['pwcrypt'] =='old':
            #self.write(basic_config['pwcrypt'])
            password= hashlib.md5(pw1.encode()).hexdigest()
        else:
            password=crypt.crypt(pw1,'$1$x87mPvWg')
        
        
        query = "UPDATE virtual_users SET password='"+password+"'   WHERE `id`='"+str(id)+"'"
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
            cursor.execute(query)
            conn.commit()
        except Error as error:
            print(error)
        finally:
            cursor.close()
            conn.close()
    def insert_virtual_users(self, email,domaintld,sent_mail,active,pw1,pw2,quota,group_id):
        #print(email+'###: '+domaintld)
        domain_id= str(mt.get_domain_id_by_name(domaintld))
        email= email+'@'+domaintld
        basic_config = read_basic_config()
        if basic_config['pwcrypt'] =='old':
            password= hashlib.md5(pw1.encode()).hexdigest()
        else:
            password=crypt.crypt(pw1,'$1$1x87mPvWg')
        
        query = "INSERT INTO virtual_users(domain_id,password,email,sent_mail,active,quota,group_id) VALUES('"+domain_id+"','"+password+"','"+email+"','"+sent_mail+"','"+active+"','"+quota+"','"+group_id+"')"
        self.write(query)
       # args = (domain_id,password,email,sent_mail,active)
        insert_id = ''
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
           
            cursor.execute(query)
            if cursor.lastrowid:
                insert_id=str(cursor.lastrowid)
            else:
                print('mailcnf_domain')
            conn.commit()
        except Error as error:
            print(error)
        finally:
            cursor.close()
            conn.close()
        return insert_id

    def print_virtual_users_by_domain_id(self, domain_id,id,guid,uid):
        query = ''
        delstring = ' '
        dictdata =[]
        if str(uid) !='0':
            delstring = " and deleted='0' "
        if domain_id == 'all':
            if guid=='all':
                query = "  SELECT id,domain_id,group_id,password,password_alt,email,sent_mail,quota,modified,active,deleted FROM `virtual_users`"
            else:
                query = "  SELECT id,domain_id,group_id,password,password_alt,email,sent_mail,quota,modified,active,deleted FROM `virtual_users` WHERE group_id in  ( "+guid+" )  " +delstring
        else:
            if id == 'all':
                query = " SELECT id,domain_id,group_id,password,password_alt,email,sent_mail,quota,modified,active,deleted FROM `virtual_users` WHERE `domain_id`= "+domain_id+" AND  group_id in ( "+guid+" ) " +delstring
            else:
                query = "  SELECT id,domain_id,group_id,password,password_alt,email,sent_mail,quota,modified,active,deleted FROM `virtual_users` WHERE `id`='"+id+"' "+delstring
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
            cursor.execute(query)
            fields=cursor.fetchall()
            counter = 0
            for field in fields:
                counter = counter + 1
                field=field+(counter, )
                dictdata.append(field)                
        except Error as error:
            print(error)
        finally:
            conn.commit()
            cursor.close()
            conn.close()
            return dictdata

    




    
    def edit_virtual_users_by_id(self, id,uid):
        query = " SELECT id,domain_id,group_id,password,password_alt,email,sent_mail,quota,modified,active,deleted FROM `virtual_users` WHERE `id`='"+str(id)+"'"
        dictdata =[]
       # self.write(query)
        counter=0
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
            cursor.execute(query)
            fields=cursor.fetchall()
            for field in fields:
                    counter = counter + 1
                    field=field+(counter, )
                    dictdata.append(field)   
            conn.commit()
        except Error as error:
            print(error)
        finally:
            cursor.close()
            conn.close()
            return dictdata
            
     
        


    def get_domain_id_by_name(self,name):
            id=0
            query = " SELECT id FROM `virtual_domains` WHERE  `name`='"+name+"' "
            try:
                db_config = read_db_config()
                conn = MySQLConnection(**db_config)
                cursor = conn.cursor()
                cursor.execute(query)
                fields=cursor.fetchall()
                for field in fields:
                    id=field[0]       
            except Error as error:
                print(error)
            finally:
                cursor.close()
                conn.close()
            return id
            

    def get_domainname_by_id(id):
        name=''
        query = "SELECT name FROM virtual_domains WHERE id='"+str(id)+"' ;"
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
            #print(query)
            cursor.execute(query)
            fields=cursor.fetchall()
            for field in fields:
                #counter = counter + 1
                name = field[0]
        except Error as error:
            print(error)
     
        finally:
            cursor.close()
            conn.close()
        return name