Skip to content

Commit

Permalink
COMCL-722: Allow user with with award review permission access option…
Browse files Browse the repository at this point in the history
… value
  • Loading branch information
olayiwola-compucorp committed Sep 25, 2024
1 parent 9c8d421 commit 1bf06f3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
61 changes: 61 additions & 0 deletions CRM/CiviAwards/Event/Listener/AlterPermission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

use Civi\API\Event\AuthorizeEvent;
use Civi\Api4\Generic\AbstractAction;
use CRM_CiviAwards_Hook_AlterAPIPermissions_Award as AwardPermission;

/**
* Award filter while fetching cases.
*/
class CRM_CiviAwards_Event_Listener_AlterPermission {

/**
* Fetches cases by the given award filters.
*
* @param \Civi\API\Event\PrepareEvent $event
* API Prepare Event Object.
*/
public static function authorize(AuthorizeEvent $event) {
$apiRequest = $event->getApiRequest();
if ($apiRequest['version'] != 4) {
return;
}

if (!self::shouldRun($apiRequest)) {
return;
}

$hasPermission = CRM_Core_Permission::check(AwardPermission::REVIEW_FIELD_SET_PERM);
if (!$hasPermission) {
return;
}

$params = CRM_Utils_Request::retrieve('params', 'String');
if (is_null($params)) {
return;
}

try {
$params = json_decode($params);
$formName = $params->formName ?? NULL;
$event->setAuthorized($formName == 'qf:CRM_CiviAwards_Form_AwardReview');
}
catch (\Throwable $th) {
}
}

/**
* Determines if the processing will run.
*
* @param array $apiRequest
* Api request data.
*
* @return bool
* TRUE if processing should run, FALSE otherwise.
*/
protected static function shouldRun(AbstractAction $apiRequest) {
return $apiRequest['entity'] == 'OptionValue' &&
$apiRequest['action'] == 'get';
}

}
6 changes: 6 additions & 0 deletions civiawards.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ function civiawards_civicrm_config(&$config) {
['CRM_CiviAwards_Event_Listener_AwardCaseFilter', 'onPrepare'],
10
);

Civi::dispatcher()->addListener(
'civi.api.authorize',
['CRM_CiviAwards_Event_Listener_AlterPermission', 'authorize'],
10
);
}

/**
Expand Down

0 comments on commit 1bf06f3

Please sign in to comment.