Skip to content

Commit

Permalink
Automatically expire UserRoles
Browse files Browse the repository at this point in the history
To ensure that people do not retain privileges on the website for
too long, `UserRole`s will not automatically expire on a specified
date.
  • Loading branch information
tomudding committed Nov 1, 2023
1 parent 1586d86 commit e5fe49a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion module/User/src/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -157,6 +157,10 @@ public function getRoleNames(): array
$names = [];

foreach ($this->getRoles() as $role) {
if (!$role->isActive()) {
continue;
}

$names[] = $role->getRole()->value;
}

Expand Down
19 changes: 19 additions & 0 deletions module/User/src/Model/UserRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}
}

0 comments on commit e5fe49a

Please sign in to comment.