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

ShareController::getDatasetByToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
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\Controller;
13
14
use OCA\Analytics\Activity\ActivityManager;
15
use OCA\Analytics\Db\ShareMapper;
16
use OCP\AppFramework\Controller;
17
use OCP\AppFramework\Http\DataResponse;
18
use OCP\IGroupManager;
19
use OCP\ILogger;
20
use OCP\IRequest;
21
use OCP\IUserManager;
22
use OCP\IUserSession;
23
use OCP\Security\ISecureRandom;
24
25
class ShareController extends Controller
26
{
27
    const SHARE_TYPE_USER = 0;
28
    const SHARE_TYPE_GROUP = 1;
29
    const SHARE_TYPE_USERGROUP = 2;
30
    const SHARE_TYPE_LINK = 3;
31
    const SHARE_TYPE_ROOM = 10;
32
33
    private $logger;
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
        $appName,
46
        IRequest $request,
47
        ILogger $logger,
48
        ShareMapper $ShareMapper,
49
        ActivityManager $ActivityManager,
50
        IGroupManager $groupManager,
51
        ISecureRandom $secureRandom,
52
        IUserSession $userSession,
53
        IUserManager $userManager
54
    )
55
    {
56
        parent::__construct($appName, $request);
57
        $this->logger = $logger;
58
        $this->ShareMapper = $ShareMapper;
59
        $this->secureRandom = $secureRandom;
60
        $this->groupManager = $groupManager;
61
        $this->ActivityManager = $ActivityManager;
62
        $this->userSession = $userSession;
63
        $this->userManager = $userManager;
64
    }
65
66
    /**
67
     * create a new share
68
     *
69
     * @NoAdminRequired
70
     * @param $datasetId
71
     * @param $type
72
     * @param $user
73
     * @return bool
74
     */
75
    public function create($datasetId, $type, $user)
76
    {
77
        if ((int)$type === self::SHARE_TYPE_LINK) {
78
            $token = $this->generateToken();
79
        } else {
80
            $token = null;
81
        }
82
        $this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_SHARE);
83
        return $this->ShareMapper->createShare($datasetId, $type, $user, $token);
84
    }
85
86
    /**
87
     * get all shares for a dataset
88
     *
89
     * @NoAdminRequired
90
     * @param $datasetId
91
     * @return DataResponse
92
     */
93
    public function read($datasetId)
94
    {
95
        $shares = $this->ShareMapper->getShares($datasetId);
96
        foreach ($shares as &$share) {
97
            if ($share['type'] === 0) {
98
                $share['displayName'] = $this->userManager->get($share['uid_owner'])->getDisplayName();
99
            }
100
            $share['pass'] = $share['pass'] !== null;
101
        }
102
        return new DataResponse($shares);
103
    }
104
105
    /**
106
     * update/set share password
107
     *
108
     * @NoAdminRequired
109
     * @param $shareId
110
     * @param $password
111
     * @return bool
112
     */
113
    public function update($shareId, $password)
114
    {
115
        //$this->logger->error($shareId . $password);
116
        if ($password !== '') $password = password_hash($password, PASSWORD_DEFAULT);
117
        else $password = null;
118
        return $this->ShareMapper->updateShare($shareId, $password);
119
    }
120
121
    /**
122
     * delete a share
123
     *
124
     * @NoAdminRequired
125
     * @param $shareId
126
     * @return bool
127
     */
128
    public function delete($shareId)
129
    {
130
        return $this->ShareMapper->deleteShare($shareId);
131
    }
132
133
    /**
134
     * generate to token used to authenticate federated shares
135
     *
136
     * @return string
137
     */
138
    private function generateToken()
139
    {
140
        $token = $this->secureRandom->generate(
141
            15,
142
            ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
143
        return $token;
144
    }
145
}