2020-06-15 17:23:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OCA\BigBlueButton\Controller;
|
|
|
|
|
2020-09-23 12:33:09 +02:00
|
|
|
use OCA\BigBlueButton\CircleHelper;
|
2020-06-15 17:23:53 +02:00
|
|
|
use OCA\BigBlueButton\Db\RoomShare;
|
|
|
|
use OCA\BigBlueButton\Service\RoomService;
|
|
|
|
use OCA\BigBlueButton\Service\RoomShareNotFound;
|
2020-09-23 12:33:09 +02:00
|
|
|
use OCA\BigBlueButton\Service\RoomShareService;
|
|
|
|
use OCP\AppFramework\Controller;
|
2020-06-15 17:23:53 +02:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
|
2020-09-23 12:33:09 +02:00
|
|
|
use OCP\IRequest;
|
2020-06-15 17:23:53 +02:00
|
|
|
use OCP\IUserManager;
|
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
class RoomShareController extends Controller {
|
2020-06-15 17:23:53 +02:00
|
|
|
/** @var RoomShareService */
|
|
|
|
private $service;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $userId;
|
|
|
|
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
|
|
|
/** @var RoomService */
|
|
|
|
private $roomService;
|
|
|
|
|
2020-08-29 13:20:28 +02:00
|
|
|
/** @var CircleHelper */
|
|
|
|
private $circleHelper;
|
|
|
|
|
2020-06-15 17:23:53 +02:00
|
|
|
use Errors;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
$appName,
|
|
|
|
IRequest $request,
|
|
|
|
RoomShareService $service,
|
|
|
|
IUserManager $userManager,
|
|
|
|
RoomService $roomService,
|
2020-08-29 13:20:28 +02:00
|
|
|
CircleHelper $circleHelper,
|
2020-06-15 17:23:53 +02:00
|
|
|
$userId
|
|
|
|
) {
|
|
|
|
parent::__construct($appName, $request);
|
|
|
|
$this->service = $service;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->roomService = $roomService;
|
2020-08-29 13:20:28 +02:00
|
|
|
$this->circleHelper = $circleHelper;
|
2020-06-15 17:23:53 +02:00
|
|
|
$this->userId = $userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2020-06-19 09:28:58 +02:00
|
|
|
public function index(): DataResponse {
|
2020-06-15 17:23:53 +02:00
|
|
|
$roomId = $this->request->getParam('id');
|
|
|
|
|
|
|
|
if ($roomId === null) {
|
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->isUserAllowed($roomId)) {
|
|
|
|
return new DataResponse([], Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
$roomShares = $this->service->findAll($roomId);
|
2020-08-29 13:20:28 +02:00
|
|
|
$shares = [];
|
|
|
|
|
|
|
|
$circleAPI = $this->circleHelper->getCircleAPI();
|
2020-06-15 17:23:53 +02:00
|
|
|
|
|
|
|
/** @var RoomShare $roomShare */
|
|
|
|
foreach ($roomShares as $roomShare) {
|
2020-08-29 13:20:28 +02:00
|
|
|
if ($roomShare->getShareType() === RoomShare::SHARE_TYPE_USER) {
|
|
|
|
$shareWithUser = $this->userManager->get($roomShare->getShareWith());
|
|
|
|
|
|
|
|
if ($shareWithUser === null) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-15 17:23:53 +02:00
|
|
|
|
|
|
|
$roomShare->setShareWithDisplayName($shareWithUser->getDisplayName());
|
2020-08-29 13:20:28 +02:00
|
|
|
} elseif ($roomShare->getShareType() === RoomShare::SHARE_TYPE_CIRCLE) {
|
|
|
|
if ($circleAPI === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$circle = $circleAPI->detailsCircle($roomShare->getShareWith());
|
|
|
|
|
|
|
|
if ($circle === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$roomShare->setShareWithDisplayName($circle->getName());
|
2020-06-15 17:23:53 +02:00
|
|
|
}
|
2020-08-29 13:20:28 +02:00
|
|
|
|
|
|
|
$shares[] = $roomShare;
|
2020-06-15 17:23:53 +02:00
|
|
|
}
|
|
|
|
|
2020-08-29 13:20:28 +02:00
|
|
|
return new DataResponse($shares);
|
2020-06-15 17:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
|
|
|
public function create(
|
|
|
|
int $roomId,
|
|
|
|
int $shareType,
|
|
|
|
string $shareWith,
|
|
|
|
int $permission
|
|
|
|
): DataResponse {
|
|
|
|
if (!$this->isUserAllowed($roomId)) {
|
|
|
|
return new DataResponse(null, Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DataResponse($this->service->create(
|
|
|
|
$roomId,
|
|
|
|
$shareType,
|
|
|
|
$shareWith,
|
|
|
|
$permission
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
|
|
|
public function update(
|
|
|
|
int $id,
|
|
|
|
int $roomId,
|
|
|
|
int $shareType,
|
|
|
|
string $shareWith,
|
|
|
|
int $permission
|
|
|
|
): DataResponse {
|
|
|
|
if (!$this->isUserAllowed($roomId)) {
|
|
|
|
return new DataResponse(null, Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->handleNotFound(function () use (
|
|
|
|
$id,
|
|
|
|
$roomId,
|
|
|
|
$shareType,
|
|
|
|
$shareWith,
|
|
|
|
$permission) {
|
|
|
|
return $this->service->update(
|
|
|
|
$id,
|
|
|
|
$roomId,
|
|
|
|
$shareType,
|
|
|
|
$shareWith,
|
|
|
|
$permission
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2020-06-19 09:28:58 +02:00
|
|
|
public function destroy(int $id): DataResponse {
|
2020-06-15 17:23:53 +02:00
|
|
|
return $this->handleNotFound(function () use ($id) {
|
|
|
|
$roomShare = $this->service->find($id);
|
|
|
|
|
|
|
|
if (!$this->isUserAllowed($roomShare->getRoomId())) {
|
|
|
|
return new DataResponse(null, Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->service->delete($id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
private function isUserAllowed(int $roomId): bool {
|
2020-06-15 17:23:53 +02:00
|
|
|
try {
|
2020-06-17 10:56:28 +02:00
|
|
|
$room = $this->roomService->find($roomId);
|
2020-06-15 17:23:53 +02:00
|
|
|
|
2020-06-17 10:56:28 +02:00
|
|
|
return $room->getUserId() === $this->userId;
|
2020-06-15 17:23:53 +02:00
|
|
|
} catch (RoomShareNotFound $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|