stages/otp_static: implement configure_flow

This commit is contained in:
Jens Langhammer 2020-09-25 12:56:14 +02:00
parent 8fa83a8d08
commit e66424cc49
5 changed files with 81 additions and 6 deletions

View File

@ -0,0 +1,52 @@
# Generated by Django 3.1.1 on 2020-09-24 20:51
import django.db.models.deletion
from django.apps.registry import Apps
from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
def create_default_setup_flow(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
Flow = apps.get_model("passbook_flows", "Flow")
FlowStageBinding = apps.get_model("passbook_flows", "FlowStageBinding")
OTPStaticStage = apps.get_model("passbook_stages_otp_static", "OTPStaticStage")
db_alias = schema_editor.connection.alias
flow, _ = Flow.objects.using(db_alias).update_or_create(
slug="default-otp-static-configure",
designation=FlowDesignation.STAGE_SETUP,
defaults={"name": "Setup Static OTP Tokens"},
)
stage = OTPStaticStage.objects.using(db_alias).update_or_create(
name="default-otp-static-configure"
)
FlowStageBinding.objects.using(db_alias).update_or_create(
target=flow, stage=stage, defaults={"order": 0}
)
class Migration(migrations.Migration):
dependencies = [
("passbook_flows", "0013_auto_20200924_1605"),
("passbook_stages_otp_static", "0001_initial"),
]
operations = [
migrations.AddField(
model_name="otpstaticstage",
name="configure_flow",
field=models.ForeignKey(
blank=True,
help_text="Flow used by an authenticated user to configure this Stage. If empty, user will not be able to configure this stage.",
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="passbook_flows.flow",
),
),
migrations.RunPython(create_default_setup_flow),
]

View File

@ -9,10 +9,10 @@ from django.views import View
from rest_framework.serializers import BaseSerializer from rest_framework.serializers import BaseSerializer
from passbook.core.types import UIUserSettings from passbook.core.types import UIUserSettings
from passbook.flows.models import Stage from passbook.flows.models import ConfigurableStage, Stage
class OTPStaticStage(Stage): class OTPStaticStage(ConfigurableStage, Stage):
"""Generate static tokens for the user as a backup.""" """Generate static tokens for the user as a backup."""
token_count = models.IntegerField(default=6) token_count = models.IntegerField(default=6)
@ -36,7 +36,11 @@ class OTPStaticStage(Stage):
@property @property
def ui_user_settings(self) -> Optional[UIUserSettings]: def ui_user_settings(self) -> Optional[UIUserSettings]:
return UIUserSettings( return UIUserSettings(
name="Static OTP", url=reverse("passbook_stages_otp_static:user-settings"), name="Static OTP",
url=reverse(
"passbook_stages_otp_static:user-settings",
kwargs={"stage_uuid": self.stage_uuid},
),
) )
def __str__(self) -> str: def __str__(self) -> str:

View File

@ -9,12 +9,28 @@
{% trans "Static One-Time Passwords" %} {% trans "Static One-Time Passwords" %}
</div> </div>
<div class="pf-c-card__body"> <div class="pf-c-card__body">
<p>
{% blocktrans with state=state|yesno:"Enabled,Disabled" %}
Status: {{ state }}
{% endblocktrans %}
{% if state %}
<i class="pf-icon pf-icon-ok"></i>
{% else %}
<i class="pf-icon pf-icon-error-circle-o"></i>
{% endif %}
</p>
<ul class="pb-otp-tokens"> <ul class="pb-otp-tokens">
{% for token in tokens %} {% for token in tokens %}
<li>{{ token.token }}</li> <li>{{ token.token }}</li>
{% endfor %} {% endfor %}
</ul> </ul>
<a href="{% url 'passbook_stages_otp_static:disable' %}" class="pf-c-button pf-m-danger">{% trans "Disable Static Tokens" %}</a> {% if not state %}
{% if stage.configure_flow %}
<a href="{% url 'passbook-flows:configure' stage_uuid=stage.stage_uuid %}" class="pf-c-button pf-m-primary">{% trans "Enable Static Tokens" %}</a>
{% endif %}
{% else %}
<a href="{% url 'passbook_stages_otp_static:disable' stage_uuid=stage.stage_uuid %}" class="pf-c-button pf-m-danger">{% trans "Disable Static Tokens" %}</a>
{% endif %}
</div> </div>
</div> </div>
{% endblock %} {% endblock %}

View File

@ -4,6 +4,8 @@ from django.urls import path
from passbook.stages.otp_static.views import DisableView, UserSettingsView from passbook.stages.otp_static.views import DisableView, UserSettingsView
urlpatterns = [ urlpatterns = [
path("settings", UserSettingsView.as_view(), name="user-settings"), path(
path("disable", DisableView.as_view(), name="disable"), "<uuid:stage_uuid>/settings/", UserSettingsView.as_view(), name="user-settings"
),
path("<uuid:stage_uuid>/disable/", DisableView.as_view(), name="disable"),
] ]

View File

@ -21,6 +21,7 @@ class UserSettingsView(LoginRequiredMixin, TemplateView):
static_devices = StaticDevice.objects.filter( static_devices = StaticDevice.objects.filter(
user=self.request.user, confirmed=True user=self.request.user, confirmed=True
) )
kwargs["state"] = static_devices.exists()
if static_devices.exists(): if static_devices.exists():
kwargs["tokens"] = StaticToken.objects.filter(device=static_devices.first()) kwargs["tokens"] = StaticToken.objects.filter(device=static_devices.first())
return kwargs return kwargs