Permalink
Browse files

Add option to activate network access in the usersuite

  • Loading branch information...
ibot3 committed Oct 11, 2018
1 parent ef6de5b commit cccf9c2460a2f6b105460df9a3fbfb00f07dca21
@@ -14,7 +14,7 @@
from sipa.config.default import MEMBERSHIP_CONTRIBUTION
from sipa.forms import ContactForm, ChangeMACForm, ChangeMailForm, \
ChangePasswordForm, flash_formerrors, HostingForm, DeleteMailForm, \
ChangeUseCacheForm, PaymentForm
ChangeUseCacheForm, PaymentForm, ActivateNetworkAccessForm
from sipa.mail import send_usersuite_contact_mail
from sipa.utils import password_changeable
from sipa.model.exceptions import DBQueryEmpty, LDAPConnectionError, \
@@ -308,6 +308,43 @@ def change_mac():
return render_template('usersuite/change_mac.html', form=form)


@bp_usersuite.route("/activate-network-access", methods=['GET', 'POST'])
@login_required
def activate_network_access():
"""As user, activate your network access
"""
form = ActivateNetworkAccessForm()

if form.validate_on_submit():
password = form.password.data
mac = form.mac.data
birthdate = form.birthdate.data
host_name = form.host_name.data

try:
with current_user.tmp_authentication(password):
current_user.activate_network_access(password, mac, birthdate, host_name)
except PasswordInvalid:
flash(gettext("Passwort war inkorrekt!"), "error")
except MacAlreadyExists:
flash(gettext("MAC-Adresse ist bereits in Verwendung!"), "error")
else:
logger.info('Successfully activated network access',
extra={'data': {'mac': mac, 'birthdate': birthdate, 'host_name': host_name},
'tags': {'rate_critical': True}})

flash(gettext("Netzwerkzugang wurde aktiviert!"), 'success')
flash(gettext("Es kann bis zu 10 Minuten dauern, "
"bis der Netzwerkzugang funktioniert."), 'info')

return redirect(url_for('.index'))

elif form.is_submitted():
flash_formerrors(form)

return render_template('usersuite/activate_network_access.html', form=form)


@bp_usersuite.route("/change_use_cache", methods=['GET', 'POST'])
@login_required
def change_use_cache():
@@ -7,9 +7,9 @@
from flask_wtf import FlaskForm
from werkzeug.local import LocalProxy
from wtforms import (BooleanField, HiddenField, PasswordField, SelectField,
StringField, TextAreaField, RadioField, IntegerField)
StringField, TextAreaField, RadioField, IntegerField, DateField)
from wtforms.validators import (AnyOf, DataRequired, Email, EqualTo,
MacAddress, Regexp, ValidationError, NumberRange)
MacAddress, Regexp, ValidationError, NumberRange, Optional)

from sipa.backends.extension import backends

@@ -178,6 +178,23 @@ class ChangeMACForm(FlaskForm):
validators=[DataRequired("MAC-Adresse nicht angegeben!"),
MacAddress("MAC ist nicht in gültigem Format!"),
require_unicast_mac])
class ActivateNetworkAccessForm(FlaskForm):
password = PasswordField(
label=lazy_gettext("Passwort"),
validators=[DataRequired(lazy_gettext("Passwort nicht angegeben!"))])
mac = StrippedStringField(
label=lazy_gettext("MAC-Adresse"),
validators=[DataRequired(lazy_gettext("MAC-Adresse nicht angegeben!")),
MacAddress(lazy_gettext("MAC ist nicht in gültigem Format!")),
require_unicast_mac],
description="XX:XX:XX:XX:XX:XX")
birthdate = DateField(label=lazy_gettext("Geburtsdatum"),
validators=[DataRequired(lazy_gettext("Geburtsdatum nicht angegeben!"))],
description="YYYY-MM-DD (z.B. 1995-10-23)")
host_name = StringField(label=lazy_gettext("Gerätename (Optional)"),
validators=[Regexp(regex="^[a-zA-Z0-9 ]+", message=u"Gerätename ist ungültig"),
Optional()],
description="TL-WR841N, MacBook, FritzBox, PC, Laptop, o.Ä.")


class ChangeUseCacheForm(FlaskForm):
@@ -23,6 +23,10 @@ class MacAlreadyExists(Exception):
pass


class NetworkAccessAlreadyActive(Exception):
pass


class DBQueryEmpty(Exception):
pass

@@ -38,6 +38,10 @@ def change_mac(self, user_id, password, interface_id, new_mac):
return self.post('user/{}/change-mac/{}'.format(user_id, interface_id),
data={'password': password, 'mac': new_mac})

def activate_network_access(self, user_id, password, mac, birthdate, host_name):
return self.post('user/{}/activate-network-access'.format(user_id),
data={'password': password, 'mac': mac, 'birthdate': birthdate, 'host_name': host_name})

def get(self, url, params=None):
request_function = partial(requests.get, params=params or {})
return self._do_api_call(request_function, url)
@@ -6,7 +6,7 @@
from sipa.model.fancy_property import active_prop, connection_dependent, \
unsupported_prop
from sipa.model.misc import PaymentDetails
from sipa.model.exceptions import UserNotFound, PasswordInvalid, MacAlreadyExists
from sipa.model.exceptions import UserNotFound, PasswordInvalid, MacAlreadyExists, NetworkAccessAlreadyActive
from .api import PycroftApi

from flask_login import AnonymousUserMixin
@@ -118,6 +118,26 @@ def mac(self):
return {'value': ", ".join(i['mac'] for i in self._interfaces),
'tmp_readonly': len(self._interfaces) != 1}

@active_prop
@connection_dependent
def network_access_active(self):
return {'value': gettext("Aktiviert") if len(self._interfaces) > 0 else gettext("Nicht aktiviert"),
'tmp_readonly': len(self._interfaces) > 0}

@network_access_active.setter
def network_access_active(self, value):
pass

def activate_network_access(self, password, mac, birthdate, host_name):
status, result = api.activate_network_access(self._id, password, mac, birthdate, host_name)

if status == 401:
raise PasswordInvalid
elif status == 400:
raise MacAlreadyExists
elif status == 412:
raise NetworkAccessAlreadyActive

@mac.setter
def mac(self, new_mac):
# if this has been reached despite `tmp_readonly`, this is a bug.
@@ -16,12 +16,18 @@ <h2 id="hot-buttons">{{ _("Häufige Aktionen") }}</h2>
<span class="glyphicon glyphicon-pencil"></span>
{{ _("MAC-Adresse ändern") }}
</a>
{% else %}
{% elif not current_user.network_access_active.capabilities.edit %}
<a class="btn btn-default" disabled title="{{ _('Nicht unterstützt') }}">
<span class="glyphicon glyphicon-pencil"></span>
{{ _("MAC-Adresse ändern") }}
</a>
{% endif %}
{% if current_user.network_access_active.capabilities.edit %}
<a href="{{ url_for('.activate_network_access') }}" class="btn btn-default">
<span class="glyphicon glyphicon-off"></span>
{{ _("Netzwerkanschluss aktivieren") }}
</a>
{% endif %}
<a href="{{ url_for('.contact') }}" class="btn btn-default">
<span class="glyphicon glyphicon-question-sign"></span>
{{ _("Kontaktformular") }}
@@ -0,0 +1,10 @@
{% extends "base.html" %}#
{% import "macros/forms.html" as forms %}
{% set page_title = _("Netzwerkanschluss aktivieren") %}

{% block content %}
{% call forms.render(form=form, form_id="activate-network-access-form", cancel_to=url_for('.index')) -%}

{%-endcall %}

{% endblock %}

0 comments on commit cccf9c2

Please sign in to comment.