cloud_bbb/lib/Controller/RoomShareController.php

177 lines
3.6 KiB
PHP
Raw Normal View History

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