78 lines
3.2 KiB
PHP
78 lines
3.2 KiB
PHP
<?php
|
|
require __DIR__ . '/_bootstrap.php';
|
|
|
|
/**
|
|
* @OA\Post(
|
|
* path="/share-save.php",
|
|
* tags={"Sharing"},
|
|
* summary="Share a home by e-mail",
|
|
* description="status=accepted if the e-mail exists in auth_users, otherwise pending.",
|
|
* security={{"bearerAuth":{}}},
|
|
* @OA\RequestBody(required=true, @OA\JsonContent(
|
|
* required={"idhome","shared_email","sharing_type","role_id"},
|
|
* @OA\Property(property="idhome", 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", enum={"read-only","add-documents","full-control"}),
|
|
* @OA\Property(property="shared_sections", type="array", @OA\Items(type="integer")),
|
|
* @OA\Property(property="expiration_date", type="string", format="date")
|
|
* )),
|
|
* @OA\Response(response=201, description="Created", @OA\JsonContent(
|
|
* @OA\Property(property="data", ref="#/components/schemas/Share"))),
|
|
* @OA\Response(response=403, ref="#/components/responses/Forbidden"),
|
|
* @OA\Response(response=422, ref="#/components/responses/ValidationError")
|
|
* )
|
|
*/
|
|
require_method('POST');
|
|
$user = require_auth($pdo);
|
|
$in = body();
|
|
|
|
$idhome = (int) ($in['idhome'] ?? 0);
|
|
$email = trim((string) ($in['shared_email'] ?? ''));
|
|
$type = trim((string) ($in['sharing_type'] ?? ''));
|
|
|
|
$roleId = (int) ($in['role_id'] ?? 0);
|
|
|
|
$fields = [];
|
|
if ($idhome <= 0) { $fields['idhome'] = ['Required']; }
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $fields['shared_email'] = ['Valid email required']; }
|
|
if ($type === '') { $fields['sharing_type'] = ['Required']; }
|
|
if ($roleId <= 0) { $fields['role_id'] = ['Required']; }
|
|
if ($fields) {
|
|
json_error(422, 'Validation failed', $fields);
|
|
}
|
|
if (!user_owns_home($pdo, $user, $idhome)) {
|
|
json_error(403, 'No access to this home');
|
|
}
|
|
|
|
$sections = isset($in['shared_sections']) && is_array($in['shared_sections'])
|
|
? json_encode(array_map('intval', $in['shared_sections']))
|
|
: null;
|
|
$expiry = !empty($in['expiration_date']) ? $in['expiration_date'] : null;
|
|
|
|
// Is the recipient registered.
|
|
$recv = $pdo->prepare('SELECT id FROM auth_users WHERE email = ? LIMIT 1');
|
|
$recv->execute([$email]);
|
|
$sharedUserId = $recv->fetchColumn();
|
|
$status = $sharedUserId !== false ? 'accepted' : 'pending';
|
|
|
|
$pdo->prepare(
|
|
'INSERT INTO home_sharing (idhome, iduser, shared_email, idshareduser, role_id, sharing_type, shared_sections, expiration_date, status)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)'
|
|
)->execute([
|
|
$idhome, $user['id'], $email, $sharedUserId ?: null, $roleId, $type, $sections, $expiry, $status,
|
|
]);
|
|
$idsharing = (int) $pdo->lastInsertId();
|
|
|
|
// TODO: send e-mail to the recipient (legacy: tools/mailer.php).
|
|
|
|
$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()), 201);
|