Exemple #1
0
    def get_session(self, request):
        user = get_user(request)
        session_key = "current"
        server_time = now()
        if isinstance(user, AnonymousUser):
            return {
                "id": session_key,
                "username": "",
                "full_name": "",
                "user_id": None,
                "facility_id": getattr(Facility.get_default_facility(), "id",
                                       None),
                "kind": ["anonymous"],
                "error": "200",
                "server_time": server_time,
            }
        # Set last activity on session to the current time to prevent session timeout
        # Only do this for logged in users, as anonymous users cannot get logged out!
        request.session["last_session_request"] = int(time.time())
        # Default to active, only assume not active when explicitly set.
        active = True if request.GET.get("active", "true") == "true" else False

        session = {
            "id": session_key,
            "username": user.username,
            "full_name": user.full_name,
            "user_id": user.id,
            "can_manage_content": user.can_manage_content,
            "server_time": server_time,
        }

        roles = list(
            Role.objects.filter(user_id=user.id).values_list(
                "kind", flat=True).distinct())

        if roles:
            session.update({
                "facility_id": user.facility_id,
                "kind": roles,
                "error": "200"
            })
        else:
            session.update({
                "facility_id": user.facility_id,
                "kind": ["learner"],
                "error": "200"
            })

        if user.is_superuser:
            session["kind"].insert(0, "superuser")

        if active:
            UserSessionLog.update_log(user)

        return session
Exemple #2
0
    def get_session_response(self, request):
        user = request.user
        session_key = "current"
        server_time = now()
        session = user.session_data
        session.update({"id": session_key, "server_time": server_time})

        visitor_cookie_expiry = datetime.utcnow() + timedelta(days=365)

        if isinstance(user, AnonymousUser):
            response = Response(session)
            if not request.COOKIES.get("visitor_id"):
                visitor_id = str(uuid4())
                response.set_cookie(
                    "visitor_id", visitor_id, expires=visitor_cookie_expiry
                )
            else:
                response.set_cookie(
                    "visitor_id",
                    request.COOKIES.get("visitor_id"),
                    expires=visitor_cookie_expiry,
                )
            return response
        # Set last activity on session to the current time to prevent session timeout
        # Only do this for logged in users, as anonymous users cannot get logged out!
        request.session["last_session_request"] = int(time.time())
        # Default to active, only assume not active when explicitly set.
        active = True if request.GET.get("active", "true") == "true" else False

        # Can only record user session log data for FacilityUsers.
        if active and isinstance(user, FacilityUser):
            UserSessionLog.update_log(user)

        response = Response(session)
        if not request.COOKIES.get("visitor_id"):
            visitor_id = str(uuid4())
            response.set_cookie("visitor_id", visitor_id, expires=visitor_cookie_expiry)
        else:
            response.set_cookie(
                "visitor_id",
                request.COOKIES.get("visitor_id"),
                expires=visitor_cookie_expiry,
            )
        return response
Exemple #3
0
    def get_session(self, request):
        user = request.user
        session_key = "current"
        server_time = now()
        session = user.session_data
        session.update({"id": session_key, "server_time": server_time})
        if isinstance(user, AnonymousUser):
            return session
        # Set last activity on session to the current time to prevent session timeout
        # Only do this for logged in users, as anonymous users cannot get logged out!
        request.session["last_session_request"] = int(time.time())
        # Default to active, only assume not active when explicitly set.
        active = True if request.GET.get("active", "true") == "true" else False

        # Can only record user session log data for FacilityUsers.
        if active and isinstance(user, FacilityUser):
            UserSessionLog.update_log(user)

        return session