summaryrefslogtreecommitdiff
path: root/jimbrella/users.py
blob: 4a6348e19e8bbfed223b92b64b4aa8cce0e3c9fd (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
from .csv_table import CsvTable
from .exceptions import UsernameTakenError


class Users(CsvTable):
    """A table of users, including admins.

    A user must register through the interface provided by JImbrella to be one
    of the users in this table. Those who borrow umbrellas via jForm are not
    stored here.

    The file format is csv. Each user takes one row.

    Columns:
    - username      | identifier Unicode string. must be unique.
    - password      | salted and hashed password.
    - role          | one of ("admin", "user"). expect additions.
    - status        | one of ("normal", "denied"). expect additions.
    - language      | one of ("en-US", 'zh-CN").
    - realname      | (optional) legal name of user.
    - id            | (optional) student/faculty ID.
    - phone         | (optional) phone number.
    - email         | (optional) email address.
    """

    def __init__(self, path):
        self.path = path
        super().__init__(
            path,
            [
                {"name": col}
                for col in [
                    "username",
                    "password",
                    "role",
                    "status",
                    "language",
                    "realname",
                    "id",
                    "phone",
                    "email",
                ]
            ],
        )

    def read(self):
        """Alias for `_read.`"""
        return self._read()

    def register(self, username, password, language):
        """Create a new user.

        If username is already taken, raise UsernameTakenError.
        """
        users = self._read()
        usernames = [user["username"] for user in users]
        if username in usernames:
            raise UsernameTakenError(username)

        user = {
            "username": username,
            "password": password,
            "role": "user",
            "status": "normal",
            "language": language,
            "realname": "",
            "id": "",
            "phone": "",
            "email": "",
        }
        self._append(user)

    def find(self, username) -> dict:
        """Find a user and return it in its `dict` structure."""

        users = self._read()
        for user in users:
            if user["username"] == username:
                return user

        return None