2019-12-08 22:43:49 +01:00
|
|
|
import argparse
|
|
|
|
import getpass
|
|
|
|
import sys
|
2021-08-20 06:43:59 +02:00
|
|
|
from typing import Optional
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2021-08-20 06:43:59 +02:00
|
|
|
from bemani.data import Config, Data, DBCreateException
|
2021-08-20 06:43:36 +02:00
|
|
|
from bemani.utils.config import load_config
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
|
2021-08-20 06:43:59 +02:00
|
|
|
def create(config: Config) -> None:
|
2019-12-08 22:43:49 +01:00
|
|
|
data = Data(config)
|
|
|
|
data.create()
|
|
|
|
data.close()
|
|
|
|
|
|
|
|
|
2021-08-20 06:43:59 +02:00
|
|
|
def generate(config: Config, message: Optional[str], allow_empty: bool) -> None:
|
2019-12-08 22:43:49 +01:00
|
|
|
if message is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Please provide a message!")
|
2019-12-08 22:43:49 +01:00
|
|
|
data = Data(config)
|
|
|
|
data.generate(message, allow_empty)
|
|
|
|
data.close()
|
|
|
|
|
|
|
|
|
2021-08-20 06:43:59 +02:00
|
|
|
def upgrade(config: Config) -> None:
|
2019-12-08 22:43:49 +01:00
|
|
|
data = Data(config)
|
|
|
|
data.upgrade()
|
|
|
|
data.close()
|
|
|
|
|
|
|
|
|
2021-08-20 06:43:59 +02:00
|
|
|
def change_password(config: Config, username: Optional[str]) -> None:
|
2019-12-08 22:43:49 +01:00
|
|
|
if username is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Please provide a username!")
|
|
|
|
password1 = getpass.getpass("Password: ")
|
|
|
|
password2 = getpass.getpass("Re-enter password: ")
|
2019-12-08 22:43:49 +01:00
|
|
|
if password1 != password2:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Passwords don't match!")
|
2019-12-08 22:43:49 +01:00
|
|
|
data = Data(config)
|
|
|
|
userid = data.local.user.from_username(username)
|
|
|
|
if userid is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("User not found!")
|
2019-12-08 22:43:49 +01:00
|
|
|
data.local.user.update_password(userid, password1)
|
2022-10-15 20:56:30 +02:00
|
|
|
print(f"User {username} changed password.")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
|
2021-08-20 06:43:59 +02:00
|
|
|
def add_admin(config: Config, username: Optional[str]) -> None:
|
2019-12-08 22:43:49 +01:00
|
|
|
if username is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Please provide a username!")
|
2019-12-08 22:43:49 +01:00
|
|
|
data = Data(config)
|
|
|
|
userid = data.local.user.from_username(username)
|
|
|
|
if userid is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("User not found!")
|
2019-12-08 22:43:49 +01:00
|
|
|
user = data.local.user.get_user(userid)
|
|
|
|
user.admin = True
|
|
|
|
data.local.user.put_user(user)
|
2022-10-15 20:56:30 +02:00
|
|
|
print(f"User {username} gained admin rights.")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
|
2021-08-20 06:43:59 +02:00
|
|
|
def remove_admin(config: Config, username: Optional[str]) -> None:
|
2019-12-08 22:43:49 +01:00
|
|
|
if username is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Please provide a username!")
|
2019-12-08 22:43:49 +01:00
|
|
|
data = Data(config)
|
|
|
|
userid = data.local.user.from_username(username)
|
|
|
|
if userid is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("User not found!")
|
2019-12-08 22:43:49 +01:00
|
|
|
user = data.local.user.get_user(userid)
|
|
|
|
user.admin = False
|
|
|
|
data.local.user.put_user(user)
|
2022-10-15 20:56:30 +02:00
|
|
|
print(f"User {username} lost admin rights.")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
2022-10-15 20:56:30 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="A utility for working with databases created with this codebase."
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"operation",
|
|
|
|
help="Operation to perform, options include 'create', 'generate', 'upgrade', 'change-password', 'add-admin' and 'remove-admin'.",
|
|
|
|
type=str,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-u",
|
|
|
|
"--username",
|
|
|
|
help="Username of user to add/remove admin rights for.",
|
|
|
|
type=str,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-m",
|
|
|
|
"--message",
|
|
|
|
help="Message to use for auto-generated migration scripts.",
|
|
|
|
type=str,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-e",
|
|
|
|
"--allow-empty",
|
|
|
|
help="Allow empty migration script to be generated. Useful for data-only migrations.",
|
2022-10-15 20:56:30 +02:00
|
|
|
action="store_true",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-c",
|
|
|
|
"--config",
|
|
|
|
help="Core configuration. Defaults to server.yaml",
|
|
|
|
type=str,
|
|
|
|
default="server.yaml",
|
2019-12-08 22:43:49 +01:00
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-08-20 06:43:59 +02:00
|
|
|
config = Config()
|
2021-08-20 06:43:36 +02:00
|
|
|
load_config(args.config, config)
|
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
try:
|
|
|
|
if args.operation == "create":
|
|
|
|
create(config)
|
|
|
|
elif args.operation == "generate":
|
|
|
|
generate(config, args.message, args.allow_empty)
|
|
|
|
elif args.operation == "upgrade":
|
|
|
|
upgrade(config)
|
|
|
|
elif args.operation == "add-admin":
|
|
|
|
add_admin(config, args.username)
|
|
|
|
elif args.operation == "remove-admin":
|
|
|
|
remove_admin(config, args.username)
|
2022-10-15 20:56:30 +02:00
|
|
|
elif args.operation == "change-password":
|
2019-12-08 22:43:49 +01:00
|
|
|
change_password(config, args.username)
|
|
|
|
else:
|
2020-01-07 22:29:07 +01:00
|
|
|
raise Exception(f"Unknown operation '{args.operation}'")
|
2019-12-08 22:43:49 +01:00
|
|
|
except DBCreateException as e:
|
|
|
|
print(str(e))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if __name__ == "__main__":
|
2019-12-08 22:43:49 +01:00
|
|
|
main()
|