Skip to content

Commit a841f3d

Browse files
authored
Merge pull request #553 from DirectoryTree/fix-account-expires-maximum
Appropriately handle maximum and minimum values of windows integer time
2 parents a746eb2 + 7cc0e53 commit a841f3d

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

src/Models/Attributes/Timestamp.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
class Timestamp
1313
{
14+
public const WINDOWS_INT_MAX = 9223372036854775807;
15+
1416
/**
1517
* The current timestamp type.
1618
*
@@ -114,15 +116,15 @@ public function fromDateTime($value)
114116
*/
115117
protected function valueIsWindowsIntegerType($value)
116118
{
117-
return is_numeric($value) && strlen((string) $value) === 18;
119+
return is_numeric($value) && in_array(strlen((string) $value), [18, 19]);
118120
}
119121

120122
/**
121123
* Converts the LDAP timestamp value to a Carbon instance.
122124
*
123125
* @param mixed $value
124126
*
125-
* @return Carbon|false
127+
* @return Carbon|int|false
126128
*
127129
* @throws LdapRecordException
128130
*/
@@ -215,18 +217,24 @@ protected function convertDateTimeToWindows(DateTime $date)
215217
*
216218
* @param int $value
217219
*
218-
* @return DateTime|false
220+
* @return DateTime|int|false
219221
*
220222
* @throws \Exception
221223
*/
222224
protected function convertWindowsIntegerTimeToDateTime($value)
223225
{
224-
// ActiveDirectory dates that contain integers may return
225-
// "0" when they are not set. We will validate that here.
226-
if (! $value) {
226+
if (is_null($value) || $value === '') {
227227
return false;
228228
}
229229

230+
if ($value == 0) {
231+
return (int) $value;
232+
}
233+
234+
if ($value == static::WINDOWS_INT_MAX) {
235+
return (int) $value;
236+
}
237+
230238
return (new DateTime())->setTimestamp(
231239
Utilities::convertWindowsTimeToUnixTime($value)
232240
);

tests/Unit/Models/ActiveDirectory/UserTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,26 @@ public function test_user_is_enabled()
212212

213213
$this->assertTrue($user->isEnabled());
214214
}
215+
216+
public function test_account_expires_with_maximum()
217+
{
218+
$user = new User();
219+
220+
$max = Timestamp::WINDOWS_INT_MAX;
221+
222+
$user->accountExpires = $max;
223+
224+
$this->assertSame($max, $user->accountExpires);
225+
}
226+
227+
public function test_account_expires_with_minimum()
228+
{
229+
$user = new User();
230+
231+
$user->accountExpires = 0;
232+
233+
$this->assertSame(0, $user->accountExpires);
234+
}
215235
}
216236

217237
class UserPasswordTestStub extends User

tests/Unit/Models/Attributes/TimestampTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,24 @@ public function test_windows_time_to_date_time_always_has_utc_set_as_timezone()
116116

117117
date_default_timezone_set('UTC');
118118
}
119+
120+
public function test_windows_int_type_properly_handles_maximum()
121+
{
122+
$timestamp = new Timestamp('windows-int');
123+
124+
$max = Timestamp::WINDOWS_INT_MAX;
125+
126+
$this->assertSame($max, $timestamp->toDateTime($max));
127+
$this->assertSame($max, $timestamp->toDateTime((string) $max));
128+
}
129+
130+
public function test_windows_int_type_properly_handles_minimum()
131+
{
132+
$timestamp = new Timestamp('windows-int');
133+
134+
$min = 0;
135+
136+
$this->assertSame($min, $timestamp->toDateTime($min));
137+
$this->assertSame($min, $timestamp->toDateTime((string) $min));
138+
}
119139
}

0 commit comments

Comments
 (0)