Ролевая модель и аудит Часть 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
+35 -6
View File
@@ -1,13 +1,42 @@
-module(admin_utils).
-include("records.hrl").
-export([is_admin/1]).
-export([is_admin/1, check_role/2, get_permissions/1]).
-export([client_ip/1]).
is_admin(UserId) ->
case core_admin:get_by_id(UserId) of
{ok, User} ->
Role = User#admin.role,
Role =:= admin orelse Role =:= superadmin orelse
Role =:= moderator orelse Role =:= support;
{ok, _Admin} -> true;
_ -> false
end.
end.
%% Проверка конкретной роли (или одной из списка ролей)
-spec check_role(UserId :: binary(), RequiredRole :: atom() | [atom()]) -> boolean().
check_role(UserId, RequiredRoles) when is_list(RequiredRoles) ->
case core_admin:get_by_id(UserId) of
{ok, Admin} -> lists:member(Admin#admin.role, RequiredRoles);
_ -> false
end;
check_role(UserId, RequiredRole) when is_atom(RequiredRole) ->
case core_admin:get_by_id(UserId) of
{ok, Admin} -> Admin#admin.role =:= RequiredRole;
_ -> false
end.
%% Возвращает список прав для роли администратора
-spec get_permissions(Role :: atom()) -> [binary()].
get_permissions(superadmin) ->
[<<"manage_admins">>, <<"manage_users">>, <<"manage_events">>,
<<"manage_calendars">>, <<"manage_reviews">>, <<"manage_reports">>,
<<"manage_tickets">>, <<"manage_banned_words">>, <<"view_stats">>,
<<"view_audit">>];
get_permissions(moderator) ->
[<<"manage_events">>, <<"manage_calendars">>, <<"manage_reviews">>,
<<"manage_reports">>, <<"manage_tickets">>, <<"manage_banned_words">>,
<<"view_stats">>];
get_permissions(support) ->
[<<"manage_tickets">>, <<"view_stats">>];
get_permissions(_) ->
[].
client_ip(_Req) -> <<"127.0.0.1">>.
+3 -3
View File
@@ -9,7 +9,7 @@
-define(TABLES, [
user, session, admin, admin_session, calendar, calendar_share, event, recurrence_exception,
booking, review, report, banned_word, ticket, subscription, audit_log
booking, review, report, banned_word, ticket, subscription, admin_audit
]).
start_link() ->
@@ -138,8 +138,8 @@ table_opts(subscription) ->
{attributes, record_info(fields, subscription)},
{ram_copies, [node()]}
];
table_opts(audit_log) ->
table_opts(admin_audit) ->
[
{attributes, record_info(fields, audit_log)},
{attributes, record_info(fields, admin_audit)},
{ram_copies, [node()]}
].