forked from BookStackApp/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEntityShareLinkService.php
More file actions
171 lines (146 loc) · 4.49 KB
/
Copy pathEntityShareLinkService.php
File metadata and controls
171 lines (146 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace BookStack\Entities;
use BookStack\Activity\ActivityType;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\EntityShareLink;
use BookStack\Entities\Queries\EntityQueries;
use BookStack\Exceptions\NotFoundException;
use BookStack\Exceptions\PermissionsException;
use BookStack\Facades\Activity;
use BookStack\Permissions\Permission;
use BookStack\Permissions\PermissionApplicator;
use Illuminate\Database\Eloquent\Collection;
class EntityShareLinkService
{
public function __construct(
protected PermissionApplicator $permissions,
protected EntityQueries $queries
) {
}
/**
* Create a new share link for the given entity.
*/
public function createShareLink(Entity $entity, ?string $name = null): EntityShareLink
{
$shareLink = new EntityShareLink([
'entity_id' => $entity->id,
'entity_type' => $entity->getMorphClass(),
'token' => EntityShareLink::generateToken(),
'name' => $name,
'created_by' => user()->id,
]);
$shareLink->save();
Activity::add(ActivityType::SHARE_LINK_CREATE, $entity);
return $shareLink;
}
/**
* Delete a share link.
*/
public function deleteShareLink(EntityShareLink $shareLink): void
{
$currentUser = user();
$shareLink->load('entity');
if ($shareLink->created_by === $currentUser->id) {
$entity = $shareLink->entity;
$shareLink->delete();
if ($entity) {
Activity::add(ActivityType::SHARE_LINK_DELETE, $entity);
}
return;
}
if (userCan(Permission::SettingsManage)) {
$entity = $shareLink->entity;
$shareLink->delete();
if ($entity) {
Activity::add(ActivityType::SHARE_LINK_DELETE, $entity);
}
return;
}
if (userCan(Permission::ContentShareManage) && $shareLink->entity) {
try {
$entity = $this->queries->findVisibleById($shareLink->entity->getType(), $shareLink->entity->id);
if ($entity) {
$shareLink->delete();
Activity::add(ActivityType::SHARE_LINK_DELETE, $entity);
return;
}
} catch (NotFoundException $e) {
}
}
throw new PermissionsException(trans('errors.permission'));
}
/**
* Get all share links for a given entity.
*/
public function getShareLinksForEntity(Entity $entity): Collection
{
return EntityShareLink::query()
->forEntity($entity)
->with('createdBy')
->orderBy('created_at', 'desc')
->get();
}
/**
* Get all share links in the system (for admin overview).
*/
public function getAllShareLinks(): Collection
{
return EntityShareLink::query()
->with(['createdBy', 'entity'])
->orderBy('created_at', 'desc')
->get();
}
/**
* Get share links created by a specific user.
*/
public function getShareLinksForUser(User $user): Collection
{
return EntityShareLink::query()
->where('created_by', '=', $user->id)
->with('entity')
->orderBy('created_at', 'desc')
->get();
}
/**
* Validate a share token and return the associated entity.
*/
public function validateAndGetEntity(string $token): ?Entity
{
$shareLink = EntityShareLink::query()
->where('token', '=', $token)
->with('entity')
->first();
if (!$shareLink || !$shareLink->entity) {
return null;
}
return $shareLink->entity;
}
/**
* Find a share link by token.
*/
public function findByToken(string $token): ?EntityShareLink
{
return EntityShareLink::query()
->where('token', '=', $token)
->with('entity')
->first();
}
/**
* Check if an entity has any active share links.
*/
public function entityHasShareLinks(Entity $entity): bool
{
return EntityShareLink::query()
->forEntity($entity)
->exists();
}
/**
* Get the count of share links for an entity.
*/
public function getShareLinkCount(Entity $entity): int
{
return EntityShareLink::query()
->forEntity($entity)
->count();
}
}