'Ensure that a valid Galaxy session exists and is available as
trans.session (part of initialization)
Support for universe_session and universe_user cookies has been
removed as of 31 Oct 2008.'

def _ensure_valid_session(self, session_cookie, create=True):

    secure_id = self.get_cookie(name=session_cookie)
    galaxy_session = None
    prev_galaxy_session = None
    user_for_new_session = None
    invalidate_existing_session = False
    galaxy_session_requires_flush = False
    if secure_id:
        session_key = self.security.decode_guid(secure_id)
        try:
            session_key = session_key.encode('utf8')
        except UnicodeDecodeError:
            session_key = None
        if session_key:
            galaxy_session = self.sa_session.query(self.app.model.GalaxySession).filter(and_((self.app.model.GalaxySession.table.c.session_key == session_key), (self.app.model.GalaxySession.table.c.is_valid == true()))).options(joinedload('user')).first()
    if self.app.config.use_remote_user:
        remote_user_email = self.environ.get(self.app.config.remote_user_header, None)
        if galaxy_session:
            if (remote_user_email and (galaxy_session.user is None)):
                galaxy_session.user = self.get_or_create_remote_user(remote_user_email)
                galaxy_session_requires_flush = True
            elif (remote_user_email and (galaxy_session.user.email != remote_user_email) and ((not self.app.config.allow_user_impersonation) or (remote_user_email not in self.app.config.admin_users_list))):
                invalidate_existing_session = True
                user_for_new_session = self.get_or_create_remote_user(remote_user_email)
                log.warning("User    logged    in    as    '%s'    externally,    but    has    a    cookie    as    '%s'    invalidating    session", remote_user_email, galaxy_session.user.email)
        elif remote_user_email:
            user_for_new_session = self.get_or_create_remote_user(remote_user_email)
        if ((galaxy_session and (galaxy_session.user is None)) and (user_for_new_session is None)):
            raise Exception('Remote    Authentication    Failure    -    user    is    unknown    and/or    not    supplied.')
    elif ((galaxy_session is not None) and galaxy_session.user and galaxy_session.user.external):
        invalidate_existing_session = True
        log.warning("User    '%s'    is    an    external    user    with    an    existing    session,    invalidating    session    since    external    auth    is    disabled", galaxy_session.user.email)
    elif ((galaxy_session is not None) and (galaxy_session.user is not None) and galaxy_session.user.deleted):
        invalidate_existing_session = True
        log.warning(("User    '%s'    is    marked    deleted,    invalidating    session" % galaxy_session.user.email))
    if invalidate_existing_session:
        prev_galaxy_session = galaxy_session
        prev_galaxy_session.is_valid = False
        galaxy_session = None
    if (galaxy_session is None):
        galaxy_session = self.__create_new_session(prev_galaxy_session, user_for_new_session)
        galaxy_session_requires_flush = True
        self.galaxy_session = galaxy_session
        self.__update_session_cookie(name=session_cookie)
    else:
        self.galaxy_session = galaxy_session
    if galaxy_session_requires_flush:
        self.sa_session.add(galaxy_session)
        if prev_galaxy_session:
            self.sa_session.add(prev_galaxy_session)
        self.sa_session.flush()
    if invalidate_existing_session:
        self.get_or_create_default_history()
