add hook for Factors to show user settings. closes #5

This commit is contained in:
Jens Langhammer 2019-02-25 13:20:07 +01:00
parent da5568b571
commit 986fed3e7c
5 changed files with 41 additions and 7 deletions

View File

@ -74,6 +74,12 @@ class Factor(PolicyModel):
type = ''
form = ''
def has_user_settings(self):
"""Entrypoint to integrate with User settings. Can either return False if no
user settings are available, or a tuple or string, string, string where the first string
is the name the item has, the second string is the icon and the third is the view-name."""
return False
def __str__(self):
return "Factor %s" % self.slug
@ -85,6 +91,9 @@ class PasswordFactor(Factor):
type = 'passbook.core.auth.factors.password.PasswordFactor'
form = 'passbook.core.forms.factors.PasswordFactorForm'
def has_user_settings(self):
return _('Change Password'), 'pficon-key', 'passbook_core:user-change-password'
def __str__(self):
return "Password Factor %s" % self.slug

View File

@ -2,25 +2,28 @@
{% load i18n %}
{% load is_active %}
{% load passbook_user_settings %}
{% block content %}
<div class="container">
<div class="col-md-3 ">
<div class="nav-category">
<h2>{% trans 'User Profile'%}</h2>
<h2>{% trans 'User Settings'%}</h2>
<ul class="nav nav-pills nav-stacked">
<li class="{% is_active 'passbook_core:user-settings' %}">
<a href="{% url 'passbook_core:user-settings' %}">
<i class="fa fa-desktop"></i> {% trans 'Details' %}
<i class="fa pficon-edit"></i> {% trans 'Details' %}
</a>
</li>
<li class="{% is_active 'passbook_core:user-change-password' %}">
<a href="{% url 'passbook_core:user-change-password' %}">
<i class="pficon pficon-locked"></i> {% trans 'Change Password' %}
<li class="nav-divider"></li>
{% user_factors as uf %}
{% for name, icon, link in uf %}
<li class="{% is_active link %}">
<a href="{% url link %}">
<i class="{{ icon }}"></i> {{ name }}
</a>
</li>
<li><a href="#"><i class="fa fa-file-text-o"></i> Journal</a></li>
<li><a href="#"><i class="fa fa-cloud"></i> Storage</a></li>
{% endfor %}
</ul>
</div>
</div>

View File

View File

@ -0,0 +1,19 @@
"""passbook user settings template tags"""
from django import template
from passbook.core.models import Factor
register = template.Library()
@register.simple_tag(takes_context=True)
def user_factors(context):
"""Return list of all factors which apply to user"""
user = context.get('request').user
_all_factors = Factor.objects.filter(enabled=True).order_by('order').select_subclasses()
matching_factors = []
for factor in _all_factors:
_link = factor.has_user_settings()
if factor.passes(user) and _link:
matching_factors.append(_link)
return matching_factors

View File

@ -15,6 +15,9 @@ class OTPFactor(Factor):
type = 'passbook.otp.factors.OTPFactor'
form = 'passbook.otp.forms.OTPFactorForm'
def has_user_settings(self):
return _('OTP'), 'pficon-locked', 'passbook_otp:otp-user-settings'
def __str__(self):
return "OTP Factor %s" % self.slug