Passed
Push — master ( 37f48a...0ebc22 )
by Marcel
02:21
created

ShareController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * Data 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 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Controller;
13
14
use OCA\Analytics\Activity\ActivityManager;
15
use OCP\AppFramework\Controller;
16
use OCP\AppFramework\Http\DataResponse;
17
use OCP\ILogger;
18
use OCP\IRequest;
19
use OCP\Security\ISecureRandom;
20
21
class ShareController extends Controller
22
{
23
    const SHARE_TYPE_USER = 0;
24
    const SHARE_TYPE_LINK = 3;
25
26
    private $logger;
27
    private $DBController;
28
    private $secureRandom;
29
    private $ActivityManager;
30
31
    public function __construct(
32
        $appName,
33
        IRequest $request,
34
        ILogger $logger,
35
        DbController $DBController,
36
        ActivityManager $ActivityManager,
37
        ISecureRandom $secureRandom
38
    )
39
    {
40
        parent::__construct($appName, $request);
41
        $this->logger = $logger;
42
        $this->DBController = $DBController;
43
        $this->secureRandom = $secureRandom;
44
        $this->ActivityManager = $ActivityManager;
45
    }
46
47
    /**
48
     * get all dataset by token
49
     *
50
     * @NoAdminRequired
51
     * @param $token
52
     * @return array
53
     */
54
    public function getDatasetByToken($token)
55
    {
56
        return $this->DBController->getDatasetByToken($token);
57
    }
58
59
    /**
60
     * verify password hahes
61
     *
62
     * @NoAdminRequired
63
     * @param $password
64
     * @param $sharePassword
65
     * @return bool
66
     */
67
    public function verifyPassword($password, $sharePassword)
68
    {
69
        return password_verify($password, $sharePassword);
70
    }
71
72
    /**
73
     * get all datasets shared with user
74
     *
75
     * @NoAdminRequired
76
     */
77
    public function getSharedDatasets()
78
    {
79
        return $this->DBController->getSharedDatasets();
80
    }
81
82
    /**
83
     * get metadata of a dataset, shared with current user
84
     *
85
     * @NoAdminRequired
86
     * @param $id
87
     * @return array
88
     */
89
    public function getSharedDataset($id)
90
    {
91
        return $this->DBController->getSharedDataset($id);
92
    }
93
94
    /**
95
     * create a new share
96
     *
97
     * @NoAdminRequired
98
     * @param $datasetId
99
     * @param $type
100
     * @return bool
101
     */
102
    public function create($datasetId, $type)
103
    {
104
        $token = $this->generateToken();
105
        //$this->logger->error($type . $token);
106
        $this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_SHARE);
107
        return $this->DBController->createShare($datasetId, $type, null, $token);
108
    }
109
110
    /**
111
     * get all shares for a dataset
112
     *
113
     * @NoAdminRequired
114
     * @param $datasetId
115
     * @return DataResponse
116
     */
117
    public function read($datasetId)
118
    {
119
        return new DataResponse($this->DBController->getShares($datasetId));
120
    }
121
122
    /**
123
     * update/set share password
124
     *
125
     * @NoAdminRequired
126
     * @param $shareId
127
     * @param $password
128
     * @return bool
129
     */
130
    public function update($shareId, $password)
131
    {
132
        //$this->logger->error($shareId . $password);
133
        if ($password !== '') $password = password_hash($password, PASSWORD_DEFAULT);
134
        else $password = null;
135
        return $this->DBController->updateShare($shareId, $password);
136
    }
137
138
    /**
139
     * delete a share
140
     *
141
     * @NoAdminRequired
142
     * @param $shareId
143
     * @return bool
144
     */
145
    public function delete($shareId)
146
    {
147
        return $this->DBController->deleteShare($shareId);
148
    }
149
150
    /**
151
     * delete all shares for a dataset
152
     *
153
     * @NoAdminRequired
154
     * @param $datasetId
155
     * @return bool
156
     */
157
    public function deleteShareByDataset($datasetId)
158
    {
159
        return $this->DBController->deleteShareByDataset($datasetId);
160
    }
161
162
    /**
163
     * generate to token used to authenticate federated shares
164
     *
165
     * @return string
166
     */
167
    private function generateToken()
168
    {
169
        $token = $this->secureRandom->generate(
170
            15,
171
            ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
172
        return $token;
173
    }
174
}