Перенести все админские эндпоинты на порт 8445 и добавить отдельную авторизацию для админов. Часть 1
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
-module(admin_handler_user_by_id).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([init/2]).
|
||||
-export([user_to_json/1, convert_updates/1]).
|
||||
|
||||
@@ -9,10 +8,10 @@ init(Req, Opts) ->
|
||||
|
||||
handle(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> -> get_user(Req);
|
||||
<<"PUT">> -> update_user(Req);
|
||||
<<"GET">> -> get_user(Req);
|
||||
<<"PUT">> -> update_user(Req);
|
||||
<<"DELETE">> -> delete_user(Req);
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
get_user(Req) ->
|
||||
@@ -22,11 +21,10 @@ get_user(Req) ->
|
||||
true ->
|
||||
UserId = cowboy_req:binding(id, Req1),
|
||||
case core_user:get_by_id(UserId) of
|
||||
{ok, User} when User#user.status =:= deleted ->
|
||||
send_error(Req1, 404, <<"User not found">>);
|
||||
{ok, User} ->
|
||||
case User#user.status of
|
||||
deleted -> send_error(Req1, 404, <<"User not found">>);
|
||||
_ -> send_json(Req1, 200, user_to_json(User))
|
||||
end;
|
||||
send_json(Req1, 200, user_to_json(User));
|
||||
{error, not_found} ->
|
||||
send_error(Req1, 404, <<"User not found">>)
|
||||
end;
|
||||
@@ -47,9 +45,8 @@ update_user(Req) ->
|
||||
try jsx:decode(Body, [return_maps]) of
|
||||
Decoded when is_map(Decoded) ->
|
||||
Updates = maps:to_list(Decoded),
|
||||
% Преобразуем бинарные значения в атомы где нужно
|
||||
ConvertedUpdates = convert_updates(Updates),
|
||||
case core_user:update(UserId, ConvertedUpdates) of
|
||||
Converted = convert_updates(Updates),
|
||||
case core_user:update(UserId, Converted) of
|
||||
{ok, User} ->
|
||||
send_json(Req2, 200, user_to_json(User));
|
||||
{error, not_found} ->
|
||||
@@ -69,16 +66,6 @@ update_user(Req) ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
convert_updates(Updates) ->
|
||||
lists:map(fun
|
||||
({<<"status">>, <<"active">>}) -> {status, active};
|
||||
({<<"status">>, <<"frozen">>}) -> {status, frozen};
|
||||
({<<"status">>, <<"deleted">>}) -> {status, deleted};
|
||||
({<<"role">>, <<"user">>}) -> {role, user};
|
||||
({<<"role">>, <<"admin">>}) -> {role, admin};
|
||||
(Other) -> Other
|
||||
end, Updates).
|
||||
|
||||
delete_user(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, AdminId, Req1} ->
|
||||
@@ -106,10 +93,10 @@ is_admin(UserId) ->
|
||||
|
||||
user_to_json(User) ->
|
||||
#{
|
||||
id => User#user.id,
|
||||
email => User#user.email,
|
||||
role => User#user.role,
|
||||
status => User#user.status,
|
||||
id => User#user.id,
|
||||
email => User#user.email,
|
||||
role => User#user.role,
|
||||
status => User#user.status,
|
||||
created_at => datetime_to_iso8601(User#user.created_at),
|
||||
updated_at => datetime_to_iso8601(User#user.updated_at)
|
||||
}.
|
||||
@@ -118,6 +105,13 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
iolist_to_binary(io_lib:format("~4..0B-~2..0B-~2..0BT~2..0B:~2..0B:~2..0BZ",
|
||||
[Year, Month, Day, Hour, Minute, Second])).
|
||||
|
||||
convert_updates(Updates) ->
|
||||
lists:map(fun
|
||||
({<<"status">>, Value}) -> {status, binary_to_existing_atom(Value)};
|
||||
({<<"role">>, Value}) -> {role, binary_to_existing_atom(Value)};
|
||||
(Other) -> Other
|
||||
end, Updates).
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
|
||||
Reference in New Issue
Block a user