Ролевая модель и аудит Часть 2. Финал. #6

This commit is contained in:
2026-04-28 19:38:31 +03:00
parent b2cea7896d
commit 967a024d0c
8 changed files with 326 additions and 18 deletions
+40
View File
@@ -0,0 +1,40 @@
-module(core_admin_audit).
-include("records.hrl").
-export([log/7, list/0, list/1]).
log(AdminId, Email, Role, Action, EntityType, EntityId, Ip) ->
log(AdminId, Email, Role, Action, EntityType, EntityId, Ip, undefined).
log(AdminId, Email, Role, Action, EntityType, EntityId, Ip, Reason) ->
Id = base64:encode(crypto:strong_rand_bytes(9)),
Entry = #admin_audit{
id = Id,
admin_id = AdminId,
email = Email,
role = Role,
action = Action,
entity_type = EntityType,
entity_id = EntityId,
timestamp = calendar:universal_time(),
ip = Ip,
reason = Reason
},
mnesia:dirty_write(Entry),
{ok, Entry}.
list() ->
mnesia:dirty_match_object(#admin_audit{_ = '_'}).
%% Фильтрация по параметрам (простая версия)
list(Filters) ->
All = list(),
lists:filter(fun(E) ->
case proplists:get_value(admin_id, Filters) of
undefined -> true;
Id -> E#admin_audit.admin_id =:= Id
end andalso
case proplists:get_value(action, Filters) of
undefined -> true;
Act -> E#admin_audit.action =:= Act
end
% можно добавить фильтр по дате и т.д.
end, All).