-
Notifications
You must be signed in to change notification settings - Fork 15
/
ShareService.php
201 lines (171 loc) · 6.8 KB
/
ShareService.php
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
namespace humhub\modules\sharebetween\services;
use humhub\libs\BasePermission;
use humhub\modules\content\components\ActiveQueryContent;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\models\Content;
use humhub\modules\content\models\ContentContainer;
use humhub\modules\content\models\ContentContainerDefaultPermission;
use humhub\modules\post\permissions\CreatePost;
use humhub\modules\sharebetween\models\Share;
use humhub\modules\space\models\Space;
use humhub\modules\space\widgets\Chooser;
use humhub\modules\user\models\User;
use Yii;
use yii\db\Expression;
use yii\web\IdentityInterface;
final class ShareService
{
private ContentActiveRecord $record;
private IdentityInterface $user;
public function __construct(ContentActiveRecord $contentRecord, IdentityInterface $user)
{
$this->user = $user;
$this->record = $contentRecord;
}
/**
* @return ContentContainerActiveRecord[]
*/
public function list(): array
{
$container = [];
foreach ($this->getShareQuery()->all() as $share) {
/** @var Share $share */
$container[] = $share->content->container;
}
return $container;
}
public function shareOnContainerGuids(array $newGuids)
{
$alreadySharedGuids = array_map(function (ContentContainerActiveRecord $record) {
return $record->guid;
}, $this->list());
// Deleted GUIDs
foreach (array_diff($alreadySharedGuids, $newGuids) as $guid) {
if ($container = ContentContainer::findRecord($guid)) {
if ($this->canCreate($container)) {
$this->delete($container);
}
}
}
// Added GUIDs
foreach (array_diff($newGuids, $alreadySharedGuids) as $guid) {
if ($container = ContentContainer::findRecord($guid)) {
if ($this->canCreate($container)) {
$this->create($container);
}
}
}
}
public function canCreate(ContentContainerActiveRecord $container): bool
{
if ($this->record->content->visibility !== Content::VISIBILITY_PUBLIC) {
return false;
}
if (!$this->record->content->getStateService()->isPublished()) {
return false;
}
if ($this->record->content->contentcontainer_id === $container->contentcontainer_id) {
return false;
}
if ($container instanceof User && Yii::$app->getModule('user')->profileDisableStream) {
return false;
}
if ($this->user->isSystemAdmin()) {
return true;
}
if (!$container->getPermissionManager($this->user)->can(CreatePost::class)) {
return false;
}
return true;
}
public function create(ContentContainerActiveRecord $container): bool
{
$share = new Share($container);
$share->content_id = $this->record->content->id;
return $share->save();
}
public function delete(ContentContainerActiveRecord $container): void
{
foreach ($this->getShareByContainer($container)->all() as $share) {
$share->hardDelete();
}
}
public function exist(ContentContainerActiveRecord $container): bool
{
return ($this->getShareByContainer($container)->count() > 0);
}
private function getShareQuery(): ActiveQueryContent
{
return Share::find()->joinWith('content')->readable()
->andWhere([
'content.created_by' => $this->user->id,
'sharebetween_share.content_id' => $this->record->content->id,
]);
}
private function getShareByContainer(ContentContainerActiveRecord $container): ActiveQueryContent
{
return $this->getShareQuery()->andWhere(['content.contentcontainer_id' => $container->contentcontainer_id]);
}
public function searchSpaces(string $keyword): array
{
$spaces = Space::find()
->visible($this->user)
->filterBlockedSpaces($this->user)
->search($keyword);
if ($this->record->content->container instanceof Space) {
$spaces->andWhere(['!=', 'space.id', $this->record->content->container->id]);
}
if (!$this->user->isSystemAdmin()) {
// Check the User can create a Post in the searched Spaces
$spaces->leftJoin('space_membership', 'space_membership.space_id = space.id')
->leftJoin(
'contentcontainer_permission',
'contentcontainer_permission.contentcontainer_id = space.contentcontainer_id
AND contentcontainer_permission.group_id = space_membership.group_id
AND contentcontainer_permission.permission_id = :permission_id',
)
->andWhere(['space_membership.user_id' => $this->user->id])
->andWhere(['OR',
// Allowed by default
['AND',
['IN', 'space_membership.group_id', $this->getDefaultAllowedGroups()],
['IS', 'contentcontainer_permission.permission_id', new Expression('NULL')],
],
// Set to allow
['contentcontainer_permission.state' => CreatePost::STATE_ALLOW],
])
->addParams(['permission_id' => CreatePost::class]);
}
$result = [];
foreach ($spaces->all() as $space) {
$result[] = Chooser::getSpaceResult($space);
}
return $result;
}
public function getDefaultAllowedGroups(): array
{
$defaultAllowedGroups = (new CreatePost())->defaultAllowedGroups;
/* @var ContentContainerDefaultPermission[] $defaultPermissions */
$defaultPermissions = ContentContainerDefaultPermission::find()
->where(['contentcontainer_class' => Space::class])
->andWhere(['permission_id' => CreatePost::class])
->all();
foreach ($defaultPermissions as $defaultPermission) {
switch ($defaultPermission->state) {
case BasePermission::STATE_ALLOW:
if (!in_array($defaultPermission->group_id, $defaultAllowedGroups)) {
$defaultAllowedGroups[] = $defaultPermission->group_id;
}
break;
case BasePermission::STATE_DENY:
if (($i = array_search($defaultPermission->group_id, $defaultAllowedGroups)) !== false) {
unset($defaultAllowedGroups[$i]);
}
break;
}
}
return $defaultAllowedGroups;
}
}