'Login a new user (possibly newly created)
- create a new session
- associate new session with user
- if old session had a history and it was not associated with a user, associate it with the new session,
otherwise associate the current session\'s history with the user
- add the disk usage of the current session to the user\'s total disk usage'

def handle_user_login(self, user):

    prev_galaxy_session = self.galaxy_session
    prev_galaxy_session.is_valid = False
    self.galaxy_session = self.__create_new_session(prev_galaxy_session, user)
    if (self.webapp.name == 'galaxy'):
        cookie_name = 'galaxysession'
        history = None
        try:
            users_last_session = user.galaxy_sessions[0]
            last_accessed = True
        except:
            users_last_session = None
            last_accessed = False
        if (prev_galaxy_session.current_history and (not prev_galaxy_session.current_history.deleted) and prev_galaxy_session.current_history.datasets):
            if ((prev_galaxy_session.current_history.user is None) or (prev_galaxy_session.current_history.user == user)):
                history = prev_galaxy_session.current_history
                if (prev_galaxy_session.user is None):
                    for hda in history.datasets:
                        user.adjust_total_disk_usage(hda.quota_amount(user))
        elif self.galaxy_session.current_history:
            history = self.galaxy_session.current_history
        if ((not history) and users_last_session and users_last_session.current_history and (not users_last_session.current_history.deleted)):
            history = users_last_session.current_history
        elif (not history):
            history = self.get_history(create=True, most_recent=True)
        if (history not in self.galaxy_session.histories):
            self.galaxy_session.add_history(history)
        if (history.user is None):
            history.user = user
        self.galaxy_session.current_history = history
        if (not last_accessed):
            self.app.security_agent.history_set_default_permissions(history, dataset=True, bypass_manage_permission=True)
        self.sa_session.add_all((prev_galaxy_session, self.galaxy_session, history))
    else:
        cookie_name = 'galaxycommunitysession'
        self.sa_session.add_all((prev_galaxy_session, self.galaxy_session))
    self.sa_session.flush()
    self.__update_session_cookie(name=cookie_name)
