summaryrefslogtreecommitdiffstats
path: root/accounts/backend/user/dummy.py
blob: 573ac853304583ad4b58bf19c54e0614509b42fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: utf-8 -*-


from copy import deepcopy
from fnmatch import fnmatch

from . import Backend
from accounts.models import Account


def _match_filter(account, filters, wildcard):
    if filters is None:
        return True

    for key in filters:
        if not hasattr(account, key):
            return False

        if wildcard:
            if not fnmatch(getattr(account, key), filters[key]):
                return False
        else:
            if getattr(account, key) != filters[key]:
                return False

    return True


class DummyBackend(Backend):
    """
    This is a simple user backend that persists the users in a simple list.

    The users are stored only in memory and during initialization two dummy
    users (test and test2) are created.
    """

    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

    def auth(self, username, password):
        """
        Tries to authenticate a user with a given password. If the
        authentication is successful an Account object will be returned.
        """
        acc = self.get_by_uid(username)
        if acc.password != password:
            raise self.InvalidPasswordError("Invalid 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

    def _store(self, account):
        self._storage.append(deepcopy(account))

    def update(self, account, as_admin=False):
        """
        Updates account information like passwords or email.
        """
        stored_account = self.get_by_uid(account.uid)
        if not as_admin:
            if stored_account.password != 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))

    def delete(self, account, as_admin=False):
        """
        Deletes an account permanently.
        """
        stored_account = self.get_by_uid(account.uid)
        if not as_admin:
            if stored_account.password != account.password:
                raise self.InvalidPasswordError("Invalid password")

        self._storage = [acc for acc in self._storage if acc.uid != account.uid]

    def _get_next_uidNumber(self):
        value = self._next_uidNumber
        self._next_uidNumber += 1
        return value