Passed
Push — master ( 9e4d93...df17d4 )
by Marcel
03:16 queued 12s
created

ShareService::getSharedReports()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 21
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 30
rs 8.4444
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2019-2022 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Service;
13
14
use OCA\Analytics\Activity\ActivityManager;
15
use OCA\Analytics\Db\ShareMapper;
16
use OCP\DB\Exception;
17
use OCP\IGroupManager;
18
use OCP\IUserManager;
19
use OCP\IUserSession;
20
use OCP\Security\ISecureRandom;
21
use Psr\Log\LoggerInterface;
22
23
class ShareService
24
{
25
    const SHARE_TYPE_USER = 0;
26
    const SHARE_TYPE_GROUP = 1;
27
    const SHARE_TYPE_USERGROUP = 2;
28
    const SHARE_TYPE_LINK = 3;
29
    const SHARE_TYPE_ROOM = 10;
30
31
    /** @var IUserSession */
32
    private $userSession;
33
    /** @var LoggerInterface */
34
    private $logger;
35
    /** @var ShareMapper */
36
    private $ShareMapper;
37
    private $secureRandom;
38
    private $ActivityManager;
39
    /** @var IGroupManager */
40
    private $groupManager;
41
    /** @var IUserManager */
42
    private $userManager;
43
    private $VariableService;
44
45
    public function __construct(
46
        IUserSession $userSession,
47
        LoggerInterface $logger,
48
        ShareMapper $ShareMapper,
49
        ActivityManager $ActivityManager,
50
        IGroupManager $groupManager,
51
        ISecureRandom $secureRandom,
52
        IUserManager $userManager,
53
        VariableService $VariableService
54
    )
55
    {
56
        $this->userSession = $userSession;
57
        $this->logger = $logger;
58
        $this->ShareMapper = $ShareMapper;
59
        $this->secureRandom = $secureRandom;
60
        $this->groupManager = $groupManager;
61
        $this->ActivityManager = $ActivityManager;
62
        $this->userManager = $userManager;
63
        $this->VariableService = $VariableService;
64
    }
65
66
    /**
67
     * create a new share
68
     *
69
     * @NoAdminRequired
70
     * @param $reportId
71
     * @param $type
72
     * @param $user
73
     * @return bool
74
     * @throws \OCP\DB\Exception
75
     */
76
    public function create($reportId, $type, $user)
77
    {
78
        $token = null;
79
        if ((int)$type === self::SHARE_TYPE_LINK) {
80
            $token = $this->generateToken();
81
        }
82
        $this->ShareMapper->createShare($reportId, $type, $user, $token);
83
        $this->ActivityManager->triggerEvent($reportId, ActivityManager::OBJECT_REPORT, ActivityManager::SUBJECT_REPORT_SHARE);
84
        return true;
85
    }
86
87
    /**
88
     * get all shares for a report
89
     *
90
     * @NoAdminRequired
91
     * @param $reportId
92
     * @return array
93
     */
94
    public function read($reportId)
95
    {
96
        $shares = $this->ShareMapper->getShares($reportId);
97
        foreach ($shares as &$share) {
98
            if ((int)$share['type'] === self::SHARE_TYPE_USER) {
99
                $share['displayName'] = $this->userManager->get($share['uid_owner'])->getDisplayName();
100
            }
101
            $share['pass'] = $share['pass'] !== null;
102
        }
103
        return $shares;
104
    }
105
106
    /**
107
     * get all report by token
108
     *
109
     * @NoAdminRequired
110
     * @param $token
111
     * @return array
112
     */
113
    public function getReportByToken($token)
114
    {
115
        $reportId = $this->ShareMapper->getReportByToken($token);
116
        return $this->VariableService->replaceTextVariables($reportId);
117
    }
118
119
    /**
120
     * verify password hahes
121
     *
122
     * @NoAdminRequired
123
     * @param $password
124
     * @param $sharePassword
125
     * @return bool
126
     */
127
    public function verifyPassword($password, $sharePassword)
128
    {
129
        return password_verify($password, $sharePassword);
130
    }
131
132
    /**
133
     * get all reports shared with user
134
     *
135
     * @NoAdminRequired
136
     * @throws Exception
137
     */
138
    public function getSharedReports()
139
    {
140
        $sharedReports = $this->ShareMapper->getAllSharedReports();
141
        $groupsOfUser = $this->groupManager->getUserGroups($this->userSession->getUser());
142
        $reports = array();
143
144
        foreach ($sharedReports as $sharedReport) {
145
            if ($sharedReport['shareType'] === self::SHARE_TYPE_GROUP) {
146
                if (array_key_exists($sharedReport['shareUid_owner'], $groupsOfUser)) {
147
                    $sharedReport['parent'] = '0';
148
                    if (!in_array($sharedReport["id"], array_column($reports, "id"))) {
149
                        unset($sharedReport['shareId']);
150
                        unset($sharedReport['shareType']);
151
                        unset($sharedReport['shareUid_owner']);
152
                        $reports[] = $sharedReport;
153
                    }
154
                }
155
            } elseif ($sharedReport['shareType'] === self::SHARE_TYPE_USER) {
156
                if ($this->userSession->getUser()->getUID() === $sharedReport['shareUid_owner']) {
157
                    $sharedReport['parent'] = '0';
158
                    if (!in_array($sharedReport["id"], array_column($reports, "id"))) {
159
                        unset($sharedReport['shareId']);
160
                        unset($sharedReport['shareType']);
161
                        unset($sharedReport['shareUid_owner']);
162
                        $reports[] = $sharedReport;
163
                    }
164
                }
165
            }
166
        }
167
        return $reports;
168
    }
169
170
    /**
171
     * get metadata of a report, shared with current user
172
     *
173
     * @NoAdminRequired
174
     * @param $reportId
175
     * @return array
176
     * @throws Exception
177
     */
178
    public function getSharedReport($reportId)
179
    {
180
        $sharedReport = $this->getSharedReports();
181
        if (in_array($reportId, array_column($sharedReport, "id"))) {
182
            $key = array_search($reportId, array_column($sharedReport, 'id'));
183
            return $sharedReport[$key];
184
        } else {
185
            return [];
186
        }
187
    }
188
189
    /**
190
     * delete a share
191
     *
192
     * @NoAdminRequired
193
     * @param $shareId
194
     * @return bool
195
     * @throws Exception
196
     */
197
    public function delete($shareId)
198
    {
199
        $this->ShareMapper->deleteShare($shareId);
200
        return true;
201
    }
202
203
    /**
204
     * delete all shares for a report
205
     *
206
     * @NoAdminRequired
207
     * @param $reportId
208
     * @return bool
209
     */
210
    public function deleteShareByReport($reportId)
211
    {
212
        return $this->ShareMapper->deleteShareByReport($reportId);
213
    }
214
215
    /**
216
     * update/set share password
217
     *
218
     * @NoAdminRequired
219
     * @param $shareId
220
     * @param string|null $password
221
     * @param string|null $canEdit
222
     * @param string|null $domain
223
     * @return bool
224
     */
225
    public function update($shareId, $password = null, $canEdit = null, $domain = null)
226
    {
227
        if ($password !== null) {
228
            $password = password_hash($password, PASSWORD_DEFAULT);
229
            return $this->ShareMapper->updateSharePassword($shareId, $password);
230
        }
231
        if ($domain !== null) {
232
            return $this->ShareMapper->updateShareDomain($shareId, $domain);
233
        }
234
        if ($canEdit !== null) {
235
            $canEdit === true ? $canEdit = \OCP\Constants::PERMISSION_UPDATE : $canEdit = \OCP\Constants::PERMISSION_READ;
0 ignored issues
show
introduced by
The condition $canEdit === true is always false.
Loading history...
236
            return $this->ShareMapper->updateSharePermissions($shareId, $canEdit);
237
        }
238
    }
239
240
    /**
241
     * generate to token used to authenticate federated shares
242
     *
243
     * @return string
244
     */
245
    private function generateToken()
246
    {
247
        $token = $this->secureRandom->generate(
248
            15,
249
            ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
250
        return $token;
251
    }
252
253
}