|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* balloon |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Balloon\App\Notification\Hook; |
|
13
|
|
|
|
|
14
|
|
|
use Balloon\App\Notification\Notifier; |
|
15
|
|
|
use Balloon\Async\Mail; |
|
16
|
|
|
use Balloon\Filesystem\Node\Collection; |
|
17
|
|
|
use Balloon\Filesystem\Node\NodeInterface; |
|
18
|
|
|
use Balloon\Hook\AbstractHook; |
|
19
|
|
|
use Balloon\Server; |
|
20
|
|
|
use Balloon\Server\User; |
|
21
|
|
|
use MongoDB\BSON\ObjectId; |
|
22
|
|
|
use Psr\Log\LoggerInterface; |
|
23
|
|
|
|
|
24
|
|
|
class NewShareAdded extends AbstractHook |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Notifier. |
|
28
|
|
|
* |
|
29
|
|
|
* @var Notifier |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $notifier; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Server. |
|
35
|
|
|
* |
|
36
|
|
|
* @var Server |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $server; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Logger. |
|
42
|
|
|
* |
|
43
|
|
|
* @var LoggerInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $logger; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Constructor. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(Notifier $notifier, Server $server, LoggerInterface $logger) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->notifier = $notifier; |
|
53
|
|
|
$this->server = $server; |
|
54
|
|
|
$this->logger = $logger; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function postSaveNodeAttributes(NodeInterface $node, array $attributes, array $remove, ?string $recursion, bool $recursion_first): void |
|
61
|
|
|
{ |
|
62
|
|
|
if (!($node instanceof Collection)) { |
|
63
|
|
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$fs = $node->getFilesystem(); |
|
|
|
|
|
|
67
|
|
|
$raw = $node->getRawAttributes(); |
|
68
|
|
|
|
|
69
|
|
|
if ($node->isReference()) { |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
if ($node->isShared() && isset($raw['acl']) && $raw['acl'] === $node->getAttributes()['acl']) { |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
if (!$node->isShared()) { |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$receiver = []; |
|
80
|
|
|
foreach ($node->getAcl() as $rule) { |
|
81
|
|
|
if (isset($raw['acl']) && $this->hadRole($raw['acl'], $rule['role']->getId())) { |
|
82
|
|
|
continue; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ('user' === $rule['type']) { |
|
86
|
|
|
if (!isset($receiver[(string) $rule['role']->getId()]) && $this->checkNotify($node, $rule['role'])) { |
|
87
|
|
|
$receiver[(string) $rule['role']->getId()] = $rule['role']; |
|
88
|
|
|
} |
|
89
|
|
|
} elseif ('group' === $rule['type']) { |
|
90
|
|
|
foreach ($rule['role']->getResolvedMembers() as $user) { |
|
91
|
|
|
if (!isset($receiver[(string) $user->getId()]) && $this->checkNotify($node, $user)) { |
|
92
|
|
|
$receiver[(string) $user->getId()] = $user; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (!empty($receiver)) { |
|
99
|
|
|
$message = $this->notifier->compose('new_share_added', [ |
|
100
|
|
|
'node' => $node, |
|
101
|
|
|
]); |
|
102
|
|
|
|
|
103
|
|
|
$this->notifier->notify($receiver, $this->server->getIdentity(), $message); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Check if had role before. |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function hadRole(array $acl, ObjectId $id): bool |
|
111
|
|
|
{ |
|
112
|
|
|
foreach ($acl as $rule) { |
|
113
|
|
|
if ($rule['id'] == $id) { |
|
114
|
|
|
return true; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Check if users needs a notification and checks if mail adress is available. |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function checkNotify(NodeInterface $node, User $user): bool |
|
125
|
|
|
{ |
|
126
|
|
|
if ($user->hasShare($node)) { |
|
127
|
|
|
$this->logger->debug('skip notifcation for share ['.$node->getId().'] user ['.$user->getId().'] already got it', [ |
|
128
|
|
|
'category' => get_class($this), |
|
129
|
|
|
]); |
|
130
|
|
|
|
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return true; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.