Skip to content

Commit

Permalink
Merge pull request #553 from DirectoryTree/fix-account-expires-maximum
Browse files Browse the repository at this point in the history
Appropriately handle maximum and minimum values of windows integer time
  • Loading branch information
stevebauman authored Feb 20, 2023
2 parents a746eb2 + 7cc0e53 commit a841f3d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Models/Attributes/Timestamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

class Timestamp
{
public const WINDOWS_INT_MAX = 9223372036854775807;

/**
* The current timestamp type.
*
Expand Down Expand Up @@ -114,15 +116,15 @@ public function fromDateTime($value)
*/
protected function valueIsWindowsIntegerType($value)
{
return is_numeric($value) && strlen((string) $value) === 18;
return is_numeric($value) && in_array(strlen((string) $value), [18, 19]);
}

/**
* Converts the LDAP timestamp value to a Carbon instance.
*
* @param mixed $value
*
* @return Carbon|false
* @return Carbon|int|false
*
* @throws LdapRecordException
*/
Expand Down Expand Up @@ -215,18 +217,24 @@ protected function convertDateTimeToWindows(DateTime $date)
*
* @param int $value
*
* @return DateTime|false
* @return DateTime|int|false
*
* @throws \Exception
*/
protected function convertWindowsIntegerTimeToDateTime($value)
{
// ActiveDirectory dates that contain integers may return
// "0" when they are not set. We will validate that here.
if (! $value) {
if (is_null($value) || $value === '') {
return false;
}

if ($value == 0) {
return (int) $value;
}

if ($value == static::WINDOWS_INT_MAX) {
return (int) $value;
}

return (new DateTime())->setTimestamp(
Utilities::convertWindowsTimeToUnixTime($value)
);
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/Models/ActiveDirectory/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,26 @@ public function test_user_is_enabled()

$this->assertTrue($user->isEnabled());
}

public function test_account_expires_with_maximum()
{
$user = new User();

$max = Timestamp::WINDOWS_INT_MAX;

$user->accountExpires = $max;

$this->assertSame($max, $user->accountExpires);
}

public function test_account_expires_with_minimum()
{
$user = new User();

$user->accountExpires = 0;

$this->assertSame(0, $user->accountExpires);
}
}

class UserPasswordTestStub extends User
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/Models/Attributes/TimestampTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,24 @@ public function test_windows_time_to_date_time_always_has_utc_set_as_timezone()

date_default_timezone_set('UTC');
}

public function test_windows_int_type_properly_handles_maximum()
{
$timestamp = new Timestamp('windows-int');

$max = Timestamp::WINDOWS_INT_MAX;

$this->assertSame($max, $timestamp->toDateTime($max));
$this->assertSame($max, $timestamp->toDateTime((string) $max));
}

public function test_windows_int_type_properly_handles_minimum()
{
$timestamp = new Timestamp('windows-int');

$min = 0;

$this->assertSame($min, $timestamp->toDateTime($min));
$this->assertSame($min, $timestamp->toDateTime((string) $min));
}
}

0 comments on commit a841f3d

Please sign in to comment.