Skip to content

Commit

Permalink
Fix redirects and state saves
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed May 5, 2025
1 parent e4b12f2 commit 559932b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@
namespace CoreEnroller\Controller;

use App\Controller\StandardEnrollerController;
use App\Lib\Enum\ApplicationStateEnum;
use App\Lib\Enum\PetitionStatusEnum;
use App\Lib\Traits\ApplicationStatesTrait;
use App\Lib\Util\StringUtilities;
use Cake\Http\Exception\BadRequestException;
use Cake\I18n\FrozenTime;
use Cake\ORM\TableRegistry;
use CoreEnroller\Lib\Enum\VerificationModeEnum;
use \App\Lib\Enum\HttpStatusCodesEnum;

class EmailVerifiersController extends StandardEnrollerController {
use ApplicationStatesTrait;

public $paginate = [
'order' => [
'EmailVerifiers.id' => 'asc'
Expand Down Expand Up @@ -125,6 +128,10 @@ public function resend($id) {
*/

public function dispatch(string $id) {
$request = $this->getRequest();
$session = $request->getSession();
$username = $session->read('Auth.external.user');

$op = $this->requestParam('op');

if(!$op) {
Expand All @@ -140,6 +147,7 @@ public function dispatch(string $id) {
$candidateAddresses = $this->EmailVerifiers->assembleVerifiableAddresses($cfg, $petition);

$this->set('vv_config', $cfg);
$this->set('controller', $this);
$this->set('vv_email_addresses', $candidateAddresses);

// To make things easier for the view, we'll create a separate view var with the
Expand Down Expand Up @@ -241,7 +249,24 @@ public function dispatch(string $id) {
catch(\Exception $e) {
$this->llog('error', $e->getMessage());
$this->Flash->error($e->getMessage());
}

if ($e->getMessage() === __d('error', 'Verifications.code')) {
// Add a flag to the session to instruct the UI to handle blocking.
$this->request->getSession()->write('verification_error', 1);
// Get preferences if we have an Auth.User.co_person_id
if(!empty($username)) {
$ApplicationStates = $this->fetchTable('ApplicationStates');
$columnStatement = $this->viewBuilder()->getVar('vv_person_id') === null ? 'person_id IS' : 'person_id';
$data = [
'tag' => ApplicationStateEnum::VerifyEmailBlocked,
'username' => $username,
'co_id' => $this->getCOID(),
$columnStatement => $this->viewBuilder()->getVar('vv_person_id') ?? null
];
$ApplicationStates->createOrUpdate($data, 'lock');
}
}
}
} else {
// Generate a Verification request, then render a form to collect it.
// If there is already a pending request, overwrite it (generate a new code).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@
use App\Lib\Util\StringUtilities;
use Cake\Routing\Router;

if (filter_var($vv_config->enable_blockonfailure, FILTER_VALIDATE_BOOLEAN)) {

$session = $this->getRequest()->getSession();
$hasVerificationError = $session->read('verification_error') ?? 0; // Replace 'keyName' with the actual session key you want to access
if (
filter_var($vv_config->enable_blockonfailure, FILTER_VALIDATE_BOOLEAN)
&& filter_var($hasVerificationError, FILTER_VALIDATE_BOOLEAN)
) {
$session->delete('verification_error');
$stateAttr = ApplicationStateEnum::VerifyEmailBlocked;
$appStateValue = $this->ApplicationState->getValue($stateAttr, 'lock');
$appStateId = $this->ApplicationState->getId($stateAttr);
$appStateValue = $this->ApplicationState->getValue($stateAttr, 'lock', true);
$appStateId = $this->ApplicationState->getId($stateAttr, true);

if ($vv_attempts_count > 0 && $appStateValue !== 'unlock') {
$currentUrl = Router::url(null, true);
Expand All @@ -46,18 +53,6 @@
));
return;
}

// Reset the locker
if ($appStateValue !== 'lock') {
$data = [
'tag' => $stateAttr,
'value' => 'lock',
'username' => $vv_user['username'] ?? '',
'co_id' => $vv_cur_co->id,
'person_id' => $vv_person_id ?? $vv_user_roles["person_id"] ?? null
];
$this->ApplicationState->updateState($data, (int)$appStateId);
}
}

$this->Field->enableFormEditMode();
Expand Down
2 changes: 1 addition & 1 deletion app/src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ protected function primaryLinkLookup(): void
* - postcondition: Application Preferences variable set
* @since COmanage Registry v5.1.0
*/
protected function getAppPrefs() {
public function getAppPrefs() {
$request = $this->getRequest();
$session = $request->getSession();

Expand Down
61 changes: 22 additions & 39 deletions app/src/Lib/Traits/ApplicationStatesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
namespace App\Lib\Traits;

use Cake\Collection\Collection;
use Cake\ORM\Table;
use Cake\Controller\ControllerFactory;
use Cake\ORM\TableRegistry;

trait ApplicationStatesTrait
Expand All @@ -50,16 +50,22 @@ public function constructComplexStateTag(array $nameParts): string
}

/**
* @param string $stateTag
* @param string|int $defaultValue
*
* @param string $stateTag
* @param string|int $defaultValue
* @param bool $invalidate
* @return string
* @since COmanage Registry v5.1.0
*/
public function getValue(string $stateTag, string|int $defaultValue): string
public function getValue(string $stateTag, string|int $defaultValue, $invalidate = false): string
{
$vv_app_prefs = [];
if(method_exists($this, 'getView')) {

if ($invalidate) {
// View, refetch
$curController = $this->getView()->get('controller');
$curController->getAppPrefs();
$vv_app_prefs = $curController->viewBuilder()->getVar('vv_app_prefs');
} elseif(method_exists($this, 'getView')) {
// View
$vv_app_prefs = $this->getView()?->get('vv_app_prefs');
} elseif(method_exists($this, 'viewBuilder')) {
Expand All @@ -79,15 +85,21 @@ public function getValue(string $stateTag, string|int $defaultValue): string
}

/**
* @param string $stateTag
*
* @param string $stateTag
* @param bool $invalidate
* @return string
* @since COmanage Registry v5.1.0
*/
public function getId(string $stateTag): string
public function getId(string $stateTag, $invalidate = false): string
{
$vv_app_prefs = [];
if(method_exists($this, 'getView')) {

if ($invalidate) {
// View, refetch
$curController = $this->getView()->get('controller');
$curController->getAppPrefs();
$vv_app_prefs = $curController->viewBuilder()->getVar('vv_app_prefs');
} elseif(method_exists($this, 'getView')) {
// View
$vv_app_prefs = $this->getView()?->get('vv_app_prefs');
} elseif(method_exists($this, 'viewBuilder')) {
Expand All @@ -105,33 +117,4 @@ public function getId(string $stateTag): string

return (string)$appStateId;
}


/**
* Update or create a state record in the ApplicationStates table.
*
* @param array $data The data to be saved.
* @param int|null $id The ID of the record to update, or null to create a new record.
*
* @return void
* @throws \Cake\ORM\Exception\PersistenceFailedException When the save operation fails.
* @since COmanage Registry v5.2.0
*/
public function updateState(array $data, ?int $id): void
{
$appstate = TableRegistry::getTableLocator()->get('ApplicationStates');
If ($id !== null) {
// Update existing record
$record = $appstate
->find()
->where(['id' => $id])
->first();
$record->set($data);
$appstate->saveOrFail($record);
} else {
$newRecord = $appstate->newEntity($data);
// Create new entry
$appstate->saveOrFail($newRecord);
}
}
}
34 changes: 34 additions & 0 deletions app/src/Model/Table/ApplicationStatesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace App\Model\Table;

use App\Lib\Enum\ApplicationStateEnum;
use Cake\Database\Expression\QueryExpression;
use Cake\ORM\Query;
use Cake\ORM\Table;
Expand Down Expand Up @@ -141,4 +142,37 @@ public function retrieveAll(string $username, ?int $coid, ?int $personid): array

return $tags ?? [];
}


/**
* Create or update an application state.
*
* This method either updates an existing application state record or creates
* a new one based on the provided data and value.
*
* @param array $data Associative array containing fields and their values.
* @param string $value Value to set for the application state.
* @return void
* @throws \Cake\ORM\Exception\PersistenceFailedException When a save operation fails.
* @since COmanage Registry v5.2.0
*/
public function createOrUpdate(array $data, string $value): void
{
$appId = $this->find()
->cache(false)
->where($data)->first();

// Enter the new value
$data['value'] = $value;

If ($appId->id !== null) {
// Update existing record
$appId->set($data);
$this->saveOrFail($appId);
} else {
$newRecord = $this->newEntity($data);
// Create new entry
$this->saveOrFail($newRecord);
}
}
}
3 changes: 1 addition & 2 deletions app/templates/element/notify/blockUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#main {
align-content: center;
}
#flash-messages,
.page-title-container {
display: none;
}
Expand Down Expand Up @@ -99,8 +100,6 @@
$('#count-down-container'),
true
);
// Reload the page
// window.location.replace(reloadUrl);
}
}, 1000);
</script>
Expand Down

0 comments on commit 559932b

Please sign in to comment.