summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Post <cent@spline.de>2020-08-07 20:48:29 +0200
committerJonah BrĂ¼chert <jbb@kaidan.im>2023-05-22 00:28:12 +0200
commit8a116c4c22b16513992b278c8b57f7d3eb7aa3eb (patch)
tree8411e09da9d16b4f31475b94e79dfe1b472de154
parent9a6c6644f3cc7bd4c52491b1e1675909948bcdb7 (diff)
downloadweb-8a116c4c22b16513992b278c8b57f7d3eb7aa3eb.tar.gz
web-8a116c4c22b16513992b278c8b57f7d3eb7aa3eb.tar.bz2
web-8a116c4c22b16513992b278c8b57f7d3eb7aa3eb.zip
Rewrote dummy user backend to not need deepcopy
-rw-r--r--accounts/backend/user/dummy.py72
1 files changed, 48 insertions, 24 deletions
diff --git a/accounts/backend/user/dummy.py b/accounts/backend/user/dummy.py
index c1dbcb4..8c54dac 100644
--- a/accounts/backend/user/dummy.py
+++ b/accounts/backend/user/dummy.py
@@ -1,4 +1,3 @@
-from copy import deepcopy
from fnmatch import fnmatch
from . import Backend
@@ -34,12 +33,37 @@ class DummyBackend(Backend):
def __init__(self, app):
super(DummyBackend, self).__init__(app)
- self._storage = [
- Account('test', 'test@accounts.spline.de', password='test', uidNumber=1),
- Account('test2', 'test2@accounts.spline.de', password='test2', uidNumber=2),
- Account('admin', 'admin@accounts.spline.de', password='admin', uidNumber=3),
- ]
- self._next_uidNumber = 3
+ self._storage = {
+ "test": {
+ "uidNumber": 1,
+ "mail": "test@accounts.spline.de",
+ "password": "test"
+ },
+ "test2": {
+ "uidNumber": 2,
+ "mail": "test2@accounts.spline.de",
+ "password": "test2"
+ },
+ "admin": {
+ "uidNumber": 3,
+ "mail": "admin@accounts.spline.de",
+ "password": "admin"
+ },
+ }
+
+ self._next_uidNumber = 4
+
+ def _get_accounts(self):
+ accounts = []
+ for uid, attrs in self._storage.items():
+ accounts.append(
+ Account(
+ uid,
+ attrs["mail"],
+ uidNumber=attrs["uidNumber"]
+ )
+ )
+ return accounts
def auth(self, username, password):
"""
@@ -47,24 +71,29 @@ class DummyBackend(Backend):
authentication is successful an Account object will be returned.
"""
acc = self.get_by_uid(username)
- if acc.password != password:
+ if not self._verify_password(acc, password):
raise self.InvalidPasswordError("Invalid password")
-
+ acc.password = password
return acc
def find(self, filters=None, wildcard=False):
"""
Find accounts by a given filter.
"""
- results = []
- for acc in self._storage:
- if _match_filter(acc, filters, wildcard):
- results.append(deepcopy(acc))
-
- return results
+ return [acc for acc in self._get_accounts() if _match_filter(acc, filters, wildcard)]
def _store(self, account):
- self._storage.append(deepcopy(account))
+ self._storage[account.uid] = {
+ "uidNumber": account.uidNumber,
+ "mail": account.mail,
+ "password": account.password
+ }
+
+ def _verify_password(self, account, password):
+ return password == self._storage[account.uid]["password"]
+
+ def _alter_password(self, account, password):
+ self._storage[account.uid]["password"] = password
def update(self, account, as_admin=False):
"""
@@ -72,18 +101,13 @@ class DummyBackend(Backend):
"""
stored_account = self.get_by_uid(account.uid)
if not as_admin:
- if stored_account.password != account.password:
+ if not self._verify_password(stored_account, account.password):
raise self.InvalidPasswordError("Invalid password")
if account.new_password_root:
old, new = account.new_password_root
- if old == stored_account.password:
- account.password = new
- account.new_password_root = None
-
- self._storage = [acc for acc in self._storage
- if acc.uid != account.uid]
- self._storage.append(deepcopy(account))
+ if self._verify_password(stored_account, old):
+ self._alter_password(stored_account, new)
def delete(self, account, as_admin=False):
"""