Passed
Push — master ( 78d786...ce974b )
by Marcel
03:18
created

ShareService::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 9
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\IGroupManager;
17
use OCP\ILogger;
18
use OCP\IUserManager;
19
use OCP\IUserSession;
20
use OCP\Security\ISecureRandom;
21
22
class ShareService
23
{
24
    const SHARE_TYPE_USER = 0;
25
    const SHARE_TYPE_GROUP = 1;
26
    const SHARE_TYPE_USERGROUP = 2;
27
    const SHARE_TYPE_LINK = 3;
28
    const SHARE_TYPE_ROOM = 10;
29
30
    /** @var ILogger */
31
    private $logger;
0 ignored issues
show
introduced by
The private property $logger is not used, and could be removed.
Loading history...
32
    /** @var ShareMapper */
33
    private $ShareMapper;
34
    private $secureRandom;
35
    private $ActivityManager;
36
    /** @var IGroupManager */
37
    private $groupManager;
38
    /** @var IUserSession */
39
    private $userSession;
40
    /** @var IUserManager */
41
    private $userManager;
42
43
    public function __construct(
44
        ShareMapper $ShareMapper,
45
        ActivityManager $ActivityManager,
46
        IGroupManager $groupManager,
47
        ISecureRandom $secureRandom,
48
        IUserSession $userSession,
49
        IUserManager $userManager
50
    )
51
    {
52
        $this->ShareMapper = $ShareMapper;
53
        $this->secureRandom = $secureRandom;
54
        $this->groupManager = $groupManager;
55
        $this->ActivityManager = $ActivityManager;
56
        $this->userSession = $userSession;
57
        $this->userManager = $userManager;
58
    }
59
60
    /**
61
     * get all dataset by token
62
     *
63
     * @NoAdminRequired
64
     * @param $token
65
     * @return array
66
     */
67
    public function getDatasetByToken($token)
68
    {
69
        return $this->ShareMapper->getDatasetByToken($token);
70
    }
71
72
    /**
73
     * verify password hahes
74
     *
75
     * @NoAdminRequired
76
     * @param $password
77
     * @param $sharePassword
78
     * @return bool
79
     */
80
    public function verifyPassword($password, $sharePassword)
81
    {
82
        return password_verify($password, $sharePassword);
83
    }
84
85
    /**
86
     * get all datasets shared with user
87
     *
88
     * @NoAdminRequired
89
     */
90
    public function getSharedDatasets()
91
    {
92
        $sharedDatasetsByGroup = array();
93
        $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

93
        $groups = $this->groupManager->getUserGroupIds(/** @scrutinizer ignore-type */ $this->userSession->getUser());
Loading history...
94
95
        foreach ($groups as $group) {
96
            $sharedDatasetByGroup = $this->ShareMapper->getDatasetsByGroup($group);
97
            $sharedDatasetsByGroup = array_merge($sharedDatasetsByGroup, $sharedDatasetByGroup);
98
        }
99
100
        $sharedDatasets = $this->ShareMapper->getSharedDatasets();
101
        return array_merge($sharedDatasetsByGroup, $sharedDatasets);
102
    }
103
104
    /**
105
     * get metadata of a dataset, shared with current user
106
     *
107
     * @NoAdminRequired
108
     * @param $id
109
     * @return array
110
     */
111
    public function getSharedDataset($id)
112
    {
113
        $sharedDataset = $this->ShareMapper->getSharedDataset($id);
114
        if (empty($sharedDataset)) {
115
            $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

115
            $groups = $this->groupManager->getUserGroupIds(/** @scrutinizer ignore-type */ $this->userSession->getUser());
Loading history...
116
            foreach ($groups as $group) {
117
                $sharedDataset = $this->ShareMapper->getDatasetByGroupId($group, $id);
118
                break;
119
            }
120
        }
121
        return $sharedDataset;
122
    }
123
124
    /**
125
     * delete all shares for a dataset
126
     *
127
     * @NoAdminRequired
128
     * @param $datasetId
129
     * @return bool
130
     */
131
    public function deleteShareByDataset($datasetId)
132
    {
133
        return $this->ShareMapper->deleteShareByDataset($datasetId);
134
    }
135
}