diff --git a/module/User/src/Model/User.php b/module/User/src/Model/User.php index 3cdfd3a5a3..cd104fe4c1 100644 --- a/module/User/src/Model/User.php +++ b/module/User/src/Model/User.php @@ -148,7 +148,7 @@ public function getMember(): MemberModel } /** - * Get the user's role names. + * Get the user's active role names. * * @return string[] Role names */ @@ -157,6 +157,10 @@ public function getRoleNames(): array $names = []; foreach ($this->getRoles() as $role) { + if (!$role->isActive()) { + continue; + } + $names[] = $role->getRole()->value; } diff --git a/module/User/src/Model/UserRole.php b/module/User/src/Model/UserRole.php index 231c10c241..a91f3cf6cf 100644 --- a/module/User/src/Model/UserRole.php +++ b/module/User/src/Model/UserRole.php @@ -5,6 +5,7 @@ namespace User\Model; use Application\Model\Traits\IdentifiableTrait; +use DateTime; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\JoinColumn; @@ -43,6 +44,15 @@ enumType: UserRoles::class, )] protected UserRoles $role; + /** + * Date after which this role has expired. + */ + #[Column( + type: 'datetime', + nullable: true, + )] + protected ?DateTime $expiration = null; + /** * Get the membership number. */ @@ -74,4 +84,13 @@ public function setRole(UserRoles $role): void { $this->role = $role; } + + /** + * Determine whether this role is active (i.e. has not expired). + */ + public function isActive(): bool + { + return null !== $this->expiration + && (new DateTime('now')) < $this->expiration; + } }