[cli] Make sure plugin permissions are installed

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
This commit is contained in:
Ferdinand Thiessen 2022-07-31 15:45:37 +02:00
parent 3d6b37f0a5
commit 6c73a63eb5
2 changed files with 24 additions and 7 deletions

View File

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

View File

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