Passed
Push — master ( bee16e...19738d )
by Marcel
02:56
created

ShareService::getDatasetByToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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 2020 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\AppFramework\Http\DataResponse;
17
use OCP\IGroupManager;
18
use OCP\ILogger;
19
use OCP\IUserManager;
20
use OCP\IUserSession;
21
use OCP\Security\ISecureRandom;
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 ILogger */
32
    private $logger;
33
    /** @var ShareMapper */
34
    private $ShareMapper;
35
    private $secureRandom;
36
    private $ActivityManager;
37
    /** @var IGroupManager */
38
    private $groupManager;
39
    /** @var IUserSession */
40
    private $userSession;
41
    /** @var IUserManager */
42
    private $userManager;
43
44
    public function __construct(
45
        ILogger $logger,
46
        ShareMapper $ShareMapper,
47
        ActivityManager $ActivityManager,
48
        IGroupManager $groupManager,
49
        ISecureRandom $secureRandom,
50
        IUserSession $userSession,
51
        IUserManager $userManager
52
    )
53
    {
54
        $this->logger = $logger;
55
        $this->ShareMapper = $ShareMapper;
56
        $this->secureRandom = $secureRandom;
57
        $this->groupManager = $groupManager;
58
        $this->ActivityManager = $ActivityManager;
59
        $this->userSession = $userSession;
60
        $this->userManager = $userManager;
61
    }
62
63
    /**
64
     * get all dataset by token
65
     *
66
     * @NoAdminRequired
67
     * @param $token
68
     * @return array
69
     */
70
    public function getDatasetByToken($token)
71
    {
72
        return $this->ShareMapper->getDatasetByToken($token);
73
    }
74
75
    /**
76
     * verify password hahes
77
     *
78
     * @NoAdminRequired
79
     * @param $password
80
     * @param $sharePassword
81
     * @return bool
82
     */
83
    public function verifyPassword($password, $sharePassword)
84
    {
85
        return password_verify($password, $sharePassword);
86
    }
87
88
    /**
89
     * get all datasets shared with user
90
     *
91
     * @NoAdminRequired
92
     */
93
    public function getSharedDatasets()
94
    {
95
        $sharedDatasetsByGroup = array();
96
        $groups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
0 ignored issues
show
Bug introduced by
It seems like $this->userSession->getUser() can also be of type null; however, parameter $user of OCP\IGroupManager::getUserGroupIds() does only seem to accept OCP\IUser, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        $groups = $this->groupManager->getUserGroupIds(/** @scrutinizer ignore-type */ $this->userSession->getUser());
Loading history...
97
98
        foreach ($groups as $group) {
99
            $sharedDatasetByGroup = $this->ShareMapper->getDatasetsByGroup($group);
100
            $sharedDatasetsByGroup = array_merge($sharedDatasetsByGroup, $sharedDatasetByGroup);
101
        }
102
103
        $sharedDatasets = $this->ShareMapper->getSharedDatasets();
104
        return array_merge($sharedDatasetsByGroup, $sharedDatasets);
105
    }
106
107
    /**
108
     * get metadata of a dataset, shared with current user
109
     *
110
     * @NoAdminRequired
111
     * @param $id
112
     * @return array
113
     */
114
    public function getSharedDataset($id)
115
    {
116
        $sharedDataset = $this->ShareMapper->getSharedDataset($id);
117
        if (empty($sharedDataset)) {
118
            $groups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
0 ignored issues
show
Bug introduced by
It seems like $this->userSession->getUser() can also be of type null; however, parameter $user of OCP\IGroupManager::getUserGroupIds() does only seem to accept OCP\IUser, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
            $groups = $this->groupManager->getUserGroupIds(/** @scrutinizer ignore-type */ $this->userSession->getUser());
Loading history...
119
            foreach ($groups as $group) {
120
                $sharedDataset = $this->ShareMapper->getDatasetByGroupId($group, $id);
121
                break;
122
            }
123
        }
124
        return $sharedDataset;
125
    }
126
127
    /**
128
     * create a new share
129
     *
130
     * @NoAdminRequired
131
     * @param $datasetId
132
     * @param $type
133
     * @param $user
134
     * @return bool
135
     */
136
    public function create($datasetId, $type, $user)
137
    {
138
        if ((int)$type === self::SHARE_TYPE_LINK) {
139
            $token = $this->generateToken();
140
        } else {
141
            $token = null;
142
        }
143
        $this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_SHARE);
144
        return $this->ShareMapper->createShare($datasetId, $type, $user, $token);
145
    }
146
147
    /**
148
     * generate to token used to authenticate federated shares
149
     *
150
     * @return string
151
     */
152
    private function generateToken()
153
    {
154
        $token = $this->secureRandom->generate(
155
            15,
156
            ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
157
        return $token;
158
    }
159
160
    /**
161
     * get all shares for a dataset
162
     *
163
     * @NoAdminRequired
164
     * @param $datasetId
165
     * @return DataResponse
166
     */
167
    public function read($datasetId)
168
    {
169
        $shares = $this->ShareMapper->getShares($datasetId);
170
        foreach ($shares as &$share) {
171
            if ($share['type'] === 0) {
172
                $share['displayName'] = $this->userManager->get($share['uid_owner'])->getDisplayName();
173
            }
174
            $share['pass'] = $share['pass'] !== null;
175
        }
176
        return new DataResponse($shares);
177
    }
178
179
    /**
180
     * update/set share password
181
     *
182
     * @NoAdminRequired
183
     * @param $shareId
184
     * @param $password
185
     * @return bool
186
     */
187
    public function update($shareId, $password)
188
    {
189
        //$this->logger->error($shareId . $password);
190
        if ($password !== '') $password = password_hash($password, PASSWORD_DEFAULT);
191
        else $password = null;
192
        return $this->ShareMapper->updateShare($shareId, $password);
193
    }
194
195
    /**
196
     * delete a share
197
     *
198
     * @NoAdminRequired
199
     * @param $shareId
200
     * @return bool
201
     */
202
    public function delete($shareId)
203
    {
204
        return $this->ShareMapper->deleteShare($shareId);
205
    }
206
207
    /**
208
     * delete all shares for a dataset
209
     *
210
     * @NoAdminRequired
211
     * @param $datasetId
212
     * @return bool
213
     */
214
    public function deleteShareByDataset($datasetId)
215
    {
216
        return $this->ShareMapper->deleteShareByDataset($datasetId);
217
    }
218
}