view website/issues/detectors/userauditor.py @ 6628:2bb6d7baa47d

"Comment" out the meta data - will not process under 1.7.5 sphinx Apparently field names with : fail on 1.7.5 sphinx which is the virtual env version on sourceforge. It works on my 1.6.7 python2 install. Looks like I need to add sphinxext-opengraph to get this to work. However that is python3 only so need to spin up new virtualenv etc. Looks like no python3 on sourceforge which may be an issue. On sourceforge in /home/project-web/roundup/src/docbuilder these packages are used and must be scp'ed as pip has no network access outside of sourceforge: Babel-2.6.0-py2.py3-none-any.whl Jinja2-2.10-py2.py3-none-any.whl MarkupSafe-1.0.tar.gz Pygments-2.2.0-py2.py3-none-any.whl Sphinx-1.7.5 Sphinx-1.7.5-py2.py3-none-any.whl Sphinx-1.7.5.tar.gz alabaster-0.7.11-py2.py3-none-any.whl certifi-2018.4.16-py2.py3-none-any.whl chardet-3.0.4-py2.py3-none-any.whl docutils-0.14-py2-none-any.whl idna-2.7-py2.py3-none-any.whl imagesize-1.0.0-py2.py3-none-any.whl packaging-17.1-py2.py3-none-any.whl pip-10.0.1 pip-10.0.1.tar.gz pyparsing-2.2.0-py2.py3-none-any.whl pytz-2018.5-py2.py3-none-any.whl requests-2.19.1-py2.py3-none-any.whl setuptools-39.2.0-py2.py3-none-any.whl six-1.11.0-py2.py3-none-any.whl snowballstemmer-1.2.1-py2.py3-none-any.whl sphinxcontrib_websupport-1.1.0-py2.py3-none-any.whl typing-3.6.4-py2-none-any.whl urllib3-1.23-py2.py3-none-any.whl
author John Rouillard <rouilj@ieee.org>
date Sun, 27 Mar 2022 13:57:04 -0400
parents 0942fe89e82e
children
line wrap: on
line source

# Copyright (c) 2003 Richard Jones (richard@mechanicalcat.net)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included in
#   all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

import re

# regular expression thanks to: http://www.regular-expressions.info/email.html
# this is the "99.99% solution for syntax only".
email_regexp = (r"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*", r"(localhost|(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9]))")
email_rfc = re.compile('^' + email_regexp[0] + '@' + email_regexp[1] + '$', re.IGNORECASE)
email_local = re.compile('^' + email_regexp[0] + '$', re.IGNORECASE)

def valid_address(address):
    ''' If we see an @-symbol in the address then check against the full
        RFC syntax. Otherwise it is a local-only address so only check
        the local part of the RFC syntax.
    '''
    if '@' in address:
        return email_rfc.match(address)
    else:
        return email_local.match(address)

def get_addresses(user):
    ''' iterate over all known addresses in a newvalues dict
        this takes of the address/alterate_addresses handling
    '''
    if 'address' in user:
        yield user['address']
    if user.get('alternate_addresses', None):
        for address in user['alternate_addresses'].split('\n'):
            yield address

def audit_user_fields(db, cl, nodeid, newvalues):
    ''' Make sure user properties are valid.

        - email address is syntactically valid
        - email address is unique
        - roles specified exist
        - timezone is valid
    '''

    for address in get_addresses(newvalues):
        if not valid_address(address):
            raise ValueError('Email address syntax is invalid "%s"'%address)

        check_main = db.user.stringFind(address=address)
        # make sure none of the alts are owned by anyone other than us (x!=nodeid)
        check_alts = [x for x in db.user.filter(None, {'alternate_addresses' : address}) if x != nodeid]
        if check_main or check_alts:
            raise ValueError('Email address %s already in use' % address)

    newroles = newvalues.get('roles')
    if newroles:
        for rolename in [r.lower().strip() for r in newroles.split(',')]:
            if rolename and rolename not in db.security.role:
                raise ValueError('Role "%s" does not exist'%rolename)

    tz = newvalues.get('timezone', None)
    if tz:
        # if they set a new timezone validate the timezone by attempting to
        # use it before we store it to the db.
        import roundup.date
        import datetime
        try:
            TZ = roundup.date.get_timezone(tz)
            dt = datetime.datetime.now()
            local = TZ.localize(dt).utctimetuple()
        except IOError:
            raise ValueError('Timezone "%s" does not exist' % tz)
        except ValueError:
            raise ValueError('Timezone "%s" exceeds valid range [-23...23]' % tz)

def init(db):
    # fire before changes are made
    db.user.audit('set', audit_user_fields)
    db.user.audit('create', audit_user_fields)

# vim: sts=4 sw=4 et si

Roundup Issue Tracker: http://roundup-tracker.org/