Passed
Push — master ( c3551c...649947 )
by Marcel
03:09
created

ShareController::generateToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Nextcloud Data
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\data\Controller;
13
14
use OCP\AppFramework\Controller;
15
use OCP\AppFramework\Http\DataResponse;
16
use OCP\ILogger;
17
use OCP\IRequest;
18
use OCP\Security\ISecureRandom;
19
20
class ShareController extends Controller
21
{
22
    private $logger;
23
    private $DBController;
24
    private $secureRandom;
25
26
    public function __construct(
27
        $appName,
28
        IRequest $request,
29
        ILogger $logger,
30
        DbController $DBController,
31
        ISecureRandom $secureRandom
32
    )
33
    {
34
        parent::__construct($appName, $request);
35
        $this->logger = $logger;
36
        $this->DBController = $DBController;
37
        $this->secureRandom = $secureRandom;
38
    }
39
40
    /**
41
     * get all datasets
42
     * @NoAdminRequired
43
     */
44
    public function getDatasetByToken($token)
45
    {
46
        return $this->DBController->getDatasetByToken($token);
47
    }
48
49
    /**
50
     * get all datasets
51
     * @NoAdminRequired
52
     */
53
    public function verifyPassword($password, $sharePassword)
54
    {
55
        return password_verify($password, $sharePassword);
56
    }
57
58
    /**
59
     * get all datasets shared with user
60
     * @NoAdminRequired
61
     */
62
    public function getSharedDatasets()
63
    {
64
        return $this->DBController->getSharedDatasets();
65
    }
66
67
    /**
68
     * get all datasets shared with user
69
     * @NoAdminRequired
70
     * @param $id
71
     * @return
72
     */
73
    public function getSharedDataset($id)
74
    {
75
        return $this->DBController->getSharedDataset($id);
76
    }
77
78
    /**
79
     * get all datasets shared with user
80
     * @NoAdminRequired
81
     * @param $id
82
     * @return
83
     */
84
    public function create($datasetId, $type)
85
    {
86
        if ($type === '3') { // Link Share
87
            $token = $this->generateToken();
88
        }
89
        $this->logger->error($type . $token);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $token does not seem to be defined for all execution paths leading up to this point.
Loading history...
90
91
        return new DataResponse($this->DBController->createShare($datasetId, $type, null, $token));
92
    }
93
94
    /**
95
     * get all datasets shared with user
96
     * @NoAdminRequired
97
     * @param $id
98
     * @return
99
     */
100
    public function read($datasetId)
101
    {
102
        return new DataResponse($this->DBController->getShare($datasetId));
103
    }
104
105
    /**
106
     * get all datasets shared with user
107
     * @NoAdminRequired
108
     * @param $id
109
     * @return
110
     */
111
    public function update($shareId, $password)
112
    {
113
        $this->logger->error($shareId . $password);
114
        if ($password !== '') $password = password_hash($password, PASSWORD_DEFAULT);
115
        else $password = null;
116
        $this->logger->error($shareId . $password);
117
        return new DataResponse($this->DBController->updateShare($shareId, $password));
118
    }
119
120
    /**
121
     * get all datasets shared with user
122
     * @NoAdminRequired
123
     * @param $id
124
     * @return
125
     */
126
    public function delete($shareId)
127
    {
128
        return new DataResponse($this->DBController->deleteShare($shareId));
129
    }
130
131
    /**
132
     * generate to token used to authenticate federated shares
133
     *
134
     * @return string
135
     */
136
    private function generateToken()
137
    {
138
        $token = $this->secureRandom->generate(
139
            15,
140
            ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
141
        return $token;
142
    }
143
}