66 lines
2.2 KiB
PHP
66 lines
2.2 KiB
PHP
<?php
|
|
require __DIR__ . '/_bootstrap.php';
|
|
|
|
/**
|
|
* @OA\Post(
|
|
* path="/share-update.php",
|
|
* tags={"Sharing"},
|
|
* summary="Update a sharing condition",
|
|
* security={{"bearerAuth":{}}},
|
|
* @OA\RequestBody(required=true, @OA\JsonContent(
|
|
* required={"idsharing"},
|
|
* @OA\Property(property="idsharing", type="integer"),
|
|
* @OA\Property(property="shared_email", type="string", format="email"),
|
|
* @OA\Property(property="role_id", type="integer"),
|
|
* @OA\Property(property="sharing_type", type="string"),
|
|
* @OA\Property(property="shared_sections", type="array", @OA\Items(type="integer")),
|
|
* @OA\Property(property="expiration_date", type="string", format="date")
|
|
* )),
|
|
* @OA\Response(response=200, description="OK", @OA\JsonContent(
|
|
* @OA\Property(property="data", ref="#/components/schemas/Share"))),
|
|
* @OA\Response(response=403, ref="#/components/responses/Forbidden")
|
|
* )
|
|
*/
|
|
require_method('POST');
|
|
$user = require_auth($pdo);
|
|
$in = body();
|
|
|
|
$idsharing = (int) ($in['idsharing'] ?? 0);
|
|
if ($idsharing <= 0) {
|
|
json_error(422, 'idsharing is required');
|
|
}
|
|
if (!user_owns_share($pdo, $user, $idsharing)) {
|
|
json_error(403, 'No access to this share');
|
|
}
|
|
|
|
$map = [
|
|
'shared_email' => fn ($v) => (string) $v,
|
|
'role_id' => fn ($v) => $v !== '' ? (int) $v : null,
|
|
'sharing_type' => fn ($v) => (string) $v,
|
|
'expiration_date' => fn ($v) => $v !== '' ? $v : null,
|
|
'shared_sections' => fn ($v) => is_array($v) ? json_encode(array_map('intval', $v)) : null,
|
|
];
|
|
|
|
$data = [];
|
|
foreach ($map as $col => $cast) {
|
|
if (array_key_exists($col, $in)) {
|
|
$data[$col] = $cast($in[$col]);
|
|
}
|
|
}
|
|
|
|
if ($data) {
|
|
$set = implode(', ', array_map(fn ($c) => "$c = ?", array_keys($data)));
|
|
$pdo->prepare("UPDATE home_sharing SET $set WHERE idsharing = ? AND iduser = ?")
|
|
->execute([...array_values($data), $idsharing, $user['id']]);
|
|
}
|
|
|
|
$stmt = $pdo->prepare(
|
|
'SELECT hs.*, sr.role_name, sr.description AS role_description, sr.permissions AS role_permissions
|
|
FROM home_sharing hs
|
|
LEFT JOIN sharing_roles sr ON sr.idrole = hs.role_id
|
|
WHERE hs.idsharing = ? LIMIT 1'
|
|
);
|
|
$stmt->execute([$idsharing]);
|
|
|
|
json_data(present_share($stmt->fetch()));
|