Skip to content

Commit

Permalink
added managing mpsk clients
Browse files Browse the repository at this point in the history
  • Loading branch information
agmes4 committed Dec 22, 2024
1 parent 74fe3fd commit ebe76c1
Showing 1 changed file with 49 additions and 14 deletions.
63 changes: 49 additions & 14 deletions sipa/model/pycroft/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from __future__ import annotations

import json
import logging
from datetime import date
from typing import List
Expand Down Expand Up @@ -28,6 +30,8 @@
from werkzeug.local import LocalProxy
from werkzeug.http import parse_date

from ..mspk_client import MPSKClientEntry

logger = logging.getLogger(__name__)

api: PycroftApi = LocalProxy(lambda: current_app.extensions['pycroft_api'])
Expand Down Expand Up @@ -322,7 +326,51 @@ def membership_end_date(self) -> ActiveProperty[date | None, date | None]:

@property
def mpsks_clients(self) -> ActiveProperty[str | None, str | None]:
return ActiveProperty (name="mpsks_clients")
return ActiveProperty(name="mpsks_clients", value=self.config["mpsks_clients"], capabilities=Capabilities(edit=True, delete=False),)

def change_mpsks_clients(self, mac, name, mpsk_id, password: str):
for i, el in enumerate(self.config["mpsks_clients"]):
if mpsk_id == el.id:
el.name = name
el.mac = mac
break
else:
raise ValueError(f"mac: {mac} not found for user")

def add_mpsks_client(self, name, mac, password):
status, response = api.add_mpsk(
self.user_data.id,
password,
mac,
name)

if status == 400:
raise ValueError(f"Execceds maximum clients")
elif status == 409:
raise MacAlreadyExists
elif status == 422:
raise ValueError

try:
response = json.loads(response)
except json.decoder.JSONDecodeError:
raise ValueError(f"Invalid response from {response}")

if ('name', 'mac', 'id') in response.keys():
return MPSKClientEntry(name=response.get('name'), mac=response.get('mac'), id=response.get('id'))
else:
raise ValueError(f"Invalid response from {response}")


def delete_mpsks_client(self, mpsk_id, password):
status, response = api.delete_mpsk(
self.user_data.id,
password,
mpsk_id,
)
if status == 400 or status == 401:
raise ValueError(f'Mpsk client not found for user: {mpsk_id}')


@property
def is_member(self) -> bool:
Expand Down Expand Up @@ -428,16 +476,3 @@ def last_update(self):
def history(self):
return self._transactions

class MPSK_Client:

def __init__(self, name="", mac=""):
self._name = name
self._mac = mac

@property
def name(self):
return self._name

@property
def mac(self):
return self._mac

0 comments on commit ebe76c1

Please sign in to comment.