stages/user_login: Allow changing of session duration

This commit is contained in:
Jens Langhammer 2020-07-04 15:20:45 +02:00
parent b26882a450
commit e5165abf04
7 changed files with 42 additions and 2 deletions

View File

@ -14,6 +14,7 @@ class UserLoginStageSerializer(ModelSerializer):
fields = [
"pk",
"name",
"session_duration",
]

View File

@ -10,7 +10,7 @@ class UserLoginStageForm(forms.ModelForm):
class Meta:
model = UserLoginStage
fields = ["name"]
fields = ["name", "session_duration"]
widgets = {
"name": forms.TextInput(),
}

View File

@ -0,0 +1,21 @@
# Generated by Django 3.0.7 on 2020-07-04 13:05
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("passbook_stages_user_login", "0001_initial"),
]
operations = [
migrations.AddField(
model_name="userloginstage",
name="session_duration",
field=models.PositiveIntegerField(
default=0,
help_text="Determines how long a session lasts, in seconds. Default of 0 means that the sessions lasts until the browser is closed.",
),
),
]

View File

@ -1,4 +1,5 @@
"""login stage models"""
from django.db import models
from django.utils.translation import gettext_lazy as _
from passbook.flows.models import Stage
@ -7,6 +8,14 @@ from passbook.flows.models import Stage
class UserLoginStage(Stage):
"""Attaches the currently pending user to the current session."""
session_duration = models.PositiveIntegerField(
default=0,
help_text=_(
"Determines how long a session lasts, in seconds. Default of 0 means"
" that the sessions lasts until the browser is closed."
),
)
type = "passbook.stages.user_login.stage.UserLoginStageView"
form = "passbook.stages.user_login.forms.UserLoginStageForm"

View File

@ -32,9 +32,11 @@ class UserLoginStageView(StageView):
self.executor.plan.context[PLAN_CONTEXT_PENDING_USER],
backend=backend,
)
self.request.session.set_expiry(self.executor.current_stage.session_duration)
LOGGER.debug(
"Logged in",
user=self.executor.plan.context[PLAN_CONTEXT_PENDING_USER],
flow_slug=self.executor.flow.slug,
session_duration=self.executor.current_stage.session_duration,
)
return self.executor.stage_ok()

View File

@ -99,5 +99,5 @@ class TestUserLoginStage(TestCase):
def test_form(self):
"""Test Form"""
data = {"name": "test"}
data = {"name": "test", "session_duration": 0}
self.assertEqual(UserLoginStageForm(data).is_valid(), True)

View File

@ -6925,6 +6925,13 @@ definitions:
title: Name
type: string
minLength: 1
session_duration:
title: Session duration
description: Determines how long a session lasts, in seconds. Default of 0
means that the sessions lasts until the browser is closed.
type: integer
maximum: 2147483647
minimum: 0
UserLogoutStage:
required:
- name