1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Tom Needham <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\Files\Controller; |
23
|
|
|
|
24
|
|
|
use OCA\Files\Service\TransferOwnership\TransferRequestManager; |
25
|
|
|
use OCP\AppFramework\Controller; |
26
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
27
|
|
|
use OCP\AppFramework\Http; |
28
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
29
|
|
|
use OCP\ILogger; |
30
|
|
|
use OCP\IRequest; |
31
|
|
|
use OCP\IUserSession; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class TransferController |
35
|
|
|
* |
36
|
|
|
* @package OCA\Files\Controller |
37
|
|
|
*/ |
38
|
|
|
class TransferController extends Controller { |
39
|
|
|
/** @var string */ |
40
|
|
|
protected $appName; |
41
|
|
|
/** @var IRequest */ |
42
|
|
|
protected $request; |
43
|
|
|
/** @var TransferRequestManager */ |
44
|
|
|
protected $transferManager; |
45
|
|
|
/** @var IUserSession */ |
46
|
|
|
protected $userSession; |
47
|
|
|
/** @var ILogger */ |
48
|
|
|
protected $logger; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $appName |
52
|
|
|
* @param IRequest $request |
53
|
|
|
* @param TransferRequestManager $transferManager |
54
|
|
|
*/ |
55
|
|
|
public function __construct($appName, |
56
|
|
|
IRequest $request, |
57
|
|
|
TransferRequestManager $transferManager, |
58
|
|
|
IUserSession $userSession, |
59
|
|
|
ILogger $logger |
60
|
|
|
) { |
61
|
|
|
parent::__construct($appName, $request); |
62
|
|
|
$this->appName = $appName; |
63
|
|
|
$this->request = $request; |
64
|
|
|
$this->transferManager = $transferManager; |
65
|
|
|
$this->userSession = $userSession; |
66
|
|
|
$this->logger = $logger; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @NoAdminRequired |
71
|
|
|
*/ |
72
|
|
|
public function transfer() { |
73
|
|
|
$file = $this->request->getParam('file'); |
74
|
|
|
$dir = $this->request->getParam('dir'); |
75
|
|
|
$destinationUser = $this->request->getParam('uid'); |
76
|
|
|
$destinationUser = \OC::$server->getUserManager()->get($destinationUser); |
77
|
|
|
$sessionUser = \OC::$server->getUserSession()->getUser(); |
78
|
|
|
$fileId = \OC::$server->getUserFolder($sessionUser->getUID())->get($dir.'/'.$file)->getId(); |
79
|
|
|
try { |
80
|
|
|
$this->transferManager->newTransferRequest($sessionUser, $destinationUser, $fileId); |
|
|
|
|
81
|
|
|
return new JSONResponse(); |
82
|
|
|
} catch (\Exception $e) { |
83
|
|
|
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @NoAdminRequired |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function accept($requestId) { |
91
|
|
|
// Check this user is the recipient of this request |
92
|
|
|
try { |
93
|
|
|
// Not found, bad request |
94
|
|
|
$request = $this->transferManager->getRequestById($requestId); |
95
|
|
|
} catch (DoesNotExistException $e) { |
96
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
97
|
|
|
} |
98
|
|
|
$sessionUser = $this->userSession->getUser(); |
99
|
|
|
if ($request->getDestinationUserId() !== $sessionUser->getUID()) { |
|
|
|
|
100
|
|
|
// This isn't their request to respond to, bad request |
101
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
try { |
105
|
|
|
$this->transferManager->acceptRequest($request); |
|
|
|
|
106
|
|
|
return new JSONResponse([]); |
107
|
|
|
} catch (\Exception $e) { |
108
|
|
|
$this->logger->logException($e, ['app' => 'files']); |
109
|
|
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @NoAdminRequired |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function reject($requestId) { |
117
|
|
|
// Check this user is the recipient of this request |
118
|
|
|
try { |
119
|
|
|
// Not found, bad request |
120
|
|
|
$request = $this->transferManager->getRequestById($requestId); |
121
|
|
|
} catch (DoesNotExistException $e) { |
122
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
123
|
|
|
} |
124
|
|
|
$sessionUser = $this->userSession->getUser(); |
125
|
|
|
if ($request->getDestinationUserId() !== $sessionUser->getUID()) { |
|
|
|
|
126
|
|
|
// This isn't their request to respond to, bad request |
127
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
try { |
131
|
|
|
$this->transferManager->rejectRequest($request); |
|
|
|
|
132
|
|
|
return new JSONResponse([]); |
133
|
|
|
} catch (\Exception $e) { |
134
|
|
|
$this->logger->logException($e, ['app' => 'files']); |
135
|
|
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
|
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: