def GET( self, domain=None, ): if domain is None: self.cur_domain = None else: self.cur_domain = str(domain) if not iredutils.isDomain(self.cur_domain): return web.seeother('/domains?msg=INVALID_DOMAIN_NAME') i = web.input() # Get all managed domains. domainLib = domainlib.Domain() result = domainLib.getAllDomains(columns=[ 'domain', 'description', 'maxquota', 'mailboxes', 'defaultuserquota', 'minpasswordlength', 'maxpasswordlength', ]) if result[0] is True: allDomains = result[1] else: return web.seeother('/domains?msg=' % result[1]) # Set first domain as current domain. if self.cur_domain is None: if len(allDomains) > 0: return web.seeother('/create/user/%s' % str(allDomains[0].domain)) else: return web.seeother('/domains?msg=NO_DOMAIN_AVAILABLE') # Get domain profile. resultOfProfile = domainLib.profile(domain=self.cur_domain) if resultOfProfile[0] is True: self.profile = resultOfProfile[1] else: return web.seeother('/domains?msg=%s' % resultOfProfile[1]) # Cet total number and allocated quota size of existing users under domain. self.numberOfExistAccounts = 0 self.usedQuotaSize = 0 qr = domainLib.getCountsOfExistAccountsUnderDomain( domain=self.cur_domain, accountType='user', ) if qr[0] is True: self.numberOfExistAccounts = qr[1] self.usedQuotaSize = qr[2] return web.render( 'mysql/user/create.html', cur_domain=self.cur_domain, allDomains=allDomains, profile=self.profile, numberOfExistAccounts=self.numberOfExistAccounts, usedQuotaSize=self.usedQuotaSize, msg=i.get('msg'), )
def add(self, domain, data): # Get domain name, username, cn. self.domain = web.safestr(data.get('domainName')).strip().lower() self.username = web.safestr(data.get('username')).strip().lower() self.mail = self.username + '@' + self.domain if self.domain != domain: return (False, 'PERMISSION_DENIED') if not iredutils.isDomain(self.domain): return (False, 'INVALID_DOMAIN_NAME') # Check account existing. connutils = connUtils.Utils() if connutils.isEmailExists(mail=self.mail): return (False, 'ALREADY_EXISTS') # Get domain profile. domainLib = domainlib.Domain() resultOfDomainProfile = domainLib.profile(domain=self.domain) if resultOfDomainProfile[0] is True: self.domainProfile = resultOfDomainProfile[1] else: return resultOfDomainProfile # Check account limit. adminLib = adminlib.Admin() numberOfExistAccounts = adminLib.getNumberOfManagedAccounts( accountType='user', domains=[self.domain]) if self.domainProfile.mailboxes == 0: # Unlimited. pass elif self.domainProfile.mailboxes <= numberOfExistAccounts: return (False, 'EXCEEDED_DOMAIN_ACCOUNT_LIMIT') # Check spare quota and number of spare account limit. # Get quota from <form> self.mailQuota = str(data.get('mailQuota')).strip() self.defaultUserQuota = self.domainProfile.get('defaultuserquota', 0) if self.mailQuota.isdigit(): self.mailQuota = int(self.mailQuota) else: self.mailQuota = self.defaultUserQuota # Re-calculate mail quota if this domain has limited max quota. if self.domainProfile.maxquota > 0: # Get used quota. qr = domainLib.getAllocatedQuotaSize(domain=self.domain) if qr[0] is True: self.allocatedQuota = qr[1] else: return qr spareQuota = self.domainProfile.maxquota - self.allocatedQuota if spareQuota > 0: if spareQuota < self.mailQuota: self.mailQuota = spareQuota else: # No enough quota. return (False, 'EXCEEDED_DOMAIN_QUOTA_SIZE') # # Get password from <form>. # self.newpw = str(data.get('newpw', '')) self.confirmpw = str(data.get('confirmpw', '')) # Get password length limit from domain profile or global setting. self.minPasswordLength = self.domainProfile.get( 'minpasswordlength', cfg.general.get('min_passwd_length', '0')) self.maxPasswordLength = self.domainProfile.get( 'maxpasswordlength', cfg.general.get('max_passwd_length', '0')) resultOfPW = iredutils.verifyNewPasswords( self.newpw, self.confirmpw, min_passwd_length=self.minPasswordLength, max_passwd_length=self.maxPasswordLength, ) if resultOfPW[0] is True: self.passwd = iredutils.getSQLPassword(resultOfPW[1]) else: return resultOfPW # Get display name from <form> self.cn = data.get('cn', '') # Assign new user to default mail aliases. assignedAliases = [ str(v).lower() for v in str(self.domainProfile.defaultuseraliases).split(',') if iredutils.isEmail(v) ] try: # Store new user in SQL db. self.conn.insert( 'mailbox', domain=self.domain, username=self.mail, password=self.passwd, name=self.cn, maildir=iredutils.setMailMessageStore(self.mail), quota=self.mailQuota, created=iredutils.sqlNOW, active='1', local_part=self.username, ) # Assign new user to default mail aliases. if len(assignedAliases) > 0: for ali in assignedAliases: try: self.conn.query(''' UPDATE alias SET goto=CONCAT(goto, %s) WHERE address=%s AND domain=%s ''' % ( web.sqlquote(',' + self.mail), web.sqlquote(ali), web.sqlquote(self.domain), )) except: pass # Create an alias account: address=goto. self.conn.insert( 'alias', address=self.mail, goto=self.mail, domain=self.domain, created=iredutils.sqlNOW, active='1', ) web.logger( msg="Create user: %s." % (self.mail), domain=self.domain, event='create', ) return (True, ) except Exception, e: return (False, str(e))