Skip to content

Commit

Permalink
Merge pull request #81 from compucorp/MMB-272-restrict-certificate-ac…
Browse files Browse the repository at this point in the history
…cess

MMB-272: Add Validity Dates Check For Certificate Download
  • Loading branch information
shahrukh-compuco authored Nov 27, 2023
2 parents 6228ebd + 57e196a commit 31c2d35
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion CRM/Certificate/Service/CertificateAccessChecker.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Civi\Api4\Relationship;
use Civi\Api4\Membership;

class CRM_Certificate_Service_CertificateAccessChecker {

Expand All @@ -23,7 +24,7 @@ public function __construct($contactId, $certificate) {
* @return bool
*/
public function check() {
return $this->hasViewPermission() || $this->hasViewPermissionByRelationship();
return $this->checkMembershipDates() && ($this->hasViewPermission() || $this->hasViewPermissionByRelationship());
}

/**
Expand Down Expand Up @@ -80,4 +81,36 @@ private function hasViewPermissionByRelationship() {
return FALSE;
}

private function checkMembershipDates(): bool {
$membershipDates = $this->getMembershipDates();
$membershipStartDate = $membershipDates['start_date'];
$membershipEndDate = $membershipDates['end_date'];
$certificateStartDate = $this->certificate->min_valid_from_date ?? $membershipEndDate;
$certificateEndDate = $this->certificate->max_valid_through_date ?? $membershipStartDate;

return !empty($membershipStartDate) && !empty($membershipEndDate) && strtotime($membershipStartDate) <= strtotime($certificateEndDate) &&
strtotime($membershipEndDate) >= strtotime($certificateStartDate);
}

private function getMembershipDates(): array {
$startDate = NULL;
$endDate = NULL;

$memberships = Membership::get(FALSE)
->addSelect('start_date', 'end_date')
->addWhere('contact_id', '=', $this->contactId)
->addWhere('status_id', '=', 2)
->execute()
->getArrayCopy();

foreach ($memberships as $membership) {
$startDate = $startDate === NULL || strtotime($membership['start_date']) < strtotime($startDate) ?
$membership['start_date'] : $startDate;
$endDate = $endDate === NULL || strtotime($membership['end_date']) > strtotime($endDate) ?
$membership['end_date'] : $endDate;
}

return ['start_date' => $startDate, 'end_date' => $endDate];
}

}

0 comments on commit 31c2d35

Please sign in to comment.