From f9ac002b53ef9a4c9cb84c77664d3324f539f1d8 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 18 Feb 2021 12:42:31 +0100 Subject: [PATCH] [Plugin] auth_plain: Create admin user if installed and no users exist. --- flaschengeist/plugins/auth_plain/__init__.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/flaschengeist/plugins/auth_plain/__init__.py b/flaschengeist/plugins/auth_plain/__init__.py index ce9d006..21f5254 100644 --- a/flaschengeist/plugins/auth_plain/__init__.py +++ b/flaschengeist/plugins/auth_plain/__init__.py @@ -9,10 +9,25 @@ import binascii from werkzeug.exceptions import BadRequest, NotFound from flaschengeist.plugins import AuthPlugin -from flaschengeist.models.user import User +from flaschengeist.models.user import User, Role, Permission +from flaschengeist.database import db +from flaschengeist import logger class AuthPlain(AuthPlugin): + def post_install(self): + if User.query.first() is None: + logger.info("Installing admin user") + role = Role(name="Superuser", permissions=Permission.query.all()) + admin = User(userid="admin", firstname="Admin", lastname="Admin", mail="", roles_=[role]) + self.modify_user(admin, None, "admin") + db.session.add(admin) + db.session.commit() + logger.warning("New administrator user was added, please change the password or remove it before going into" + "production mode. Initial credentials:\n" + "name: admin\n" + "password: admin") + def login(self, user: User, password: str): if user.has_attribute("password"): return AuthPlain._verify_password(user.get_attribute("password"), password)