Skip to content

Commit 7eeeb53

Browse files
edgebalmarkharding
authored andcommitted
[Hotfix] (fix): Comments without user (#558)
* (fix): Explicitly check for user when adding a comment * (feat): Fallback to Unknown user * (chore): Don't send a username
1 parent eacc2b1 commit 7eeeb53

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

Core/Comments/Comment.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Minds\Entities\RepositoryEntity;
99
use Minds\Entities\User;
1010
use Minds\Helpers\Flags;
11+
use Minds\Helpers\Unknown;
1112

1213
/**
1314
* Comment Entity
@@ -271,6 +272,13 @@ protected function _extendExport(array $export)
271272
$output['ownerObj'] = $this->getOwnerObj();
272273
$output['description'] = $this->getBody();
273274

275+
if (!$output['ownerObj'] && !$this->getOwnerGuid()) {
276+
$unknown = Unknown::user();
277+
278+
$output['ownerObj'] = $unknown->export();
279+
$output['owner_guid'] = $unknown->guid;
280+
}
281+
274282
if ($export['attachments']) {
275283
foreach ($export['attachments'] as $key => $value) {
276284
$output['attachments'][$key] = $this->getAttachment($key);

Core/Comments/Manager.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ public function add(Comment $comment)
7777
{
7878
$entity = $this->entitiesBuilder->single($comment->getEntityGuid());
7979

80-
if (!$this->acl->interact($entity)) {
80+
if (
81+
!$comment->getOwnerGuid() ||
82+
!$this->acl->interact($entity, $comment->getOwnerEntity(true))
83+
) {
8184
throw new BlockedUserException();
8285
}
8386

Helpers/Unknown.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* Description
5+
*
6+
* @author emi
7+
*/
8+
9+
namespace Minds\Helpers;
10+
11+
use Minds\Entities\User;
12+
13+
class Unknown
14+
{
15+
/**
16+
* @return User
17+
* @throws \Exception
18+
*/
19+
public static function user()
20+
{
21+
$user = new User();
22+
23+
$user->guid = 0;
24+
$user->username = '';
25+
$user->name = 'Unknown User';
26+
27+
return $user;
28+
}
29+
}

Spec/Core/Comments/ManagerSpec.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Minds\Core\Luid;
1414
use Minds\Core\Security\ACL;
1515
use Minds\Entities\Entity;
16+
use Minds\Entities\User;
1617
use Minds\Exceptions\BlockedUserException;
1718
use PhpSpec\ObjectBehavior;
1819
use Prophecy\Argument;
@@ -81,9 +82,18 @@ function it_is_initializable()
8182

8283
function it_should_add(
8384
Comment $comment,
84-
Entity $entity
85+
Entity $entity,
86+
User $owner
8587
)
8688
{
89+
$comment->getOwnerEntity(true)
90+
->shouldBeCalled()
91+
->willReturn($owner);
92+
93+
$comment->getOwnerGuid()
94+
->shouldBeCalled()
95+
->willReturn(1000);
96+
8797
$comment->getEntityGuid()
8898
->shouldBeCalled()
8999
->willReturn(5000);
@@ -92,7 +102,7 @@ function it_should_add(
92102
->shouldBeCalled()
93103
->willReturn($entity);
94104

95-
$this->acl->interact($entity)
105+
$this->acl->interact($entity, $owner)
96106
->shouldBeCalled()
97107
->willReturn(true);
98108

@@ -127,9 +137,18 @@ function it_should_add(
127137

128138
function it_should_throw_if_blocked_user_during_add(
129139
Comment $comment,
130-
Entity $entity
140+
Entity $entity,
141+
User $owner
131142
)
132143
{
144+
$comment->getOwnerEntity(true)
145+
->shouldBeCalled()
146+
->willReturn($owner);
147+
148+
$comment->getOwnerGuid()
149+
->shouldBeCalled()
150+
->willReturn(1000);
151+
133152
$comment->getEntityGuid()
134153
->shouldBeCalled()
135154
->willReturn(5000);
@@ -138,7 +157,7 @@ function it_should_throw_if_blocked_user_during_add(
138157
->shouldBeCalled()
139158
->willReturn($entity);
140159

141-
$this->acl->interact($entity)
160+
$this->acl->interact($entity, $owner)
142161
->shouldBeCalled()
143162
->willReturn(false);
144163

0 commit comments

Comments
 (0)