Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MMB-272: Add Validity Dates Check For Certificate Download #80

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
}

}
Loading