Ролевая модель и аудит Часть 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
+37
View File
@@ -0,0 +1,37 @@
-module(admin_handler_me).
-behaviour(cowboy_handler).
-include("records.hrl").
-export([init/2]).
init(Req, _Opts) ->
case cowboy_req:method(Req) of
<<"GET">> ->
case handler_auth:authenticate(Req) of
{ok, AdminId, Req1} ->
case core_admin:get_by_id(AdminId) of
{ok, Admin} ->
Permissions = admin_utils:get_permissions(Admin#admin.role),
Resp = jsx:encode(#{
id => Admin#admin.id,
email => Admin#admin.email,
role => Admin#admin.role,
permissions => Permissions
}),
Req2 = cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>}, Resp, Req1),
{ok, Req2, []};
{error, not_found} ->
send_error(Req1, 404, <<"Admin not found">>)
end;
{error, Code, Message, Req1} ->
send_error(Req1, Code, Message)
end;
_ ->
send_error(Req, 405, <<"Method not allowed">>)
end.
send_error(Req, Code, Message) ->
Body = jsx:encode(#{error => Message}),
Req2 = cowboy_req:reply(Code, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Req2, []}.