From efd0b8acb3f4f098697c8c30bd196dacda39b704 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Thu, 18 Jul 2024 15:34:09 +0200 Subject: ogcp: add image restrict functionality Add center scope restriction using /image/restrict. Add view in Images to update scope permissions. Disable images in Commands for image update and restore if the client belongs to a disabled center. Consolidate template code to render scope selection checkboxes. --- ogcp/forms/action_forms.py | 17 +++- ogcp/templates/actions/image_config.html | 24 ++++++ ogcp/templates/actions/image_details.html | 22 ++++- ogcp/templates/auth/user_form.html | 13 +-- ogcp/templates/images.html | 4 + ogcp/templates/scopes_checkbox_group.html | 16 ++++ ogcp/views.py | 130 ++++++++++++++++++++++++------ 7 files changed, 185 insertions(+), 41 deletions(-) create mode 100644 ogcp/templates/actions/image_config.html create mode 100644 ogcp/templates/scopes_checkbox_group.html diff --git a/ogcp/forms/action_forms.py b/ogcp/forms/action_forms.py index e72e37c..82958fb 100644 --- a/ogcp/forms/action_forms.py +++ b/ogcp/forms/action_forms.py @@ -7,7 +7,8 @@ from wtforms import ( Form, SubmitField, HiddenField, SelectField, BooleanField, IntegerField, - StringField, RadioField, FormField, FieldList, DecimalField, TextAreaField + StringField, RadioField, FormField, FieldList, DecimalField, TextAreaField, + SelectMultipleField ) from wtforms.validators import InputRequired from flask_wtf import FlaskForm @@ -236,6 +237,20 @@ class ImageDetailsForm(FlaskForm): permissions = StringField(label=_l('Permissions')) software_id = StringField(label=_l('Software id')) checksum = StringField(label=_l('Checksum')) + scopes = SelectMultipleField( + label=_l('Allowed scopes'), + description=_l('No scope selection gives full access'), + ) + +class ImageConfigForm(FlaskForm): + image_id = HiddenField() + server = HiddenField() + name = HiddenField() + scopes = SelectMultipleField( + label=_l('Allowed scopes'), + description=_l('No scope selection gives full access'), + ) + submit = SubmitField(label=_l('Submit')) class ServerForm(FlaskForm): name = StringField(label=_l('Name'), diff --git a/ogcp/templates/actions/image_config.html b/ogcp/templates/actions/image_config.html new file mode 100644 index 0000000..c286683 --- /dev/null +++ b/ogcp/templates/actions/image_config.html @@ -0,0 +1,24 @@ +{% extends 'images.html' %} +{% import "bootstrap/wtf.html" as wtf %} + +{% set btn_back = true %} + +{% block content %} + +

{{_('Update image')}}

+ +
+ {{ form.hidden_tag() }} + + {{ form.image_id() }} + {{ form.server() }} + {{ form.name() }} + + {% include 'scopes_checkbox_group.html' %} + +
+ {{ form.submit(class="btn btn-primary") }} +
+
+ +{% endblock %} diff --git a/ogcp/templates/actions/image_details.html b/ogcp/templates/actions/image_details.html index 87105cb..ab66503 100644 --- a/ogcp/templates/actions/image_details.html +++ b/ogcp/templates/actions/image_details.html @@ -7,9 +7,23 @@

{{_('Image details')}}

-{{ wtf.quick_form(form, - method='post', - button_map={'create': 'primary'}, - extra_classes="mx-5") }} +
+
+ {{ form.hidden_tag() }} + + {% for field in form if field.type != 'CSRFToken' and field.name not in ['scopes'] %} + {% if not field.flags.hidden %} +
+ +
+ {{ field(class="form-control") }} +
+
+ {% endif %} + {% endfor %} + + {% include 'scopes_checkbox_group.html' %} +
+
{% endblock %} diff --git a/ogcp/templates/auth/user_form.html b/ogcp/templates/auth/user_form.html index bd2f396..ae34490 100644 --- a/ogcp/templates/auth/user_form.html +++ b/ogcp/templates/auth/user_form.html @@ -104,18 +104,7 @@ -
- {{ form.scopes.label(class_='form-label') }} -
{{ form.scopes.description }}
-
- {% for value, label, checked in form.scopes.iter_choices() %} -
- - -
- {% endfor %} -
-
+ {% include 'scopes_checkbox_group.html' %}
diff --git a/ogcp/templates/images.html b/ogcp/templates/images.html index 00bb2e4..462e68d 100644 --- a/ogcp/templates/images.html +++ b/ogcp/templates/images.html @@ -67,6 +67,10 @@ {% endif %} + {% if current_user.get_permission('IMAGE', 'UPDATE') %} + + {% endif %} {% endif %} {% if btn_back %}