Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/archinstall/lib/models
diff options
context:
space:
mode:
authorDaniel Girtler <blackrabbit256@gmail.com>2022-05-27 05:48:29 +1000
committerGitHub <noreply@github.com>2022-05-26 21:48:29 +0200
commit870da403e79ab50350803b45f200e0b272334989 (patch)
tree9b203a054bd10cbc73a81b4fd5fe24ef8e6f141a /archinstall/lib/models
parent353c05318ce80b8eec32031c9e14b8471b458548 (diff)
Rework user management (#1220)
* Rework users * Update user installation * Fix config serialization * Update * Update schemas and documentation * Update * Fix flake8 * Make users mypy compatible * Fix minor copy Co-authored-by: Daniel Girtler <girtler.daniel@gmail.com> Co-authored-by: Anton Hvornum <anton@hvornum.se>
Diffstat (limited to 'archinstall/lib/models')
-rw-r--r--archinstall/lib/models/users.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/archinstall/lib/models/users.py b/archinstall/lib/models/users.py
new file mode 100644
index 00000000..6052b73a
--- /dev/null
+++ b/archinstall/lib/models/users.py
@@ -0,0 +1,77 @@
+from dataclasses import dataclass
+from typing import Dict, List, Union, Any, TYPE_CHECKING
+
+if TYPE_CHECKING:
+ _: Any
+
+
+@dataclass
+class User:
+ username: str
+ password: str
+ sudo: bool
+
+ @property
+ def groups(self) -> List[str]:
+ # this property should be transferred into a class attr instead
+ # if it's every going to be used
+ return []
+
+ def json(self) -> Dict[str, Any]:
+ return {
+ 'username': self.username,
+ '!password': self.password,
+ 'sudo': self.sudo
+ }
+
+ def display(self) -> str:
+ password = '*' * len(self.password)
+ return f'{_("Username")}: {self.username:16} {_("Password")}: {password:16} sudo: {str(self.sudo)}'
+
+ @classmethod
+ def _parse(cls, config_users: List[Dict[str, Any]]) -> List['User']:
+ users = []
+
+ for entry in config_users:
+ username = entry.get('username', None)
+ password = entry.get('!password', '')
+ sudo = entry.get('sudo', False)
+
+ if username is None:
+ continue
+
+ user = User(username, password, sudo)
+ users.append(user)
+
+ return users
+
+ @classmethod
+ def _parse_backwards_compatible(cls, config_users: Dict, sudo: bool) -> List['User']:
+ if len(config_users.keys()) > 0:
+ username = list(config_users.keys())[0]
+ password = config_users[username]['!password']
+
+ if password:
+ return [User(username, password, sudo)]
+
+ return []
+
+ @classmethod
+ def parse_arguments(
+ cls,
+ config_users: Union[List[Dict[str, str]], Dict[str, str]],
+ config_superusers: Union[List[Dict[str, str]], Dict[str, str]]
+ ) -> List['User']:
+ users = []
+
+ # backwards compability
+ if isinstance(config_users, dict):
+ users += cls._parse_backwards_compatible(config_users, False)
+ else:
+ users += cls._parse(config_users)
+
+ # backwards compability
+ if isinstance(config_superusers, dict):
+ users += cls._parse_backwards_compatible(config_superusers, True)
+
+ return users