From 5884d867fd387e0e33a7aa654e8ca6cc2c009bae Mon Sep 17 00:00:00 2001 From: kaiyou <pierre@jaury.eu> Date: Sat, 5 Sep 2020 18:34:24 +0200 Subject: [PATCH] Support per service actions --- hiboo/application/base.py | 14 ++++++++++ hiboo/service/admin.py | 16 +++++++++++- hiboo/service/templates/service_action.html | 15 +++++++++++ hiboo/service/templates/service_details.html | 27 +++++++++++++++++++- hiboo/service/templates/service_list.html | 12 ++++++--- 5 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 hiboo/service/templates/service_action.html diff --git a/hiboo/application/base.py b/hiboo/application/base.py index fa6a1b46..c20ae1b6 100644 --- a/hiboo/application/base.py +++ b/hiboo/application/base.py @@ -9,6 +9,7 @@ class BaseApplication(object): """ registry = dict() + actions = dict() sso_protocol = None name = None @@ -66,7 +67,20 @@ class BaseApplication(object): class OIDCApplication(BaseApplication): sso_protocol = sso.oidc + class SAMLApplication(BaseApplication): sso_protocol = sso.saml +def action(label, profile=False, quick=False): + """ Registers a profile or application action + """ + class Register(): + def __init__(self, function): + self.function = function + def __set_name__(self, owner, name): + if not owner.actions: + owner.actions = dict() + owner.actions[name] = (label, profile, quick) + setattr(owner, name, self.function) + return Register \ No newline at end of file diff --git a/hiboo/service/admin.py b/hiboo/service/admin.py index bd41a146..780e5983 100644 --- a/hiboo/service/admin.py +++ b/hiboo/service/admin.py @@ -10,7 +10,8 @@ import uuid @security.admin_required() def list(): services = models.Service.query.all() - return flask.render_template("service_list.html", services=services) + return flask.render_template("service_list.html", + services=services, application=application) @blueprint.route("/create") @@ -78,6 +79,19 @@ def delete(service_uuid): return flask.redirect(flask.url_for(".list")) +@blueprint.route("/action/<service_uuid>/<action>", methods=["GET", "POST"]) +@security.admin_required() +def action(service_uuid, action): + service = models.Service.query.get(service_uuid) or flask.abort(404) + app = application.registry.get(service.application_id) or flask.abort(404) + label, profile, quick = app.actions.get(action) or flask.abort(404) + if profile: + flask.abort(404) + result = getattr(app, action)(service) + return flask.render_template("service_action.html", + label=label, service=service, result=result) + + @blueprint.route("/setapp/<service_uuid>") @security.admin_required() def setapp_select(service_uuid): diff --git a/hiboo/service/templates/service_action.html b/hiboo/service/templates/service_action.html new file mode 100644 index 00000000..5453afa8 --- /dev/null +++ b/hiboo/service/templates/service_action.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block title %}{{ service.name }}{% endblock %} +{% block subtitle %}{{ label }}{% endblock %} + +{% block content %} +<div class="row"> + <div class="col-xs-12"> + <div class="box"> + <div class="box-body"> + {{ result | safe }} + </div> + </div> +</div> +{% endblock %} diff --git a/hiboo/service/templates/service_details.html b/hiboo/service/templates/service_details.html index f54b44af..19ca15c2 100644 --- a/hiboo/service/templates/service_details.html +++ b/hiboo/service/templates/service_details.html @@ -5,8 +5,11 @@ {% block content %} <div class="row"> - <div class="col-xs-12"> + <div class="col-md-6 col-xs-12"> <div class="box"> + <div class="box-header"> + <h4>{% trans %}Attributes{% endtrans %}</h4> + </div> <div class="box-body"> <dl class="dl-horizontal"> <dt>{% trans %}Service name{% endtrans %}</dt> @@ -30,6 +33,23 @@ </div> </div> </div> + <div class="col-md-6 col-xs-12"> + <div class="box"> + <div class="box-header"> + <h4>{% trans %}Advanced actions{% endtrans %}</h4> + </div> + <div class="box-body"> + <dl class="dl-horizontal"> + {% for action, (label, profile, _) in application.actions.items() %} + {% if not profile %} + <dt><a href="{{ url_for(".action", service_uuid=service.uuid, action=action) }}">{{ label }}</a></dt> + <dd>{{ function.__doc__ }}</dd> + {% endif %} + {% endfor %} + </dl> + </div> + </div> + </div> </div> <div class="row"> <div class="col-xs-12"> @@ -42,6 +62,11 @@ {% endblock %} {% block actions %} +{% for action, (label, profile, quick) in application.actions.items() %} +{% if quick and not profile %} +<a href="{{ url_for(".action", service_uuid=service.uuid, action=action) }}" class="btn btn-info">{{ label }}</a> +{% endif %} +{% endfor %} <a href="{{ url_for("profile.list_for_service", service_uuid=service.uuid) }}" class="btn btn-primary">{% trans %}View profiles{% endtrans %}</a> <a href="{{ url_for(".setapp_select", service_uuid=service.uuid) }}" class="btn btn-primary">{% trans %}Change application{% endtrans %}</a> <a href="{{ url_for(".edit", service_uuid=service.uuid) }}" class="btn btn-primary">{% trans %}Edit this service{% endtrans %}</a> diff --git a/hiboo/service/templates/service_list.html b/hiboo/service/templates/service_list.html index 615ada8f..fbf82e7a 100644 --- a/hiboo/service/templates/service_list.html +++ b/hiboo/service/templates/service_list.html @@ -18,15 +18,21 @@ <th>{% trans %}Actions{% endtrans %}</th> </tr> {% for service in services %} + {% set app = application.registry.get(service.application_id) %} <tr> <td><a href="{{ url_for(".details", service_uuid=service.uuid) }}">{{ service.name }}</a></td> <td>{{ service.provider }}</td> - <td>{{ service.application_id }}</td> + <td>{{ app.name }}</td> <td>{{ service.POLICIES[service.policy] }}</td> <td>{{ service.max_profiles }}</td> <td> - <a href="{{ url_for("profile.list_for_service", service_uuid=service.uuid)}}">Profiles</a> - <a href="{{ url_for(".edit", service_uuid=service.uuid)}}">Edit</a> + <a href="{{ url_for("profile.list_for_service", service_uuid=service.uuid)}}">Profiles</a> + <a href="{{ url_for(".edit", service_uuid=service.uuid)}}">Edit</a> + {% for action, (label, profile, quick) in app.actions.items() %} + {% if quick and not profile %} + <a href="{{ url_for(".action", service_uuid=service.uuid, action=action) }}">{{ label }}</a> + {% endif %} + {% endfor %} </td> </tr> {% endfor %} -- GitLab