예제 #1
0
def get_dbowner_connection(procedure=None,
                           dbo_password=None,
                           dbo_account='gm-dbo'):
    if procedure is None:
        procedure = _('<restricted procedure>')

    # 1) get password for gm-dbo
    if dbo_password is None:
        dbo_password = wx.GetPasswordFromUser(message=_("""
 [%s]

This is a restricted procedure. We need the
current password for the GNUmed database owner.

Please enter the current password for <%s>:""") % (procedure, dbo_account),
                                              caption=procedure)
        if dbo_password == '':
            return None

    gmLog2.add_word2hide(dbo_password)

    # 2) connect as gm-dbo
    pool = gmConnectionPool.gmConnectionPool()
    conn = None
    try:
        conn = pool.get_dbowner_connection(readonly=False,
                                           verbose=True,
                                           dbo_password=dbo_password,
                                           dbo_account=dbo_account)
    except Exception:
        _log.exception('cannot connect')
        gmGuiHelpers.gm_show_error(
            aMessage=_('Cannot connect as the GNUmed database owner <%s>.') %
            dbo_account,
            aTitle=procedure)
        gmPG2.log_database_access(
            action='failed to connect as database owner for [%s]' % procedure)
    return conn
예제 #2
0
            map_urb_zip_country2region(urb='Kassel',
                                       zip='34119',
                                       country=None,
                                       country_code='DE'))
        print(
            map_urb_zip_country2region(urb='Kassel',
                                       zip='34119',
                                       country=None,
                                       country_code=None))

#		print map_urb_zip_country2region(urb = 'Kassel', zip = '34119', country = u'Deutschland', country_code = 'DE')
#		print map_urb_zip_country2region(urb = 'Kassel', zip = '34119', country = u'Deutschland', country_code = 'DE')
#		print map_urb_zip_country2region(urb = 'Kassel', zip = '34119', country = u'Deutschland', country_code = 'DE')
#		print map_urb_zip_country2region(urb = 'Kassel', zip = '34119', country = u'Deutschland', country_code = 'DE')

#--------------------------------------------------------
#gmPG2.get_connection()

    l, creds = gmPG2.request_login_params()
    gmConnectionPool.gmConnectionPool().credentials = creds

    test_address_exists()
    #test_create_address()
    #test_get_countries()
    #test_get_country_for_region()
    #test_delete_tag()
    #test_tag_images()
    test_get_billing_address()
    #test_map_urb_zip_region2country()
    #test_map_urb_zip_country2region()
예제 #3
0
def _async_signal_update_status(status):
    gmConnectionPool.gmConnectionPool().discard_pooled_connection_of_thread()
    wx.CallAfter(_signal_update_status, status)
예제 #4
0
def connect_to_database(max_attempts=3,
                        expected_version=None,
                        require_version=True):
    """Display the login dialog and try to log into the backend.

	- up to max_attempts times
	- returns True/False
	"""
    # force programmer to set a valid expected_version
    expected_hash = gmPG2.known_schema_hashes[expected_version]
    client_version = _cfg.get(option='client_version')
    global current_db_name
    current_db_name = 'gnumed_v%s' % expected_version

    attempt = 0

    dlg = cLoginDialog(None, -1, client_version=client_version)
    dlg.Centre(wx.BOTH)

    while attempt < max_attempts:

        _log.debug('login attempt %s of %s', (attempt + 1), max_attempts)

        connected = False

        dlg.ShowModal()
        login = dlg.panel.GetLoginInfo()
        if login is None:
            _log.info("user cancelled login dialog")
            break

        gmLog2.add_word2hide(login.password)

        # try getting a connection to verify the parameters work
        creds = gmConnectionPool.cPGCredentials()
        creds.database = login.database
        creds.host = login.host
        creds.port = login.port
        creds.user = login.user
        creds.password = login.password
        pool = gmConnectionPool.gmConnectionPool()
        pool.credentials = creds
        try:
            conn = gmPG2.get_raw_connection(verbose=True, readonly=True)
            connected = True

        except gmPG2.cAuthenticationError as e:
            attempt += 1
            _log.error("login attempt failed: %s", e)
            if attempt < max_attempts:
                if ('host=127.0.0.1' in ('%s' % e)) or ('host='
                                                        not in ('%s' % e)):
                    msg = _(
                        'Unable to connect to database:\n\n'
                        '%s\n\n'
                        "Are you sure you have got a local database installed ?\n"
                        '\n'
                        "Please retry with proper credentials or cancel.\n"
                        '\n'
                        ' (for the public and any new GNUmed data-\n'
                        '  bases the default user name and password\n'
                        '  are {any-doc, any-doc})\n'
                        '\n'
                        'You may also need to check the PostgreSQL client\n'
                        'authentication configuration in pg_hba.conf. For\n'
                        'details see:\n'
                        '\n'
                        'wiki.gnumed.de/bin/view/Gnumed/ConfigurePostgreSQL')
                else:
                    msg = _(
                        "Unable to connect to database:\n\n"
                        "%s\n\n"
                        "Please retry with proper credentials or cancel.\n"
                        "\n"
                        "For the public and any new GNUmed databases the\n"
                        "default user name and password are {any-doc, any-doc}.\n"
                        "\n"
                        'You may also need to check the PostgreSQL client\n'
                        'authentication configuration in pg_hba.conf. For\n'
                        'details see:\n'
                        '\n'
                        'wiki.gnumed.de/bin/view/Gnumed/ConfigurePostgreSQL')
                msg = msg % e
                msg = regex.sub(
                    r'password=[^\s]+',
                    'password=%s' % gmTools.u_replacement_character, msg)
                gmGuiHelpers.gm_show_error(msg, _('Connecting to backend'))
            del e
            continue

        except gmPG2.dbapi.OperationalError as exc:
            _log.exception('login attempt failed')
            gmPG2.log_pg_exception_details(exc)
            msg = _(
                "Unable to connect to database:\n\n"
                "%s\n\n"
                "Please retry another backend / user / password combination !\n"
                "\n"
                " (for the public and any new GNUmed databases\n"
                "  the default user name and password are\n"
                "  {any-doc, any-doc})\n"
                "\n") % exc
            msg = regex.sub(r'password=[^\s]+',
                            'password=%s' % gmTools.u_replacement_character,
                            msg)
            gmGuiHelpers.gm_show_error(msg, _('Connecting to backend'))
            del exc
            continue

        conn.close()

        seems_bootstrapped = gmPG2.schema_exists(schema='gm')
        if not seems_bootstrapped:
            _log.error(
                'schema [gm] does not exist - database not bootstrapped ?')
            msg = _('The database you connected to does not seem\n'
                    'to have been boostrapped properly.\n'
                    '\n'
                    'Make sure you have run the GNUmed database\n'
                    'bootstrapper tool to create a new database.\n'
                    '\n'
                    'Further help can be found on the website at\n'
                    '\n'
                    '  http://wiki.gnumed.de\n'
                    '\n'
                    'or on the GNUmed mailing list.')
            gmGuiHelpers.gm_show_error(msg, _('Verifying database'))
            connected = False
            break

        compatible = gmPG2.database_schema_compatible(version=expected_version)
        if compatible or not require_version:
            dlg.panel.save_state()

        if not compatible:
            connected_db_version = gmPG2.get_schema_version()
            msg = msg_generic % (client_version, connected_db_version,
                                 expected_version,
                                 gmTools.coalesce(login.host, '<localhost>'),
                                 login.database, login.user)
            if require_version:
                gmGuiHelpers.gm_show_error(msg + msg_fail,
                                           _('Verifying database version'))
                connected = False
                continue
            gmGuiHelpers.gm_show_info(msg + msg_override,
                                      _('Verifying database version'))

        # FIXME: make configurable
        max_skew = 1  # minutes
        if _cfg.get(option='debug'):
            max_skew = 10
        if not gmPG2.sanity_check_time_skew(tolerance=(max_skew * 60)):
            if _cfg.get(option='debug'):
                gmGuiHelpers.gm_show_warning(msg_time_skew_warn % max_skew,
                                             _('Verifying database settings'))
            else:
                gmGuiHelpers.gm_show_error(msg_time_skew_fail % max_skew,
                                           _('Verifying database settings'))
                connected = False
                continue

        sanity_level, message = gmPG2.sanity_check_database_settings()
        if sanity_level != 0:
            gmGuiHelpers.gm_show_error((msg_insanity % message),
                                       _('Verifying database settings'))
            if sanity_level == 2:
                connected = False
                continue

        gmExceptionHandlingWidgets.set_is_public_database(
            _cfg.get(option='is_public_db'))
        gmExceptionHandlingWidgets.set_helpdesk(_cfg.get(option='helpdesk'))

        gmLog2.log_multiline(logging.DEBUG,
                             message='fingerprint',
                             text=gmPG2.get_db_fingerprint(eol='\n'))

        conn = gmPG2.get_connection(
            verbose=True,
            connection_name='GNUmed-[DbListenerThread]',
            pooled=False)
        listener = gmBackendListener.gmBackendListener(conn=conn)
        break

    dlg.DestroyLater()

    return connected
예제 #5
0
def connect_to_database(max_attempts=3,
                        expected_version=None,
                        require_version=True):
    """Display the login dialog and try to log into the backend.

	- up to max_attempts times
	- returns True/False
	"""
    # force programmer to set a valid expected_version
    gmPG2.known_schema_hashes[expected_version]
    client_version = _cfg.get(option='client_version')
    global current_db_name
    current_db_name = 'gnumed_v%s' % expected_version
    attempt = 0
    dlg = cLoginDialog(None, -1, client_version=client_version)
    dlg.Centre(wx.BOTH)

    while attempt < max_attempts:
        _log.debug('login attempt %s of %s', (attempt + 1), max_attempts)
        connected = False
        dlg.ShowModal()
        login = dlg.panel.GetLoginInfo()
        if login is None:
            _log.info("user cancelled login dialog")
            break

        # obscure unconditionally, it could be a valid password
        gmLog2.add_word2hide(login.password)
        # try getting a connection to verify the parameters do work
        creds = gmConnectionPool.cPGCredentials()
        creds.database = login.database
        creds.host = login.host
        creds.port = login.port
        creds.user = login.user
        creds.password = login.password
        pool = gmConnectionPool.gmConnectionPool()
        pool.credentials = creds
        try:
            conn = gmPG2.get_raw_connection(verbose=True, readonly=True)
            _log.info('successfully connected: %s', conn)
            connected = True

        except gmPG2.cAuthenticationError as exc:
            _log.exception('login attempt failed')
            gmPG2.log_pg_exception_details(exc)
            attempt += 1
            if attempt < max_attempts:
                if ('host=127.0.0.1' in ('%s' % exc)) or ('host='
                                                          not in ('%s' % exc)):
                    msg = msg_auth_error_local
                else:
                    msg = msg_auth_error
                msg = msg % exc
                msg = regex.sub(
                    r'password=[^\s]+',
                    'password=%s' % gmTools.u_replacement_character, msg)
                gmGuiHelpers.gm_show_error(msg, _('Connecting to backend'))
            del exc
            continue

        except gmPG2.dbapi.OperationalError as exc:
            _log.exception('login attempt failed')
            gmPG2.log_pg_exception_details(exc)
            msg = msg_login_problem_generic % exc
            msg = regex.sub(r'password=[^\s]+',
                            'password=%s' % gmTools.u_replacement_character,
                            msg)
            gmGuiHelpers.gm_show_error(msg, _('Connecting to backend'))
            del exc
            continue

        conn.close()

        if not __database_is_acceptable_for_use(
                require_version=require_version,
                expected_version=expected_version,
                login=login):
            _log.info('database not suitable for use')
            connected = False
            break

        dlg.panel.save_state()
        gmExceptionHandlingWidgets.set_is_public_database(
            _cfg.get(option='is_public_db'))
        gmExceptionHandlingWidgets.set_helpdesk(_cfg.get(option='helpdesk'))
        _log.debug('establishing DB listener connection')
        conn = gmPG2.get_connection(
            verbose=True,
            connection_name='GNUmed-[DbListenerThread]',
            pooled=False)
        gmBackendListener.gmBackendListener(conn=conn)
        break

    dlg.DestroyLater()
    return connected