Ролевая модель и аудит Часть 1.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
-module(admin_utils).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([is_admin/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;
|
||||
_ -> false
|
||||
end.
|
||||
@@ -130,14 +130,14 @@ authenticate_user_request(_Req, Email, Password) ->
|
||||
-spec authenticate_admin_request(Req :: cowboy_req:req(), Email :: binary(), Password :: binary()) ->
|
||||
{ok, Token :: binary(), User :: map()} | {error, atom()}.
|
||||
authenticate_admin_request(_Req, Email, Password) ->
|
||||
case logic_auth:authenticate_user(Email, Password) of
|
||||
{ok, User} ->
|
||||
Role = maps:get(role, User, <<"admin">>),
|
||||
case is_admin_role(Role) of
|
||||
case logic_auth:authenticate_admin(Email, Password) of
|
||||
{ok, AdminMap} ->
|
||||
Role = maps:get(role, AdminMap, <<"admin">>),
|
||||
case admin_utils:is_admin(Role) of
|
||||
true ->
|
||||
UserId = maps:get(id, User),
|
||||
Token = generate_admin_token(UserId, Role),
|
||||
{ok, Token, User};
|
||||
AdminId = maps:get(id, AdminMap),
|
||||
Token = generate_admin_token(AdminId, Role),
|
||||
{ok, Token, AdminMap};
|
||||
false -> {error, insufficient_permissions}
|
||||
end;
|
||||
Error -> Error
|
||||
@@ -152,9 +152,4 @@ generate_refresh_token(_UserId) ->
|
||||
ExpiresAt = calendar:gregorian_seconds_to_datetime(
|
||||
calendar:datetime_to_gregorian_seconds(Now) + 30 * 24 * 3600
|
||||
),
|
||||
{RefreshToken, ExpiresAt}.
|
||||
|
||||
%% ========== ВНУТРЕННИЕ ==========
|
||||
|
||||
is_admin_role(Role) ->
|
||||
lists:member(Role, [<<"admin">>, <<"superadmin">>, <<"moderator">>, <<"support">>]).
|
||||
{RefreshToken, ExpiresAt}.
|
||||
@@ -8,7 +8,7 @@
|
||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
||||
|
||||
-define(TABLES, [
|
||||
user, session, calendar, calendar_share, event, recurrence_exception,
|
||||
user, session, admin, admin_session, calendar, calendar_share, event, recurrence_exception,
|
||||
booking, review, report, banned_word, ticket, subscription, audit_log
|
||||
]).
|
||||
|
||||
@@ -62,7 +62,7 @@ create_table(Table) ->
|
||||
{atomic, ok} ->
|
||||
ok;
|
||||
{aborted, {already_exists, _}} ->
|
||||
ok;
|
||||
ok; % таблица уже существует – пропускаем
|
||||
{aborted, Reason} ->
|
||||
error({table_creation_failed, Table, Reason})
|
||||
end.
|
||||
@@ -78,6 +78,16 @@ table_opts(session) ->
|
||||
{attributes, record_info(fields, session)},
|
||||
{ram_copies, [node()]}
|
||||
];
|
||||
table_opts(admin) ->
|
||||
[
|
||||
{attributes, record_info(fields, admin)},
|
||||
{ram_copies, [node()]}
|
||||
];
|
||||
table_opts(admin_session) ->
|
||||
[
|
||||
{attributes, record_info(fields, admin_session)},
|
||||
{ram_copies, [node()]}
|
||||
];
|
||||
table_opts(calendar) ->
|
||||
[
|
||||
{attributes, record_info(fields, calendar)},
|
||||
|
||||
Reference in New Issue
Block a user