Files
Password-Manager/delphi-backend/Handlers/PM.Handler.Folders.pas
T
r-zakarya bd14831449 feat: Bitwarden import bundle + settings search + quick-search hotkey + UX fixes
- Bitwarden CSV import: folders auto-created server-side; notes column on
  login rows surfaces as a "Notes" custom field instead of polluting tags;
  type=card / type=identity rows now mapped to kind=note with the
  credit-card / identity template + card_* / identity_* columns
  pulled into custom_fields; `fields` column parsed (Bitwarden's
  "label: value\nlabel: value" lines + our own JSON shape).
- Settings panel search: live filter at top of the panel, matches each
  .setting-row individually, hides whole section when no row matches,
  shows a "No matches" banner. Esc clears query (without closing
  Settings); Esc with empty query closes the panel.
- Quick-search hotkey customizable: SetQuickSearchHotkey added to
  PM.Bridge; cmd://autofill/hotkeys extended with qs_mods/qs_vk
  (independent of the autofill enabled flag — quick-search stays
  armed even when autofill is off); state.quickSearchHotkey synced
  via settings_json; new "Quick search picker" row in Settings.
- FireDAC SQLite folder POST/PUT: pre-declare ftString on color/icon
  params so .Clear (NULL) doesn't trip "[FireDAC][Phys][SQLite]-335
  type unknown" at Prepare — was crashing the CSV-import folder
  auto-creation path.
- Edge form-data autocomplete suppressed on slideover inputs (title,
  site, username, password, TOTP, note body, custom fields):
  autocomplete=off (new-password on secrets) + spellcheck=false. Fixes
  the "Informations enregistrées" dropdown popping over data after a
  field was edited.
- closeSlideOver blurs any focused descendant before removing .is-open
  so an invisible focused field can't react to arrow-down / backspace
  after dismissal.
- Slideover Esc handler upgraded to capture phase so it fires before
  the input's own keydown or browser-level Esc swallow on the active
  autocomplete popup.
- Settings panel Esc closes the panel when search input is empty;
  search keeps the keystroke when it has a query to clear.
- Discard-fantome on note open: customFields working copy and
  originalCustomJson now share the SAME normalized array — comparing
  raw plainCustom against the .map()'d working copy made notes look
  dirty on open.
- Delete / Backspace global shortcut: batch-trash on normal views,
  batch perm-delete on trash view, gated on selection + no input
  focused + no modal up.
- Toggle thumb vertical centering via top:50% + translateY(-50%);
  state checked uses translate(16px, -50%) to keep the centring.
- Batch bar disappears after per-card restore/perm-delete/trash:
  state.checked.delete(id) before render for the relevant flows;
  state.checked.clear() before render in emptyTrash and the new
  moveEntriesToFolder helper.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-30 23:25:50 +01:00

377 lines
10 KiB
ObjectPascal

unit PM.Handler.Folders;
(*
GET /folders -> [{name, color, icon}, ...]
POST /folders body {name, color?, icon?} -> {message, name}
PUT /folders/{name} body {color?, icon?} -> {message}
DELETE /folders/{name} -> {message}
*)
interface
implementation
uses
System.SysUtils, System.JSON, System.NetEncoding,
System.Generics.Collections,
Data.DB,
FireDAC.Comp.Client, FireDAC.Stan.Param,
IdCustomHTTPServer,
PM.Router, PM.JSON, PM.Database, PM.Session, PM.Audit, PM.RateLimit;
// ===== GET /folders ==========================================================
procedure HandleGetFolders(ARequest: TIdHTTPRequestInfo;
AResponse: TIdHTTPResponseInfo; const AParams: TArray<string>);
var
LUserId: Integer;
LQ: TFDQuery;
LArr: TJSONArray;
LObj: TJSONObject;
begin
try
LUserId := Authenticate(ARequest, AResponse);
except
on ESessionRejected do Exit;
end;
LArr := TJSONArray.Create;
DB.Lock;
try
LQ := TFDQuery.Create(nil);
try
LQ.Connection := DB.Connection;
LQ.SQL.Text :=
'SELECT name, color, icon FROM folders ' +
'WHERE user_id = :uid ORDER BY sort_order, name';
LQ.ParamByName('uid').AsInteger := LUserId;
LQ.Open;
while not LQ.Eof do
begin
LObj := TJSONObject.Create;
LObj.AddPair('name', LQ.FieldByName('name').AsString);
LObj.AddPair('color', LQ.FieldByName('color').AsString);
LObj.AddPair('icon', LQ.FieldByName('icon').AsString);
LArr.Add(LObj);
LQ.Next;
end;
finally
LQ.Free;
end;
finally
DB.Unlock;
end;
TJSONHelper.SendJSON(AResponse, LArr);
end;
// ===== POST /folders =========================================================
procedure HandleCreateFolder(ARequest: TIdHTTPRequestInfo;
AResponse: TIdHTTPResponseInfo; const AParams: TArray<string>);
var
LUserId: Integer;
LBody: TJSONObject;
LName, LColor, LIcon: string;
LQ: TFDQuery;
LObj: TJSONObject;
begin
try
LUserId := Authenticate(ARequest, AResponse);
RequireCSRF(ARequest, AResponse, LUserId);
except
on ESessionRejected do Exit;
end;
LBody := TJSONHelper.ReadBody(ARequest);
try
LName := Trim(LBody.GetValue<string>('name', ''));
LColor := Trim(LBody.GetValue<string>('color', ''));
LIcon := Trim(LBody.GetValue<string>('icon', ''));
finally
LBody.Free;
end;
if LName = '' then
begin
TJSONHelper.SendError(AResponse, 400, 'Folder name required');
Exit;
end;
if SameText(LName, 'All') then
begin
TJSONHelper.SendError(AResponse, 400, 'Cannot use All');
Exit;
end;
DB.Lock;
try
LQ := TFDQuery.Create(nil);
try
LQ.Connection := DB.Connection;
LQ.SQL.Text :=
'INSERT INTO folders (user_id, name, color, icon) ' +
'VALUES (:uid, :name, :color, :icon)';
LQ.ParamByName('uid').AsInteger := LUserId;
LQ.ParamByName('name').AsString := LName;
// Pre-declare type so .Clear (NULL) doesn't leave the param
// untyped — FireDAC SQLite rejects untyped params at Prepare.
LQ.ParamByName('color').DataType := ftString;
LQ.ParamByName('icon').DataType := ftString;
if LColor = '' then LQ.ParamByName('color').Clear
else LQ.ParamByName('color').AsString := LColor;
if LIcon = '' then LQ.ParamByName('icon').Clear
else LQ.ParamByName('icon').AsString := LIcon;
try
LQ.ExecSQL;
except
on E: Exception do
begin
TJSONHelper.SendError(AResponse, 409, 'Folder exists');
Exit;
end;
end;
finally
LQ.Free;
end;
finally
DB.Unlock;
end;
LogAudit(LUserId, 'add_folder', GetClientIP(ARequest));
LObj := TJSONObject.Create;
LObj.AddPair('message', 'Created');
LObj.AddPair('name', LName);
TJSONHelper.SendJSON(AResponse, LObj);
end;
// ===== PUT /folders/{name} ===================================================
// Body: {color?, icon?} — pass empty string to clear.
procedure HandleUpdateFolder(ARequest: TIdHTTPRequestInfo;
AResponse: TIdHTTPResponseInfo; const AParams: TArray<string>);
var
LUserId: Integer;
LBody: TJSONObject;
LName, LColor, LIcon: string;
LHasColor, LHasIcon: Boolean;
LQ: TFDQuery;
begin
try
LUserId := Authenticate(ARequest, AResponse);
RequireCSRF(ARequest, AResponse, LUserId);
except
on ESessionRejected do Exit;
end;
if Length(AParams) < 1 then
begin
TJSONHelper.SendError(AResponse, 400, 'Folder name required');
Exit;
end;
LName := TNetEncoding.URL.Decode(AParams[0]);
if SameText(LName, 'All') then
begin
TJSONHelper.SendError(AResponse, 400, 'Cannot customise All');
Exit;
end;
LBody := TJSONHelper.ReadBody(ARequest);
try
LHasColor := LBody.GetValue('color') <> nil;
LHasIcon := LBody.GetValue('icon') <> nil;
LColor := LBody.GetValue<string>('color', '');
LIcon := LBody.GetValue<string>('icon', '');
finally
LBody.Free;
end;
if not (LHasColor or LHasIcon) then
begin
TJSONHelper.SendOK(AResponse, 'No change');
Exit;
end;
DB.Lock;
try
LQ := TFDQuery.Create(nil);
try
LQ.Connection := DB.Connection;
// Build SET clause dynamically based on which fields the caller sent.
var LSet := '';
if LHasColor then LSet := 'color = :color';
if LHasIcon then
begin
if LSet <> '' then LSet := LSet + ', ';
LSet := LSet + 'icon = :icon';
end;
LQ.SQL.Text :=
'UPDATE folders SET ' + LSet +
' WHERE user_id = :uid AND name = :name';
LQ.ParamByName('uid').AsInteger := LUserId;
LQ.ParamByName('name').AsString := LName;
if LHasColor then
begin
LQ.ParamByName('color').DataType := ftString;
if LColor = '' then LQ.ParamByName('color').Clear
else LQ.ParamByName('color').AsString := LColor;
end;
if LHasIcon then
begin
LQ.ParamByName('icon').DataType := ftString;
if LIcon = '' then LQ.ParamByName('icon').Clear
else LQ.ParamByName('icon').AsString := LIcon;
end;
LQ.ExecSQL;
if LQ.RowsAffected = 0 then
begin
TJSONHelper.SendError(AResponse, 404, 'Not found');
Exit;
end;
finally
LQ.Free;
end;
finally
DB.Unlock;
end;
LogAudit(LUserId, 'update_folder', GetClientIP(ARequest));
TJSONHelper.SendOK(AResponse, 'Updated');
end;
// ===== POST /folders/reorder =================================================
// Body: {names: ["Work", "Personal", "Misc"]} — write sort_order = index+1
// for each. Names not in the list keep their previous sort_order (so a
// partial reorder still works after another tab created a folder).
procedure HandleReorderFolders(ARequest: TIdHTTPRequestInfo;
AResponse: TIdHTTPResponseInfo; const AParams: TArray<string>);
var
LUserId: Integer;
LBody: TJSONObject;
LArr: TJSONArray;
LQ: TFDQuery;
I: Integer;
begin
try
LUserId := Authenticate(ARequest, AResponse);
RequireCSRF(ARequest, AResponse, LUserId);
except
on ESessionRejected do Exit;
end;
LBody := TJSONHelper.ReadBody(ARequest);
try
LArr := LBody.GetValue<TJSONArray>('names');
if (LArr = nil) or (LArr.Count = 0) then
begin
TJSONHelper.SendError(AResponse, 400, 'names array required');
Exit;
end;
DB.Lock;
try
LQ := TFDQuery.Create(nil);
try
LQ.Connection := DB.Connection;
LQ.SQL.Text :=
'UPDATE folders SET sort_order = :ord ' +
'WHERE user_id = :uid AND name = :name';
for I := 0 to LArr.Count - 1 do
begin
LQ.ParamByName('uid').AsInteger := LUserId;
LQ.ParamByName('ord').AsInteger := I + 1;
LQ.ParamByName('name').AsString := LArr.Items[I].Value;
LQ.ExecSQL;
end;
finally
LQ.Free;
end;
finally
DB.Unlock;
end;
finally
LBody.Free;
end;
LogAudit(LUserId, 'reorder_folders', GetClientIP(ARequest));
TJSONHelper.SendOK(AResponse, 'Reordered');
end;
// ===== DELETE /folders/{name} ================================================
procedure HandleDeleteFolder(ARequest: TIdHTTPRequestInfo;
AResponse: TIdHTTPResponseInfo; const AParams: TArray<string>);
var
LUserId: Integer;
LName: string;
LQ: TFDQuery;
LChanges: Integer;
begin
try
LUserId := Authenticate(ARequest, AResponse);
RequireCSRF(ARequest, AResponse, LUserId);
except
on ESessionRejected do Exit;
end;
if Length(AParams) < 1 then
begin
TJSONHelper.SendError(AResponse, 400, 'Folder name required');
Exit;
end;
LName := TNetEncoding.URL.Decode(AParams[0]);
if SameText(LName, 'All') then
begin
TJSONHelper.SendError(AResponse, 400, 'Cannot delete All');
Exit;
end;
DB.Lock;
try
LQ := TFDQuery.Create(nil);
try
LQ.Connection := DB.Connection;
LQ.SQL.Text := 'DELETE FROM folders WHERE user_id = :uid AND name = :name';
LQ.ParamByName('uid').AsInteger := LUserId;
LQ.ParamByName('name').AsString := LName;
LQ.ExecSQL;
LChanges := LQ.RowsAffected;
finally
LQ.Free;
end;
if LChanges = 0 then
begin
TJSONHelper.SendError(AResponse, 404, 'Not found');
Exit;
end;
// Reassign entries from the deleted folder to 'All'
LQ := TFDQuery.Create(nil);
try
LQ.Connection := DB.Connection;
LQ.SQL.Text :=
'UPDATE vault_entries SET folder = ''All'' ' +
'WHERE user_id = :uid AND folder = :name';
LQ.ParamByName('uid').AsInteger := LUserId;
LQ.ParamByName('name').AsString := LName;
LQ.ExecSQL;
finally
LQ.Free;
end;
finally
DB.Unlock;
end;
LogAudit(LUserId, 'delete_folder', GetClientIP(ARequest));
TJSONHelper.SendOK(AResponse, 'Deleted');
end;
initialization
Router.Register('GET', '/folders', HandleGetFolders);
Router.Register('POST', '/folders', HandleCreateFolder);
Router.Register('POST', '/folders/reorder', HandleReorderFolders);
Router.Register('PUT', '/folders/(.+)', HandleUpdateFolder);
Router.Register('DELETE', '/folders/(.+)', HandleDeleteFolder);
end.