'Log the user in
- common function called by login() & register()'

def login_user(self, user):

    db = current.db
    deployment_settings = current.deployment_settings
    request = current.request
    session = current.session
    settings = self.settings
    req_vars = request.vars
    if (not user.utc_offset):
        user.utc_offset = session.s3.utc_offset
    session.auth = Storage(user=user, last_visit=request.now, expiration=((req_vars.get('remember', False) and settings.long_expiration) or settings.expiration), remember=req_vars.has_key('remember'), hmac_key=web2py_uuid())
    self.user = user
    self.s3_set_roles()
    self.set_cookie()
    language = user.language
    current.T.force(language)
    session.s3.language = language
    session.confirmation = self.messages.logged_in
    utable = settings.table_user
    db((utable.id == self.user.id)).update(timestmp=request.utcnow)
    if (deployment_settings.get_auth_set_presence_on_login() and req_vars.has_key('auth_user_clientlocation') and req_vars.get('auth_user_clientlocation')):
        position = req_vars.get('auth_user_clientlocation').split('|', 3)
        userlat = float(position[0])
        userlon = float(position[1])
        accuracy = (float(position[2]) / 1000)
        closestpoint = 0
        closestdistance = 0
        gis = current.gis
        locations = gis.get_features_in_radius(userlat, userlon, accuracy)
        ignore_levels_for_presence = deployment_settings.get_auth_ignore_levels_for_presence()
        greatCircleDistance = gis.greatCircleDistance
        for location in locations:
            if (location.level not in ignore_levels_for_presence):
                if (closestpoint != 0):
                    currentdistance = greatCircleDistance(closestpoint.lat, closestpoint.lon, location.lat, location.lon)
                    if (currentdistance < closestdistance):
                        closestpoint = location
                        closestdistance = currentdistance
                else:
                    closestpoint = location
        s3tracker = S3Tracker()
        person_id = self.s3_logged_in_person()
        if ((closestpoint == 0) and deployment_settings.get_auth_create_unknown_locations()):
            newpoint = {'lat': userlat, 'lon': userlon, 'name': 'Waypoint'}
            closestpoint = current.s3db.gis_location.insert(**newpoint)
            s3tracker(db.pr_person, person_id).set_location(closestpoint, timestmp=request.utcnow)
        elif (closestpoint != 0):
            s3tracker(db.pr_person, person_id).set_location(closestpoint, timestmp=request.utcnow)
