cloud_bbb/lib/Controller/JoinController.php

180 lines
4.7 KiB
PHP
Raw Permalink Normal View History

2020-04-26 11:36:41 +02:00
<?php
2020-04-26 11:36:41 +02:00
namespace OCA\BigBlueButton\Controller;
use OCA\BigBlueButton\BackgroundJob\IsRunningJob;
2020-05-16 17:14:17 +02:00
use OCA\BigBlueButton\BigBlueButton\API;
use OCA\BigBlueButton\BigBlueButton\Presentation;
2020-06-04 10:25:51 +02:00
use OCA\BigBlueButton\Db\Room;
use OCA\BigBlueButton\NoPermissionException;
use OCA\BigBlueButton\NotFoundException;
use OCA\BigBlueButton\Permission;
2020-09-23 12:33:09 +02:00
use OCA\BigBlueButton\Service\RoomService;
use OCP\AppFramework\Controller;
2020-04-26 11:36:41 +02:00
use OCP\AppFramework\Http\RedirectResponse;
2020-09-23 12:33:09 +02:00
use OCP\AppFramework\Http\TemplateResponse;
use OCP\BackgroundJob\IJobList;
2020-04-26 11:36:41 +02:00
use OCP\IRequest;
use OCP\IURLGenerator;
2020-04-26 11:36:41 +02:00
use OCP\IUserSession;
class JoinController extends Controller {
/** @var string */
protected $token;
/** @var Room|null */
2020-06-04 10:25:51 +02:00
protected $room;
2020-04-26 12:14:06 +02:00
/** @var RoomService */
private $service;
/** @var IURLGenerator */
private $urlGenerator;
2020-04-26 12:14:06 +02:00
/** @var IUserSession */
private $userSession;
2020-05-16 17:14:17 +02:00
/** @var API */
private $api;
2020-04-26 12:14:06 +02:00
/** @var Permission */
private $permission;
/** @var IJobList */
private $jobList;
2020-04-26 12:14:06 +02:00
public function __construct(
string $appName,
IRequest $request,
RoomService $service,
IURLGenerator $urlGenerator,
2020-04-26 12:14:06 +02:00
IUserSession $userSession,
API $api,
Permission $permission,
IJobList $jobList
2020-04-26 12:14:06 +02:00
) {
parent::__construct($appName, $request);
2020-04-26 12:14:06 +02:00
$this->service = $service;
$this->urlGenerator = $urlGenerator;
2020-04-26 12:14:06 +02:00
$this->userSession = $userSession;
2020-05-16 17:14:17 +02:00
$this->api = $api;
$this->permission = $permission;
$this->jobList = $jobList;
2020-04-26 12:14:06 +02:00
}
public function setToken(string $token): void {
$this->token = $token;
2020-06-04 10:25:51 +02:00
$this->room = null;
2020-04-26 12:14:06 +02:00
}
public function isValidToken(): bool {
2020-06-04 10:25:51 +02:00
$room = $this->getRoom();
2020-04-26 12:14:06 +02:00
return $room !== null;
}
/**
* @PublicPage
*
2020-04-26 12:14:06 +02:00
* @NoCSRFRequired
*
* @return RedirectResponse|TemplateResponse
2020-04-26 12:14:06 +02:00
*/
public function index($displayname, $u = '', $filename = '', $password = '') {
2020-06-04 10:25:51 +02:00
$room = $this->getRoom();
2020-04-26 12:14:06 +02:00
if ($room === null) {
2020-05-16 17:14:17 +02:00
throw new NotFoundException();
2020-04-26 12:14:06 +02:00
}
2021-01-22 19:12:55 +01:00
$moderatorToken = $this->request->getParam('moderatorToken');
if (!empty($moderatorToken) && $moderatorToken !== $room->moderatorToken) {
throw new NoPermissionException();
}
2020-06-17 15:09:20 +02:00
$displayname = trim($displayname);
2020-05-16 17:14:17 +02:00
$userId = null;
$presentation = null;
2020-04-26 12:14:06 +02:00
if ($this->userSession->isLoggedIn()) {
$user = $this->userSession->getUser();
$displayname = $user->getDisplayName();
2020-05-16 17:14:17 +02:00
$userId = $user->getUID();
2020-09-02 10:05:36 +02:00
if ($room->access === Room::ACCESS_INTERNAL_RESTRICTED && !$this->permission->isUser($room, $userId)) {
throw new NoPermissionException();
}
if ($this->permission->isAdmin($room, $userId)) {
2020-05-16 17:14:17 +02:00
$presentation = new Presentation($u, $filename);
}
2020-09-02 10:05:36 +02:00
} elseif ($room->access === Room::ACCESS_INTERNAL || $room->access === Room::ACCESS_INTERNAL_RESTRICTED) {
2020-08-31 16:27:13 +02:00
return new RedirectResponse($this->getLoginUrl());
} elseif (empty($displayname) || strlen($displayname) < 3 || ($room->access === Room::ACCESS_PASSWORD && $password !== $room->password)) {
$response = new TemplateResponse($this->appName, 'join', [
'room' => $room->name,
'wrongdisplayname' => !empty($displayname) && strlen($displayname) < 3,
'passwordRequired' => $room->access === Room::ACCESS_PASSWORD,
'wrongPassword' => $password !== $room->password && $password !== '',
'loginUrl' => $this->getLoginUrl(),
2020-04-26 12:14:06 +02:00
], 'guest');
return $response;
}
2021-01-22 19:12:55 +01:00
$isModerator = (!empty($moderatorToken) && $moderatorToken === $room->moderatorToken) || $this->permission->isModerator($room, $userId);
if ($room->requireModerator && !$isModerator && !$this->api->isRunning($room)) {
return new TemplateResponse($this->appName, 'waiting', [
'room' => $room->name,
'name' => $displayname,
], 'guest');
}
2020-05-16 17:14:17 +02:00
$creationDate = $this->api->createMeeting($room, $presentation);
2021-01-22 19:12:55 +01:00
$joinUrl = $this->api->createJoinUrl($room, $creationDate, $displayname, $isModerator, $userId);
2020-05-16 17:14:17 +02:00
$this->markAsRunning($room);
2020-08-25 13:17:35 +02:00
\OCP\Util::addHeader('meta', ['http-equiv' => 'refresh', 'content' => '3;url='.$joinUrl]);
2020-04-26 12:14:06 +02:00
2020-08-25 13:17:35 +02:00
return new TemplateResponse($this->appName, 'forward', [
2020-08-28 11:36:15 +02:00
'room' => $room->name,
'url' => $joinUrl,
2020-08-25 13:17:35 +02:00
], 'guest');
2020-04-26 12:14:06 +02:00
}
2020-06-04 10:25:51 +02:00
private function getRoom(): ?Room {
2020-06-04 10:25:51 +02:00
if ($this->room === null) {
$this->room = $this->service->findByUid($this->token);
}
return $this->room;
}
2020-08-31 16:27:13 +02:00
private function getLoginUrl(): string {
return $this->urlGenerator->linkToRoute('core.login.showLoginForm', [
'redirect_url' => $this->urlGenerator->linkToRoute(
'bbb.join.index',
['token' => $this->token]
),
]);
}
private function markAsRunning(Room $room) {
if (!$room->running) {
$this->service->updateRunning($room->getId(), true);
}
if (!$this->jobList->has(IsRunningJob::class, [
'id' => $room->id,
])) {
$this->jobList->add(IsRunningJob::class, [
'id' => $room->id,
]);
}
}
2020-04-26 11:36:41 +02:00
}