From 6c73a63eb56def83c4a46261f25ff531d2de8d90 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sun, 31 Jul 2022 15:45:37 +0200 Subject: [PATCH] [cli] Make sure plugin permissions are installed Signed-off-by: Ferdinand Thiessen --- flaschengeist/cli/plugin_cmd.py | 29 ++++++++++++++++---- flaschengeist/plugins/auth_plain/__init__.py | 2 +- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/flaschengeist/cli/plugin_cmd.py b/flaschengeist/cli/plugin_cmd.py index bb0dd98..3b0f420 100644 --- a/flaschengeist/cli/plugin_cmd.py +++ b/flaschengeist/cli/plugin_cmd.py @@ -4,7 +4,9 @@ from flask import current_app from flask.cli import with_appcontext from importlib_metadata import EntryPoint, entry_points +from flaschengeist.database import db from flaschengeist.config import config +from flaschengeist.models.user import Permission @click.group() @@ -12,12 +14,7 @@ def plugin(): pass -@plugin.command() -@click.argument("plugin", nargs=-1, type=str) -@click.option("--all", help="Install all enabled plugins", is_flag=True) -@with_appcontext -@pass_context -def install(ctx, plugin, all): +def install_plugin_command(ctx, plugin, all): """Install one or more plugins""" if not all and len(plugin) == 0: ctx.fail("At least one plugin must be specified, or use `--all` flag.") @@ -31,10 +28,30 @@ def install(ctx, plugin, all): for p in plugins: name = p.id.split(".")[-1] click.echo(f"Installing {name}{'.'*(20-len(name))}", nl=False) + # Install permissions + if p.permissions: + cur_perm = set(x.name for x in Permission.query.filter(Permission.name.in_(p.permissions)).all()) + all_perm = set(p.permissions) + + add = all_perm - cur_perm + if add: + db.session.bulk_save_objects([Permission(name=x) for x in all_perm]) + db.session.commit() + # Custom installation steps p.install() click.secho(" ok", fg="green") +@plugin.command() +@click.argument("plugin", nargs=-1, type=str) +@click.option("--all", help="Install all enabled plugins", is_flag=True) +@with_appcontext +@pass_context +def install(ctx, plugin, all): + """Install one or more plugins""" + return install_plugin_command(ctx, plugin, all) + + @plugin.command() @click.argument("plugin", nargs=-1, type=str) @with_appcontext diff --git a/flaschengeist/plugins/auth_plain/__init__.py b/flaschengeist/plugins/auth_plain/__init__.py index e73b98c..51b12ef 100644 --- a/flaschengeist/plugins/auth_plain/__init__.py +++ b/flaschengeist/plugins/auth_plain/__init__.py @@ -19,7 +19,7 @@ class AuthPlain(AuthPlugin): def install(self): plugins_installed(self.post_install) - def post_install(self, **kwargs): + def post_install(self, *args, **kwargs): if User.query.filter(User.deleted == False).count() == 0: logger.info("Installing admin user") role = Role.query.filter(Role.name == "Superuser").first()