Skip to content

Commit

Permalink
Merge pull request #440 from tomudding/feature/more-restrictive-valid…
Browse files Browse the repository at this point in the history
…ation-tue-usernames

feat: additional validity checks for TU/e-usernames starting with a year
  • Loading branch information
tomudding authored Sep 2, 2024
2 parents 34dfc26 + f7034a4 commit ee89eea
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
13 changes: 11 additions & 2 deletions module/Application/language/en.po

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions module/Application/language/gewisdb.pot

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions module/Application/language/nl.po

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions module/Database/src/Form/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
use Laminas\Validator\StringLength;
use Throwable;

use function date;
use function preg_match;
use function str_ends_with;
use function substr;

class Member extends Form implements InputFilterProviderInterface
{
Expand Down Expand Up @@ -399,6 +402,20 @@ public function getInputFilterSpecification(): array
],
],
],
[
'name' => Callback::class,
'options' => [
'callback' => function ($value) {
return $this->isNewTueUsernameValid($value);
},
'messages' => [
Callback::INVALID_VALUE => $this->translator->translate(
// phpcs:ignore -- user-visible strings should not be split
'Your TU/e-username appears to be incorrect. Ensure that it starts with a valid year and looks like: YYYYxxxx. If you believe your TU/e-username is correct, please contact the secretary.',
),
],
],
],
],
'filters' => [
['name' => ToNull::class],
Expand All @@ -407,6 +424,25 @@ public function getInputFilterSpecification(): array
];
}

private function isNewTueUsernameValid(string $value): bool
{
try {
// Only check for YYYYABCD TU/e usernames.
if (preg_match('/^s\d{6}$/', $value)) {
return true;
}

$year = substr($value, 0, 4);
$currentYear = date('Y');

// Check if the year is within the valid range, the assumption being that you can never have a number
// starting with a year that is higher than the current year.
return $year >= 2000 && $year <= $currentYear;
} catch (Throwable) {
return false;
}
}

private function isOldEnough(string $value): bool
{
try {
Expand Down

0 comments on commit ee89eea

Please sign in to comment.