Skip to content

Commit d265dd0

Browse files
committed
fix custom attribute validation for old users
1 parent 81855fe commit d265dd0

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

src/LaunchDarkly/LDClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public function isOffline(): bool
296296

297297
/**
298298
* Tracks that an application-defined event occurred.
299-
*
299+
*
300300
* This method creates a "custom" analytics event containing the specified event name (key)
301301
* and context properties. You may attach arbitrary data or a metric value to the event with the
302302
* optional `data` and `metricValue` parameters.

src/LaunchDarkly/LDContext.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@
66

77
use LaunchDarkly\Types\AttributeReference;
88

9+
function isAllowableUserCustomAttr(string $name): bool
10+
{
11+
switch ($name) {
12+
case 'anonymous':
13+
case 'avatar':
14+
case 'country':
15+
case 'email':
16+
case 'firstName':
17+
case 'ip':
18+
case 'key':
19+
case 'kind':
20+
case 'lastName':
21+
case 'name':
22+
return false;
23+
default:
24+
return true;
25+
}
26+
}
27+
928
/**
1029
* A collection of attributes that can be referenced in flag evaluations and analytics events.
1130
* This entity is also called an "evaluation context."
@@ -220,7 +239,9 @@ public static function fromUser(LDUser $user): LDContext
220239
$attrs = [];
221240
}
222241
foreach ($userCustom as $k => $v) {
223-
$attrs[$k] = $v;
242+
if (isAllowableUserCustomAttr($k)) {
243+
$attrs[$k] = $v;
244+
}
224245
}
225246
}
226247
$privateAttrs = null;
@@ -810,9 +831,6 @@ private static function decodeJsonSingleKind(array $o, ?string $kind): LDContext
810831

811832
private static function decodeJsonOldUser(array $o, bool $wasParsedAsArray): LDContext
812833
{
813-
$j = json_encode($o);
814-
file_put_contents('php://stderr', "decodeJsonOldUser($j, $wasParsedAsArray)\n", FILE_APPEND);
815-
816834
$b = self::builder('');
817835
$key = null;
818836
foreach ($o as $k => $v) {
@@ -826,7 +844,9 @@ private static function decodeJsonOldUser(array $o, bool $wasParsedAsArray): LDC
826844
throw self::parsingBadTypeError($k);
827845
}
828846
foreach ((array)$v as $k1 => $v1) {
829-
$b->set($k1, $v1);
847+
if (!isAllowableUserCustomAttr($k1)) {
848+
$b->set($k1, $v1);
849+
}
830850
}
831851
}
832852
break;

tests/LDContextTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,17 @@ public function testContextFromUser()
378378
->private("c2")
379379
->build();
380380
self::assertContextsEqual($c2Expected, $c2);
381+
382+
// make sure custom attrs can't override built-in ones
383+
$u3 = (new LDUserBuilder("key"))
384+
->email("good")
385+
->custom(["email" => "bad"])
386+
->build();
387+
$c3 = LDContext::fromUser($u3);
388+
$c3Expected = LDContext::builder($u3->getKey())
389+
->set("email", "good")
390+
->build();
391+
self::assertContextsEqual($c3Expected, $c3);
381392
}
382393

383394
private static function assertContextValid($c)

0 commit comments

Comments
 (0)