From 8a116c4c22b16513992b278c8b57f7d3eb7aa3eb Mon Sep 17 00:00:00 2001 From: Vincent Post Date: Fri, 7 Aug 2020 20:48:29 +0200 Subject: Rewrote dummy user backend to not need deepcopy --- accounts/backend/user/dummy.py | 72 ++++++++++++++++++++++++++++-------------- 1 file 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): """ -- cgit v1.2.3-1-g7c22