summaryrefslogtreecommitdiff
path: root/jimbrella
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2021-10-27 21:52:23 +0800
committerFrederick Yin <fkfd@fkfd.me>2021-10-27 21:52:23 +0800
commit0e482d22fc79e80eef0511326d93b1e17d453de5 (patch)
treef5f2dbdf8d1288166264e97d2d3b23494f03bd3a /jimbrella
parente53830b4bf7c0f7b7999860e3a2cdc03a03544c7 (diff)
Users: csv table for users
Diffstat (limited to 'jimbrella')
-rw-r--r--jimbrella/users.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/jimbrella/users.py b/jimbrella/users.py
new file mode 100644
index 0000000..c68908f
--- /dev/null
+++ b/jimbrella/users.py
@@ -0,0 +1,74 @@
+import csv
+from .csv_table import CsvTable
+from .lockfile import Lockfile
+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
+ self.lockfile = Lockfile(self.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)