33namespace BookStack \Activity \Models ;
44
55use BookStack \App \Model ;
6+ use BookStack \Permissions \Models \JointPermission ;
7+ use BookStack \Permissions \PermissionApplicator ;
68use BookStack \Users \Models \HasCreatorAndUpdater ;
79use BookStack \Users \Models \OwnableInterface ;
8- use BookStack \Users \Models \User ;
910use BookStack \Util \HtmlContentFilter ;
11+ use Illuminate \Database \Eloquent \Builder ;
1012use Illuminate \Database \Eloquent \Factories \HasFactory ;
1113use Illuminate \Database \Eloquent \Relations \BelongsTo ;
14+ use Illuminate \Database \Eloquent \Relations \HasMany ;
1215use Illuminate \Database \Eloquent \Relations \MorphTo ;
1316
1417/**
1720 * @property string $html
1821 * @property int|null $parent_id - Relates to local_id, not id
1922 * @property int $local_id
20- * @property string $entity_type
21- * @property int $entity_id
23+ * @property string $commentable_type
24+ * @property int $commentable_id
2225 * @property string $content_ref
2326 * @property bool $archived
2427 */
@@ -44,8 +47,8 @@ public function entity(): MorphTo
4447 public function parent (): BelongsTo
4548 {
4649 return $ this ->belongsTo (Comment::class, 'parent_id ' , 'local_id ' , 'parent ' )
47- ->where ('entity_type ' , '= ' , $ this ->entity_type )
48- ->where ('entity_id ' , '= ' , $ this ->entity_id );
50+ ->where ('commentable_type ' , '= ' , $ this ->commentable_type )
51+ ->where ('commentable_id ' , '= ' , $ this ->commentable_id );
4952 }
5053
5154 /**
@@ -58,11 +61,27 @@ public function isUpdated(): bool
5861
5962 public function logDescriptor (): string
6063 {
61- return "Comment # {$ this ->local_id } (ID: {$ this ->id }) for {$ this ->entity_type } (ID: {$ this ->entity_id }) " ;
64+ return "Comment # {$ this ->local_id } (ID: {$ this ->id }) for {$ this ->commentable_type } (ID: {$ this ->commentable_id }) " ;
6265 }
6366
6467 public function safeHtml (): string
6568 {
6669 return HtmlContentFilter::removeScriptsFromHtmlString ($ this ->html ?? '' );
6770 }
71+
72+ public function jointPermissions (): HasMany
73+ {
74+ return $ this ->hasMany (JointPermission::class, 'entity_id ' , 'commentable_id ' )
75+ ->whereColumn ('joint_permissions.entity_type ' , '= ' , 'comments.commentable_type ' );
76+ }
77+
78+ /**
79+ * Scope the query to just the comments visible to the user based upon the
80+ * user visibility of what has been commented on.
81+ */
82+ public function scopeVisible (Builder $ query ): Builder
83+ {
84+ return app ()->make (PermissionApplicator::class)
85+ ->restrictEntityRelationQuery ($ query , 'comments ' , 'commentable_id ' , 'commentable_type ' );
86+ }
6887}
0 commit comments