From c3cf97ea504925dabd4174aca2adb22ea2d8fec4 Mon Sep 17 00:00:00 2001 From: Craig Williams Date: Thu, 10 Feb 2022 14:52:48 +0000 Subject: [PATCH] Re-jigged some of the registration routes and modified the buttons/links to use them --- assets/avsdev/js/widgets/organisations.js | 56 +++++++++++++++++++ locale/en_US/messages.php | 2 + routes/organisation-registration.php | 19 ++++--- .../OrganisationRegistrationController.php | 6 +- templates/pages/organisation.html.twig | 7 +-- templates/tables/organisations.html.twig | 12 ++++ 6 files changed, 86 insertions(+), 16 deletions(-) diff --git a/assets/avsdev/js/widgets/organisations.js b/assets/avsdev/js/widgets/organisations.js index e64db54..80c6413 100644 --- a/assets/avsdev/js/widgets/organisations.js +++ b/assets/avsdev/js/widgets/organisations.js @@ -46,6 +46,48 @@ function attachOrganisationForm() { }); } +/** + * Approve/Reject organisation + */ +function approveOrganisation(slug, approve, options) { + var data = {}; + data[site.csrf.keys.name] = site.csrf.name; + data[site.csrf.keys.value] = site.csrf.value; + + var url = site.uri.public + '/api/organisations/o/' + slug + '/registration/' + (approve ? 'approve' : 'reject'); + var debugAjax = (typeof site !== "undefined") && site.debug.ajax; + + return $.ajax({ + type: "PUT", + url: url, + data: data, + dataType: debugAjax ? 'html' : 'json', + }).fail(function(jqXHR) { + // Error messages + if (debugAjax && jqXHR.responseText) { + document.write(jqXHR.responseText); + document.close(); + } else { + console.log("Error (" + jqXHR.status + "): " + jqXHR.responseText); + + // Display errors on failure + // TODO: ufAlerts widget should have a 'destroy' method + if (!$("#alerts-page").data('ufAlerts')) { + $("#alerts-page").ufAlerts(); + } else { + $("#alerts-page").ufAlerts('clear'); + } + + $("#alerts-page").ufAlerts('fetch').ufAlerts('render'); + } + + return jqXHR; + }).done(function(response) { + if (!approve && options.delete_redirect) window.location.href = options.delete_redirect; + else window.location.reload(); + }); +} + /** * Link organisation action buttons, for example in a table or on a specific organisation's page. * @param {module:jQuery} el jQuery wrapped element to target. @@ -183,6 +225,20 @@ function bindOrganisationButtons(el, options) { }); }); + // Cancel a registration request + el.find('.js-organisation-approveRegistration').click(function(e) { + e.preventDefault(); + + approveOrganisation($(this).data('slug'), true, options); + }); + + // Cancel a registration request + el.find('.js-organisation-rejectRegistration').click(function(e) { + e.preventDefault(); + + approveOrganisation($(this).data('slug'), false, options); + }); + // Delete organisation button el.find('.js-organisation-delete').click(function(e) { e.preventDefault(); diff --git a/locale/en_US/messages.php b/locale/en_US/messages.php index 59fd311..8250c75 100644 --- a/locale/en_US/messages.php +++ b/locale/en_US/messages.php @@ -71,6 +71,8 @@ return [ 'TOKEN_NOT_FOUND' => 'Approval token does not exist / Organisation is already approved/rejected.', 'APPROVED' => 'You have successfully approved the organisation {{name}}.', 'REJECTED' => 'You have successfully rejected the organisation {{name}}.', + 'APPROVE' => 'Approve organisation', + 'REJECT' => 'Reject organisation', ], ], diff --git a/routes/organisation-registration.php b/routes/organisation-registration.php index d4310a0..1852cc1 100644 --- a/routes/organisation-registration.php +++ b/routes/organisation-registration.php @@ -12,17 +12,20 @@ use UserFrosting\Sprinkle\Core\Util\NoCache; /* * Routes for organisation registration workflows. */ -$app->group('/organisations', function () { - $this->get('/registration/approve', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationRegistrationController:approveToken'); - $this->get('/registration/reject', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationRegistrationController:rejectToken'); - - $this->post('/o/{slug}/registration/approve', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationRegistrationController:approve'); - $this->post('/o/{slug}/registration/reject', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationRegistrationController:reject'); +$app->group('/organisations/registration', function () { + $this->get('/approve', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationRegistrationController:approveToken'); + $this->get('/reject', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationRegistrationController:rejectToken'); })->add('authGuard')->add(new NoCache()); $app->group('/api/organisations', function () { - $this->post('/register', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationRegistrationController:register'); - $this->delete('/o/{slug}/registration', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationRegistrationController:cancel'); + $this->post('/registeration', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationRegistrationController:register'); +})->add('authGuard')->add(new NoCache()); + +$app->group('/api/organisations/o/{slug}/registration', function () { + $this->put('/approve', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationRegistrationController:approve'); + $this->put('/reject', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationRegistrationController:reject'); + + $this->delete('', 'UserFrosting\Sprinkle\Organisations\Controller\OrganisationRegistrationController:cancel'); })->add('authGuard')->add(new NoCache()); $app->group('/modals/organisations', function () { diff --git a/src/Controller/OrganisationRegistrationController.php b/src/Controller/OrganisationRegistrationController.php index 1e13e87..e71ac02 100644 --- a/src/Controller/OrganisationRegistrationController.php +++ b/src/Controller/OrganisationRegistrationController.php @@ -285,7 +285,7 @@ class OrganisationRegistrationController extends SimpleController 'name' => $organisation->name ]); - return $response->withRedirect($this->ci->router->pathFor('uri_organisation', ['slug' => $organisation->slug])); + return $response->withJson([], 200); } /** @@ -433,7 +433,7 @@ class OrganisationRegistrationController extends SimpleController 'name' => $organisation->name ]); - return $response->withRedirect($this->ci->router->pathFor('uri_organisations')); + return $response->withJson([], 200); } /** @@ -572,7 +572,7 @@ class OrganisationRegistrationController extends SimpleController return $this->ci->view->render($response, 'modals/organisation.html.twig', [ 'organisation' => $organisation, 'form' => [ - 'action' => 'api/organisations/register', + 'action' => 'api/organisations/registeration', 'method' => 'POST', 'fields' => $fields, 'submit_text' => $translator->translate('REGISTER'), diff --git a/templates/pages/organisation.html.twig b/templates/pages/organisation.html.twig index e07f1e6..0ff88a0 100644 --- a/templates/pages/organisation.html.twig +++ b/templates/pages/organisation.html.twig @@ -57,11 +57,8 @@
{% if checkAccess('approve_organisation') %} -
- {% include "forms/csrf.html.twig" %} - - -
+ + {% elseif isOrganisationAdmin(organisation) %} {% endif %} diff --git a/templates/tables/organisations.html.twig b/templates/tables/organisations.html.twig index 32c77e2..f4c82fb 100644 --- a/templates/tables/organisations.html.twig +++ b/templates/tables/organisations.html.twig @@ -91,6 +91,18 @@ {% endverbatim %}{{translate("ORGANISATION.EDIT")}}{% verbatim %} + {{#ifx row.flag_approved '==' 0 }} +
  • + + {% endverbatim %}{{translate("ORGANISATION.APPROVAL.APPROVE")}}{% verbatim %} + +
  • +
  • + + {% endverbatim %}{{translate("ORGANISATION.APPROVAL.REJECT")}}{% verbatim %} + +
  • + {{/ifx }}
  • {% endverbatim %}{{translate("ORGANISATION.MERGE")}}{% verbatim %}