[Plugin] auth_plain: Create admin user if installed and no users exist.

This commit is contained in:
Ferdinand Thiessen 2021-02-18 12:42:31 +01:00
parent 251648e9a4
commit f9ac002b53
1 changed files with 16 additions and 1 deletions

View File

@ -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)