Улучшение безопасности и обработки ошибок. Этап 1. #8

This commit is contained in:
2026-04-29 11:00:14 +03:00
parent 3da4ee28d4
commit cfd2e38952
10 changed files with 219 additions and 116 deletions
@@ -12,7 +12,7 @@ init(Req, _Opts) ->
end.
get_report(Req) ->
case handler_auth:authenticate(Req) of
case auth_admin(Req) of
{ok, AdminId, Req1} ->
case admin_utils:is_admin(AdminId) of
true ->
@@ -31,17 +31,18 @@ get_report(Req) ->
end.
update_report(Req) ->
case handler_auth:authenticate(Req) of
case auth_admin(Req) of
{ok, AdminId, Req1} ->
case admin_utils:is_admin(AdminId) of
true ->
ReportId = cowboy_req:binding(id, Req1),
{ok, Body, Req2} = cowboy_req:read_body(Req1),
try jsx:decode(Body, [return_maps]) of
#{<<"status">> := NewStatus} ->
#{<<"status">> := NewStatus, <<"reason">> := Reason} ->
StatusAtom = binary_to_atom(NewStatus, utf8),
case core_report:update_status(ReportId, StatusAtom, AdminId) of
{ok, Report} ->
log_audit(AdminId, <<"update_report_status">>, <<"report">>, ReportId, Reason),
send_json(Req2, 200, report_to_json(Report));
{error, not_found} ->
send_error(Req2, 404, <<"Report not found">>);
@@ -49,7 +50,7 @@ update_report(Req) ->
send_error(Req2, 500, <<"Internal server error">>)
end;
_ ->
send_error(Req2, 400, <<"Missing status field">>)
send_error(Req2, 400, <<"Missing status or reason">>)
catch
_:_ -> send_error(Req2, 400, <<"Invalid JSON">>)
end;
@@ -60,6 +61,26 @@ update_report(Req) ->
send_error(Req1, Code, Message)
end.
auth_admin(Req) ->
case handler_auth:authenticate(Req) of
{ok, AdminId, Req1} ->
case admin_utils:is_admin(AdminId) of
true -> {ok, AdminId, Req1};
false -> {error, 403, <<"Admin access required">>, Req1}
end;
{error, Code, Message, Req1} ->
{error, Code, Message, Req1}
end.
log_audit(AdminId, Action, EntityType, EntityId, Reason) ->
case core_admin:get_by_id(AdminId) of
{ok, Admin} ->
core_admin_audit:log(AdminId, Admin#admin.email, Admin#admin.role,
Action, EntityType, EntityId,
<<"127.0.0.1">>, Reason);
_ -> ok
end.
report_to_json(R) ->
#{
id => R#report.id,