2020-06-16 16:54:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OCA\BigBlueButton;
|
|
|
|
|
|
|
|
use Closure;
|
2020-09-23 12:33:09 +02:00
|
|
|
use OCA\BigBlueButton\Db\Restriction;
|
2020-06-16 16:54:50 +02:00
|
|
|
use OCA\BigBlueButton\Db\Room;
|
|
|
|
use OCA\BigBlueButton\Db\RoomShare;
|
2020-09-23 12:33:09 +02:00
|
|
|
use OCA\BigBlueButton\Service\RestrictionService;
|
|
|
|
use OCA\BigBlueButton\Service\RoomService;
|
|
|
|
use OCA\BigBlueButton\Service\RoomShareService;
|
2020-06-16 16:54:50 +02:00
|
|
|
use OCP\IGroupManager;
|
2020-09-23 12:33:09 +02:00
|
|
|
use OCP\IUserManager;
|
2020-06-16 16:54:50 +02:00
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
class Permission {
|
2020-06-16 16:54:50 +02:00
|
|
|
|
2020-08-27 17:21:34 +02:00
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
2020-06-16 16:54:50 +02:00
|
|
|
/** @var IGroupManager */
|
|
|
|
private $groupManager;
|
|
|
|
|
2020-08-27 17:21:34 +02:00
|
|
|
/** @var RoomService */
|
|
|
|
private $roomService;
|
|
|
|
|
|
|
|
/** @var RestrictionService */
|
|
|
|
private $restrictionService;
|
|
|
|
|
2020-06-16 16:54:50 +02:00
|
|
|
/** @var RoomShareService */
|
|
|
|
private $roomShareService;
|
|
|
|
|
2020-08-29 13:20:28 +02:00
|
|
|
/** @var CircleHelper */
|
|
|
|
private $circleHelper;
|
|
|
|
|
2020-06-16 16:54:50 +02:00
|
|
|
public function __construct(
|
2020-08-27 17:21:34 +02:00
|
|
|
IUserManager $userManager,
|
2020-06-16 16:54:50 +02:00
|
|
|
IGroupManager $groupManager,
|
2020-08-27 17:21:34 +02:00
|
|
|
RoomService $roomService,
|
|
|
|
RestrictionService $restrictionService,
|
2020-08-29 13:20:28 +02:00
|
|
|
RoomShareService $roomShareService,
|
|
|
|
CircleHelper $circleHelper
|
2020-06-16 16:54:50 +02:00
|
|
|
) {
|
2020-08-27 17:21:34 +02:00
|
|
|
$this->userManager = $userManager;
|
2020-06-16 16:54:50 +02:00
|
|
|
$this->groupManager = $groupManager;
|
2020-08-27 17:21:34 +02:00
|
|
|
$this->roomService = $roomService;
|
|
|
|
$this->restrictionService = $restrictionService;
|
2020-06-16 16:54:50 +02:00
|
|
|
$this->roomShareService = $roomShareService;
|
2020-08-29 13:20:28 +02:00
|
|
|
$this->circleHelper = $circleHelper;
|
2020-06-16 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:21:34 +02:00
|
|
|
public function getRestriction(string $uid): Restriction {
|
|
|
|
$user = $this->userManager->get($uid);
|
|
|
|
$groupIds = $this->groupManager->getUserGroupIds($user);
|
|
|
|
|
|
|
|
return $this->restrictionService->findByGroupIds($groupIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isAllowedToCreateRoom(string $uid) {
|
2020-08-29 13:20:28 +02:00
|
|
|
$numberOfCreatedRooms = count($this->roomService->findAll($uid, [], []));
|
2020-08-27 17:21:34 +02:00
|
|
|
$restriction = $this->getRestriction($uid);
|
|
|
|
|
|
|
|
return $restriction->getMaxRooms() < 0 || $restriction->getMaxRooms() > $numberOfCreatedRooms;
|
|
|
|
}
|
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
public function isUser(Room $room, ?string $uid) {
|
2020-06-16 16:54:50 +02:00
|
|
|
return $this->hasPermission($room, $uid, function (RoomShare $share) {
|
|
|
|
return $share->hasUserPermission();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
public function isModerator(Room $room, ?string $uid) {
|
2020-06-17 08:19:54 +02:00
|
|
|
if ($room->everyoneIsModerator) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-16 16:54:50 +02:00
|
|
|
return $this->hasPermission($room, $uid, function (RoomShare $share) {
|
|
|
|
return $share->hasModeratorPermission();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
public function isAdmin(Room $room, ?string $uid) {
|
2020-06-16 16:54:50 +02:00
|
|
|
return $this->hasPermission($room, $uid, function (RoomShare $share) {
|
|
|
|
return $share->hasAdminPermission();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
private function hasPermission(Room $room, ?string $uid, Closure $hasPermission): bool {
|
2020-06-16 16:54:50 +02:00
|
|
|
if ($uid === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($uid === $room->userId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$shares = $this->roomShareService->findAll($room->id);
|
|
|
|
|
|
|
|
/** @var RoomShare $share */
|
|
|
|
foreach ($shares as $share) {
|
|
|
|
if (!$hasPermission($share)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($share->getShareType() === RoomShare::SHARE_TYPE_USER) {
|
|
|
|
if ($share->getShareWith() === $uid) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} elseif ($share->getShareType() === RoomShare::SHARE_TYPE_GROUP) {
|
|
|
|
if ($this->groupManager->isInGroup($uid, $share->getShareWith())) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-29 13:20:28 +02:00
|
|
|
} elseif ($share->getShareType() === RoomShare::SHARE_TYPE_CIRCLE) {
|
|
|
|
if ($this->circleHelper->isInCircle($uid, $share->getShareWith())) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-16 16:54:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|