Beispiel #1
0
    def add(self, data):
        self.cn = data.get('cn', '')
        self.mail = web.safestr(data.get('mail')).strip().lower()

        if not iredutils.isEmail(self.mail):
            return (False, 'INVALID_MAIL')

        # Check admin exist.
        connutils = connUtils.Utils()
        if connutils.isAdminExists(self.mail):
            return (False, 'ALREADY_EXISTS')

        # Get domainGlobalAdmin setting.
        self.domainGlobalAdmin = web.safestr(data.get('domainGlobalAdmin', 'no'))
        if self.domainGlobalAdmin not in ['yes', 'no', ]:
            self.domainGlobalAdmin = 'no'

        # Get language setting.
        preferredLanguage = web.safestr(data.get('preferredLanguage', 'en_US'))

        # Get new password.
        self.newpw = web.safestr(data.get('newpw'))
        self.confirmpw = web.safestr(data.get('confirmpw'))

        result = iredutils.verifyNewPasswords(self.newpw, self.confirmpw)

        if result[0] is True:
            self.passwd = result[1]
        else:
            return result

        try:
            self.conn.insert(
                'dbmail_admins',
                username=self.mail,
                name=self.cn,
                password=iredutils.getSQLPassword(self.passwd),
                language=preferredLanguage,
                created=iredutils.getGMTTime(),
                active='1',
            )

            if self.domainGlobalAdmin == 'yes':
                self.conn.insert(
                    'dbmail_domain_admins',
                    username=self.mail,
                    domain='ALL',
                    created=iredutils.getGMTTime(),
                    active='1',
                )

            web.logger(msg="Create admin: %s." % (self.mail), event='create',)
            return (True,)
        except Exception, e:
            return (False, str(e))
Beispiel #2
0
    def delete(self, domain, mails=[]):
        self.domain = str(domain)
        if not iredutils.isDomain(self.domain):
            return (False, 'INVALID_DOMAIN_NAME')

        if not isinstance(mails, (list, tuple,)):
            return (False, 'INVALID_MAIL')

        self.mails = [str(v).lower()
                      for v in mails
                      if iredutils.isEmail(v) and str(v).endswith('@' + self.domain)
                     ]

        # Remove alias from domain.defaultuseraliases.
        # Get domain profile.
        domainLib = domainlib.Domain()
        qr = domainLib.simpleProfile(domain=self.domain, columns=['domain', 'defaultuseraliases', ])

        if qr[0] is True:
            self.domainProfile = qr[1]
        else:
            return qr

        self.defaultUserAliases = self.domainProfile.defaultuseraliases.split(',')

        # Remove from domain.defaultuseraliases.
        self.newDefaultAliases = [str(v).lower()
                                  for v in self.defaultUserAliases
                                  if v not in self.mails
                                 ]

        # Delete domain and related records.
        try:
            self.conn.delete('dbmail_aliases_extra', where='%s' % web.sqlors('alias = ', self.mails),)
            self.conn.delete('dbmail_aliases', where='%s' % web.sqlors('alias = ', self.mails),)
            self.conn.update('dbmail_domains',
                             vars={'domain': self.domain, },
                             defaultuseraliases=','.join(self.newDefaultAliases),
                             modified=iredutils.getGMTTime(),
                             where='domain = $domain',
                            )

            web.logger(
                msg="Delete mail alias: %s." % ', '.join(self.mails),
                domain=self.domain,
                event='delete',
            )
            return (True,)
        except Exception, e:
            return (False, str(e))
Beispiel #3
0
    def update(
        self,
        domain,
        profile_type,
        data,
    ):
        self.profile_type = str(profile_type)
        self.domain = str(domain)

        # Pre-defined.
        sql_vars = {
            'domain': self.domain,
        }
        updates = {
            'modified': iredutils.getGMTTime(),
        }

        if self.profile_type == 'general':
            # Get name.
            cn = data.get('cn', '')
            updates['description'] = cn

            # Get default quota for new user.
            self.defaultQuota = str(data.get('defaultQuota'))
            if self.defaultQuota.isdigit():
                updates['defaultuserquota'] = int(self.defaultQuota)

            if session.get('domainGlobalAdmin') is True:
                # Get account status
                #if 'accountStatus' in data.keys():
                #    updates['active'] = 1
                #else:
                #    updates['active'] = 0

                # Get domain quota size.
                domainQuota = str(data.get('domainQuota', 0))
                if domainQuota.isdigit():
                    domainQuota = int(domainQuota)
                else:
                    domainQuota = 0

                if domainQuota > 0:
                    domainQuotaUnit = str(data.get('domainQuotaUnit', 'MB'))
                    if domainQuotaUnit == 'GB':
                        domainQuota = domainQuota * 1024
                    elif domainQuotaUnit == 'TB':
                        domainQuota = domainQuota * 1024 * 1024

                updates['maxquota'] = domainQuota

                # Update SQL db with columns: maxquota, active.
                try:
                    self.conn.update('dbmail_domains',
                                     vars=sql_vars,
                                     where='domain=$domain',
                                     **updates)
                except Exception, e:
                    return (False, str(e))

                # Get list of domain admins.
                domainAdmins = [
                    str(v).lower() for v in data.get('domainAdmin', [])
                    if iredutils.isEmail(str(v))
                ]

                try:
                    # Delete all records first.
                    self.conn.delete(
                        'dbmail_domain_admins',
                        vars=sql_vars,
                        where='domain=$domain',
                    )

                    # Add new admins.
                    if len(domainAdmins) > 0:
                        v = []
                        for adm in domainAdmins:
                            v += [{
                                'username': adm,
                                'domain': self.domain,
                                'created': iredutils.getGMTTime(),
                                'active': 1,
                            }]

                        self.conn.multiple_insert(
                            'dbmail_domain_admins',
                            values=v,
                        )
                except Exception, e:
                    return (False, str(e))
Beispiel #4
0
            # Get bcc status
            self.rbcc_status = '0'
            if 'recipientbcc' in data.keys():
                self.rbcc_status = '1'

            self.sbcc_status = '0'
            if 'senderbcc' in data.keys():
                self.sbcc_status = '1'

            senderBccAddress = str(data.get('senderBccAddress', None))
            if iredutils.isEmail(senderBccAddress):
                try:
                    self.conn.insert('sender_bcc_domain',
                                     domain=self.domain,
                                     bcc_address=senderBccAddress,
                                     created=iredutils.getGMTTime(),
                                     active=self.sbcc_status)
                except Exception, e:
                    return (False, str(e))

            recipientBccAddress = str(data.get('recipientBccAddress', None))
            if iredutils.isEmail(recipientBccAddress):
                try:
                    self.conn.insert('recipient_bcc_domain',
                                     domain=self.domain,
                                     bcc_address=recipientBccAddress,
                                     created=iredutils.getGMTTime(),
                                     active=self.rbcc_status)
                except Exception, e:
                    return (False, str(e))
Beispiel #5
0
    def update(self, profile_type, mail, data):
        self.profile_type = web.safestr(profile_type)
        self.mail = web.safestr(mail)

        if not session.get('domainGlobalAdmin') and session.get('username') != self.mail:
            # Don't allow to view/update other admins' profile.
            return (False, 'PERMISSION_DENIED')

        sql_vars = {'admin': self.mail, }
        if self.profile_type == 'general':
            # Get name
            self.cn = data.get('cn', '')

            # Get preferred language.
            preferredLanguage = str(data.get('preferredLanguage', 'en_US'))

            # Update in SQL db.
            try:
                self.conn.update(
                    'dbmail_admins',
                    vars=sql_vars,
                    where='username=$admin',
                    name=self.cn,
                    language=preferredLanguage,
                )

                # Update language immediately.
                if session.get('username') == self.mail:
                    session['lang'] = preferredLanguage
            except Exception, e:
                return (False, str(e))

            if session.get('domainGlobalAdmin') is True:
                # Update account status
                accountStatus = '0'    # Disabled
                if 'accountStatus' in data.keys():
                    accountStatus = '1'    # Active

                try:
                    self.conn.update(
                        'dbmail_admins',
                        vars=sql_vars,
                        where='username=$admin',
                        active=accountStatus,
                    )
                except Exception, e:
                    return (False, str(e))

                # Update global admin.
                self.domainGlobalAdmin = False
                if 'domainGlobalAdmin' in data.keys():
                    self.domainGlobalAdmin = True

                if self.domainGlobalAdmin is True:
                    try:
                        self.conn.delete(
                            'dbmail_domain_admins',
                            vars=sql_vars,
                            where='username=$admin',
                        )

                        self.conn.insert(
                            'dbmail_domain_admins',
                            username=self.mail,
                            created=iredutils.getGMTTime(),
                            domain='ALL',
                            active=accountStatus,
                        )
                    except Exception, e:
                        return (False, str(e))
Beispiel #6
0
                    try:
                        # Delete all managed domains.
                        self.conn.delete(
                            'dbmail_domain_admins',
                            vars=sql_vars,
                            where="""username=$admin AND domain <> 'ALL'""",
                        )

                        # Insert new domains.
                        for d in newmds:
                            self.conn.insert(
                                'dbmail_domain_admins',
                                username=self.mail,
                                domain=d,
                                active=accountStatus,
                                created=iredutils.getGMTTime(),
                            )
                    except Exception, e:
                        return (False, str(e))

        elif self.profile_type == 'password':
            self.cur_passwd = str(data.get('oldpw', ''))
            self.newpw = web.safestr(data.get('newpw', ''))
            self.confirmpw = web.safestr(data.get('confirmpw', ''))

            # Verify new passwords.
            qr = iredutils.verifyNewPasswords(self.newpw, self.confirmpw)
            if qr[0] is True:
                self.passwd = iredutils.getSQLPassword(qr[1])
            else:
                return qr