Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/OpenViduController.php
Show First 20 Lines • Show All 406 Lines • ▼ Show 20 Lines | class OpenViduController extends Controller | ||||
{ | { | ||||
$connection = Connection::where('id', $conn)->first(); | $connection = Connection::where('id', $conn)->first(); | ||||
// There's no such connection, bye bye | // There's no such connection, bye bye | ||||
if (!$connection || $connection->room->name != $id) { | if (!$connection || $connection->room->name != $id) { | ||||
return $this->errorResponse(404, \trans('meet.connection-not-found')); | return $this->errorResponse(404, \trans('meet.connection-not-found')); | ||||
} | } | ||||
foreach (request()->input() as $key => $value) { | |||||
switch ($key) { | |||||
case 'hand': | |||||
// Only possible on user's own connection(s) | |||||
if (!$this->isSelfConnection($connection)) { | |||||
return $this->errorResponse(403); | |||||
} | |||||
if ($value) { | |||||
// Store current time, so we know the order in the queue | |||||
$connection->metadata = ['hand' => time()] + $connection->metadata; | |||||
} else { | |||||
$connection->metadata = array_diff_key($connection->metadata, ['hand' => 0]); | |||||
} | |||||
break; | |||||
case 'role': | |||||
// Only the moderator can do it | // Only the moderator can do it | ||||
if (!$this->isModerator($connection->room)) { | if (!$this->isModerator($connection->room)) { | ||||
return $this->errorResponse(403); | return $this->errorResponse(403); | ||||
} | } | ||||
foreach (request()->input() as $key => $value) { | |||||
switch ($key) { | |||||
case 'role': | |||||
// The 'owner' role is not assignable | // The 'owner' role is not assignable | ||||
if ($value & Room::ROLE_OWNER && !($connection->role & Room::ROLE_OWNER)) { | if ($value & Room::ROLE_OWNER && !($connection->role & Room::ROLE_OWNER)) { | ||||
return $this->errorResponse(403); | return $this->errorResponse(403); | ||||
} elseif (!($value & Room::ROLE_OWNER) && ($connection->role & Room::ROLE_OWNER)) { | } elseif (!($value & Room::ROLE_OWNER) && ($connection->role & Room::ROLE_OWNER)) { | ||||
return $this->errorResponse(403); | return $this->errorResponse(403); | ||||
} | } | ||||
// The room owner has always a 'moderator' role | // The room owner has always a 'moderator' role | ||||
if (!($value & Room::ROLE_MODERATOR) && $connection->role & Room::ROLE_OWNER) { | if (!($value & Room::ROLE_MODERATOR) && $connection->role & Room::ROLE_OWNER) { | ||||
$value |= Room::ROLE_MODERATOR; | $value |= Room::ROLE_MODERATOR; | ||||
} | } | ||||
// Promotion to publisher? Put the user hand down | |||||
if ($value & Room::ROLE_PUBLISHER && !($connection->role & Room::ROLE_PUBLISHER)) { | |||||
$connection->metadata = array_diff_key($connection->metadata, ['hand' => 0]); | |||||
} | |||||
$connection->{$key} = $value; | $connection->{$key} = $value; | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
// The connection observer will send a signal to everyone when needed | // The connection observer will send a signal to everyone when needed | ||||
$connection->save(); | $connection->save(); | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | protected function isModerator(Room $room): bool | ||||
) { | ) { | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
/** | /** | ||||
* Check if current user "owns" the specified connection. | |||||
* | |||||
* @param \App\OpenVidu\Connection $connection The connection | |||||
* | |||||
* @return bool | |||||
*/ | |||||
protected function isSelfConnection(Connection $connection): bool | |||||
{ | |||||
return ($conn = $this->getConnectionFromRequest()) | |||||
&& $conn->id === $connection->id; | |||||
} | |||||
/** | |||||
* Get the connection object for the token in current request headers. | * Get the connection object for the token in current request headers. | ||||
* It will also validate the token. | * It will also validate the token. | ||||
* | * | ||||
* @return \App\OpenVidu\Connection|null Connection (if exists and the token is valid) | * @return \App\OpenVidu\Connection|null Connection (if exists and the token is valid) | ||||
*/ | */ | ||||
protected function getConnectionFromRequest() | protected function getConnectionFromRequest() | ||||
{ | { | ||||
// Authenticate the user via the extra request header | // Authenticate the user via the extra request header | ||||
Show All 14 Lines |