Skip to content

Commit

Permalink
Show IP ranges and other improvements (#1326)
Browse files Browse the repository at this point in the history
* Added IP ranges as a hint when adding new ones to an app. Upgraded govuk-frontend and moj-frontend to latest versions

* Formatted large ip allowlists so they drop down a line rather than extend over the page

* Made formatting simpler. Formatted ip allow list display page a bit better. Fixed styling issues caused by govuk upgrade

* Added functionality to show/hide forms marked with data-show-if-selected. Works with checkboxes and radio buttons

* reverted to old hint as fixed in html

* Removed js from super linter

* updated dockerfile to build css correctly

* updated dockerfile to import js files

* Removed CSS linting

* Updated favicon location

* Replaced unused class with govuk visually hidden
  • Loading branch information
jamesstottmoj authored Aug 2, 2024
1 parent a5e8593 commit 2374de6
Show file tree
Hide file tree
Showing 19 changed files with 186 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/super-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DEFAULT_BRANCH: main
VALIDATE_ALL_CODEBASE: false
VALIDATE_CSS: false
VALIDATE_GITHUB_ACTIONS: false
VALIDATE_DOCKERFILE_HADOLINT: false
VALIDATE_JAVASCRIPT_STANDARD: false
VALIDATE_JSCPD: false
VALIDATE_PYTHON_MYPY: false
VALIDATE_PYTHON_PYLINT: false
Expand Down
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COPY jest.config.js controlpanel/frontend/static /src/
RUN npm install
RUN mkdir -p dist &&\
./node_modules/.bin/babel src/module-loader.js src/components src/javascripts -o dist/app.js -s
RUN ./node_modules/.bin/sass --load-path=node_modules/ --style=compressed src/app.scss:dist/app.css
RUN ./node_modules/.bin/sass --load-path=./ --style=compressed src/app.scss:dist/app.css
WORKDIR /src
RUN /node_modules/.bin/jest

Expand Down Expand Up @@ -69,6 +69,10 @@ COPY tests tests

# install javascript dependencies
COPY --from=build-node dist/app.css dist/app.js static/
COPY --from=build-node node_modules/govuk-frontend/dist/govuk/assets/fonts/. static/assets/fonts
COPY --from=build-node node_modules/govuk-frontend/dist/govuk/assets/images/. static/assets/images
COPY --from=build-node node_modules/govuk-frontend/dist/govuk/govuk-frontend.min.js static/assets/js/govuk-frontend.min.js
COPY --from=build-node node_modules/govuk-frontend/dist/govuk/govuk-frontend.min.js.map static/assets/js/govuk-frontend.min.js.map
COPY --from=build-node node_modules/accessible-autocomplete/dist/ static/accessible-autocomplete
COPY --from=build-node node_modules/govuk-frontend static/govuk-frontend
COPY --from=build-node node_modules/@ministryofjustice/frontend/moj static/ministryofjustice-frontend
Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,21 @@ build-static:

build-css:
mkdir static
mkdir -p static/assets/fonts
mkdir -p static/assets/images
cp -R node_modules/govuk-frontend/dist/govuk/assets/fonts/. static/assets/fonts
cp -R node_modules/govuk-frontend/dist/govuk/assets/images/. static/assets/images
cp -R node_modules/accessible-autocomplete/dist/ static/accessible-autocomplete
cp -R node_modules/govuk-frontend/ static/govuk-frontend
cp -R node_modules/@ministryofjustice/frontend/ static/ministryofjustice-frontend
cp -R node_modules/html5shiv/dist/ static/html5-shiv
./node_modules/.bin/sass --load-path=node_modules/ --style=compressed controlpanel/frontend/static/app.scss:static/app.css
npm run css --load

build-js:
mkdir -p static/assets/js
cp node_modules/govuk-frontend/dist/govuk/govuk-frontend.min.js static/assets/js/govuk-frontend.min.js
cp node_modules/govuk-frontend/dist/govuk/govuk-frontend.min.js.map static/assets/js/govuk-frontend.min.js.map

cp -R node_modules/jquery/dist/ static/jquery
cp -R node_modules/jquery-ui/dist/ static/jquery-ui
./node_modules/.bin/babel \
Expand Down
9 changes: 6 additions & 3 deletions controlpanel/frontend/jinja2/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
<div class="govuk-warning-text">
<span class="govuk-warning-text__icon" aria-hidden="true">!</span>
<strong class="govuk-warning-text__text">
<span class="govuk-warning-text__assistive">Warning</span>
<span class="govuk-visually-hidden">Warning</span>
{% for message in broadcast_messages %}
{{ message }} <br/>
{% endfor %}
Expand Down Expand Up @@ -194,8 +194,11 @@

{% block body_end %}
{% block scripts %}
<script src="{{ static('govuk-frontend/govuk/all.js') }}"></script>
<script>window.GOVUKFrontend.initAll()</script>
<script type="module" src="{{ static('assets/js/govuk-frontend.min.js') }}"></script>
<script type="module">
import { initAll } from "{{ static('assets/js/govuk-frontend.min.js') }}"
initAll()
</script>
<script src="{{ static('accessible-autocomplete/accessible-autocomplete.min.js') }}"></script>
<script src="{{ static('jquery/jquery.min.js') }}"></script>
<script src="{{ static('jquery-ui/jquery-ui.min.js') }}"></script>
Expand Down
2 changes: 1 addition & 1 deletion controlpanel/frontend/jinja2/datasource-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1 class="govuk-heading-xl">{{ page_title }}</h1>
<div class="govuk-warning-text">
<span class="govuk-warning-text__icon" aria-hidden="true">!</span>
<strong class="govuk-warning-text__text">
<span class="govuk-warning-text__assistive">Warning</span>
<span class="govuk-visually-hidden">Warning</span>
This bucket was deleted by <a href="{{ url('manage-user', kwargs={ "pk": bucket.deleted_by.auth0_id }) }}">
{{ user_name(bucket.deleted_by) }}</a> on {{ bucket.deleted_at.strftime("%Y/%m/%d %H:%M:%S") }}.<br>
All access listed below has been revoked in IAM.
Expand Down
4 changes: 2 additions & 2 deletions controlpanel/frontend/jinja2/govuk-frontend.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{# Ensure that older IE versions always render with the correct rendering engine #}
<meta http-equiv="X-UA-Compatible" content="IE=edge">
{% block headIcons %}
<link rel="shortcut icon" href="{{ static('govuk-frontend/govuk/assets/images/favicon.ico') }}" type="image/x-icon">
<link rel="shortcut icon" href="{{ static('assets/images/favicon.ico') }}" type="image/x-icon">
<link rel="apple-touch-icon" href="{{ static('ministryofjustice-frontend/moj/assets/images/moj-apple-touch-icon-180x180.png') }}" sizes="180x180">
<link rel="apple-touch-icon" href="{{ static('ministryofjustice-frontend/moj/assets/images/moj-apple-touch-icon-167x167.png') }}" sizes="167x167">
<link rel="apple-touch-icon" href="{{ static('ministryofjustice-frontend/moj/assets/images/moj-apple-touch-icon-152x152.png') }}" sizes="152x152">
Expand All @@ -35,7 +35,7 @@
<meta property="og:image" content="{{ scheme }}://{{ host }}{{ static('ministryofjustice-frontend/assets/images/moj-opengraph-image.png') }}">
</head>

<body class="app-body {{ bodyClasses or "" }}">
<body class="app-body govuk-frontend-supported {{ bodyClasses or "" }}">
<script>document.body.className = ((document.body.className) ? document.body.className + ' js-enabled' : 'js-enabled');</script>
<div class="app-pane">
{% block bodyStart %}{% endblock %}
Expand Down
35 changes: 20 additions & 15 deletions controlpanel/frontend/jinja2/includes/auth0-connections-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,31 @@
{% set name = "connections" %}
{% set itemHintId = id + '-item-hint' %}
<div class="govuk-checkboxes__item">
{% set client_id_field_name = item[1] + '_auth0_client_id' %}
{% set client_secret_field_name = item[1] + '_auth0_client_secret' %}
{% set conn_name_field_name = item[1] + '_auth0_conn_name' %}
{% set has_linked_client_fields = (form.fields.get(client_id_field_name, None)) %}
{% set label_text = item[0] + " (Please provide the client credential from the provider)" if has_linked_client_fields else item[0] %}

<input class="govuk-checkboxes__input" id="{{ id }}" name="{{ name }}" type="checkbox" value="{{ item[1] }}"
{{-" checked" if item[0] in params.selected_values else "" }}>
{{ govukLabel({
"text": item[0],
"text": label_text,
"classes": 'govuk-checkboxes__label' + (' ' + (item.label|default({})).classes|default("")),
"attributes": (item.label|default({})).attributes|default(""),
"for": id
}) }}
{% set client_id_field_name = item[1] + '_auth0_client_id' %}
{% set client_secret_field_name = item[1] + '_auth0_client_secret' %}
{% set conn_name_field_name = item[1] + '_auth0_conn_name' %}
{% set has_linked_client_fields = (form.fields.get(client_id_field_name, None)) %}

{% if has_linked_client_fields %}
{% set client_id_field = form.fields.get(client_id_field_name) %}
{% set client_secret_field = form.fields.get(client_secret_field_name) %}
{% set conn_name_field = form.fields.get(conn_name_field_name) %}
(Please provide the client credential from the provider)
<div class="cpanel-subform" data-show-if-selected="{{ item[0] }}">
<br/>
</div>

{% if has_linked_client_fields %}
{% set client_id_field = form.fields.get(client_id_field_name) %}
{% set client_secret_field = form.fields.get(client_secret_field_name) %}
{% set conn_name_field = form.fields.get(conn_name_field_name) %}

<div class="govuk-grid-row checkbox-subform" data-show-if-selected="{{ item[0] }}">
<div class="govuk-grid-column-full">
<br/>
{{ govukInput({
"name": conn_name_field_name,
"classes": "govuk-!-width-one-half",
Expand Down Expand Up @@ -77,9 +82,9 @@
"errorMessage": {"text": params.errors[client_secret_field_name]|join(". ")} if params.errors.get(client_secret_field_name) else {},
})
}}
</div>
{% endif %}
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
{% endset -%}
Expand Down
4 changes: 2 additions & 2 deletions controlpanel/frontend/jinja2/ip-allowlist-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ <h1 class="govuk-heading-xl">{{ page_title }}</h1>
{{ ip_allowlist.description }}
</td>
<td class="govuk-table__cell">
{{ ip_allowlist.allowed_ip_ranges }}
{{ ip_allowlist.allowed_ip_ranges.replace(",", ", ") }}
</td>
<td class="govuk-table__cell align-right no-wrap">
<a href="{{ url('manage-ip-allowlist', kwargs={ "pk": ip_allowlist.pk }) }}"
class="govuk-button govuk-button--secondary">
Manage IP allowlist
Manage IP allowlist
</a>
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion controlpanel/frontend/jinja2/reset.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h1 class="govuk-heading-l">Home Directory Reset</h1>
<div class="govuk-warning-text">
<span class="govuk-warning-text__icon" aria-hidden="true">!</span>
<strong class="govuk-warning-text__text">
<span class="govuk-warning-text__assistive">Warning</span>
<span class="govuk-visually-hidden">Warning</span>
Please ensure you've copied any important files from your home
directory. Important files should be stored in S3 or GitHub.
</strong>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Update auth0 connections - {{ app.name }}
{%- endset %}

{% set page_name = "Auth0 connections" %}
{% set page_title = "Auth0 connections" %}

{% block content %}
<h1 class="govuk-heading-xl">{{ legend }}</h1>
Expand Down
4 changes: 2 additions & 2 deletions controlpanel/frontend/jinja2/webapp-create.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

{% set new_datasource_html %}
Create a new webapp data source
<div class="cpanel-subform" data-show-if-selected="connect_bucket-1">
<div class="radio-subform" data-show-if-selected="connect_bucket-1">
{{ govukInput({
"name": "new_datasource_name",
"classes": "govuk-!-width-one-half",
Expand All @@ -35,7 +35,7 @@

{% set existing_datasource_html %}
Connect an existing webapp data source
<div class="cpanel-subform" data-show-if-selected="connect_bucket-2">
<div class="radio-subform" data-show-if-selected="connect_bucket-2">
<div class="govuk-form-group
{%- if form.existing_datasource_id.errors %} govuk-form-group--error{% endif %}">
{{ govukLabel({"text": "Select webapp data source"}) }}
Expand Down
6 changes: 3 additions & 3 deletions controlpanel/frontend/static/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ $govuk-global-styles: true;
$govuk-font-family-nta: "Arial";
$govuk-font-family-nta-tabular: "Arial";

$govuk-assets-path: "/static/govuk-frontend/govuk/assets/";
$govuk-assets-path: "/static/assets/";

// GOV.UK Frontend
@import "govuk-frontend/govuk/all";
@import "node_modules/govuk-frontend/dist/govuk/all";

// MOJ Frontend
@import "@ministryofjustice/frontend/moj/all";
@import "node_modules/@ministryofjustice/frontend/moj/all";

// Other component styles
@import "components/all";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"classes": 'govuk-checkboxes__hint',
"attributes": item.hint.attributes,
"html": item.hint.html,
text: item.hint.text
"text": item.hint.text
}) }}
{%- endif %}
</div>
Expand Down
4 changes: 2 additions & 2 deletions controlpanel/frontend/static/components/hint/macro.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% macro govukHint(params) %}
<span {%- if params.id %} id="{{ params.id }}"{% endif %} class="govuk-hint {%- if params.classes %} {{ params.classes }}{% endif %}"
<div {%- if params.id %} id="{{ params.id }}"{% endif %} class="govuk-hint {%- if params.classes %} {{ params.classes }}{% endif %}"
{%- for attribute, value in params.attributes %} {{attribute}}="{{value}}"{% endfor %}>
{{ params.html|default(params.text)|safe }}
</span>
</div>
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
moj.Modules.toggleCheckboxForm = {
subFormClass: 'checkbox-subform',
selector: 'data-show-if-selected',
hiddenClass: 'govuk-!-display-none',

init() {
const panels = document.querySelectorAll(`.${this.subFormClass}`);
if (panels) {
this.bindEvents(panels);
}
},

bindEvents(panels) {

panels.forEach(panel => {
const attribute = panel.getAttribute(this.selector);
var formItem = document.querySelector(`[value=${attribute}]`);

this.setVisibility(panel, formItem.checked);

formItem.addEventListener('change', () => {
this.togglePanel(panel);
});
});
},

togglePanel(panel) {
panel.classList.toggle(this.hiddenClass);
},

setVisibility(panel, show) {
if (show) {
panel.classList.remove(this.hiddenClass);
} else {
panel.classList.add(this.hiddenClass);
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
moj.Modules.toggleRadioForm = {
panelClass: 'radio-subform',
selector: 'data-show-if-selected',
radioButtonClass: 'govuk-radios__input',
hiddenClass: 'govuk-!-display-none',

init() {
const panels = document.querySelectorAll(`.${this.panelClass}`);
if (panels) {
this.bindEvents(panels);
}
},

bindEvents(panels) {

const radios = document.querySelectorAll(`.${this.radioButtonClass}`);

radios.forEach(radio => {
radio.addEventListener('change', () => {
const name = radio.getAttribute('name');

document.querySelectorAll(`input[name="${name}"]`).forEach(otherRadio => {
if (otherRadio !== radio) {
// Trigger a custom 'deselect' event on every member of the current radio group except the clicked one...
const event = new Event('deselect');
otherRadio.dispatchEvent(event);
}
});
});
});

panels.forEach(panel => {
const attribute = panel.getAttribute(this.selector);
var formItem = document.querySelector(`#${attribute}`);

this.setVisibility(panel, formItem.checked);

formItem.addEventListener('change', (event) => {
this.setVisibility(panel, event.target.checked);
});

formItem.addEventListener('deselect', (event) => {
this.setVisibility(panel, event.target.checked);
});
});
},

setVisibility(panel, show) {
if (show) {
panel.classList.remove(this.hiddenClass);
} else {
panel.classList.add(this.hiddenClass);
}
}
};
10 changes: 10 additions & 0 deletions controlpanel/frontend/views/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,24 @@ class UpdateAppIPAllowlists(OIDCLoginRequiredMixin, PermissionRequiredMixin, Upd
permission_required = "api.update_app_ip_allowlists"
fields = ["ip_allowlists"]

def format_ip_allowlists(self, allowlist):
# splits larger ip allowlists into multiple lines

allowlist = allowlist.replace(",", ", ")
return allowlist

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context["app"] = self.get_object()
context["env_name"] = self.request.GET.get("env_name")

context["app_ip_allowlists"] = [
{
"text": ip_allowlist.name,
"value": ip_allowlist.pk,
"hint": {
"text": self.format_ip_allowlists(ip_allowlist.allowed_ip_ranges),
},
"checked": ip_allowlist.pk
in context["app"].env_allow_ip_ranges_ids(context["env_name"]),
}
Expand Down
Loading

0 comments on commit 2374de6

Please sign in to comment.