Stage 9
This commit is contained in:
49
src/admin_app.erl
Normal file
49
src/admin_app.erl
Normal file
@@ -0,0 +1,49 @@
|
||||
-module(admin_app).
|
||||
-behaviour(application).
|
||||
|
||||
-export([start/2, stop/1]).
|
||||
|
||||
start(_StartType, _StartArgs) ->
|
||||
application:ensure_all_started(cowboy),
|
||||
start_admin_http(),
|
||||
{ok, self()}.
|
||||
|
||||
stop(_State) ->
|
||||
ok.
|
||||
|
||||
start_admin_http() ->
|
||||
Port = application:get_env(eventhub, admin_http_port, 8445),
|
||||
|
||||
Dispatch = cowboy_router:compile([
|
||||
{'_', [
|
||||
{"/admin/health", admin_handler_health, []},
|
||||
{"/admin/stats", admin_handler_stats, []},
|
||||
{"/admin/users", admin_handler_users, []},
|
||||
{"/admin/users/:id", admin_handler_user_by_id, []},
|
||||
{"/admin/calendars", admin_handler_calendars, []},
|
||||
{"/admin/calendars/:id", admin_handler_calendar_by_id, []},
|
||||
{"/admin/events", admin_handler_events, []},
|
||||
{"/admin/events/:id", admin_handler_event_by_id, []},
|
||||
{"/admin/reports", handler_reports, []},
|
||||
{"/admin/reports/:id", handler_report_by_id, []},
|
||||
{"/admin/tickets", handler_tickets, []},
|
||||
{"/admin/tickets/:id", handler_ticket_by_id, []},
|
||||
{"/admin/tickets/stats", handler_ticket_stats, []},
|
||||
{"/admin/subscriptions", handler_admin_subscriptions, []},
|
||||
{"/admin/banned-words", handler_banned_words, []}
|
||||
]}
|
||||
]),
|
||||
|
||||
Middlewares = [
|
||||
cowboy_router,
|
||||
cowboy_handler
|
||||
],
|
||||
|
||||
Env = #{dispatch => Dispatch},
|
||||
|
||||
cowboy:start_clear(admin_http, [{port, Port}], #{
|
||||
env => Env,
|
||||
middlewares => Middlewares
|
||||
}),
|
||||
|
||||
io:format("Admin HTTP server started on port ~p~n", [Port]).
|
||||
@@ -4,7 +4,7 @@
|
||||
{ws_port, 8081},
|
||||
{admin_http_port, 8445},
|
||||
{admin_ws_port, 8446},
|
||||
{jwt_secret, <<"change_me_in_production">>},
|
||||
{jwt_secret, <<"my-super-secret-key-for-jwt-32-bytes!">>},
|
||||
{argon2_params, #{t_cost => 2, m_cost => 19, parallelism => 1}}
|
||||
]},
|
||||
{mnesia, [
|
||||
|
||||
@@ -12,6 +12,7 @@ start(_StartType, _StartArgs) ->
|
||||
ok = infra_mnesia:init_tables(),
|
||||
ok = infra_mnesia:wait_for_tables(),
|
||||
start_http(),
|
||||
start_admin_http(),
|
||||
{ok, Pid};
|
||||
Error ->
|
||||
Error
|
||||
@@ -76,4 +77,30 @@ start_http() ->
|
||||
middlewares => Middlewares
|
||||
}),
|
||||
|
||||
io:format("HTTP server started on port ~p~n", [Port]).
|
||||
io:format("HTTP server started on port ~p~n", [Port]).
|
||||
|
||||
start_admin_http() ->
|
||||
Port = application:get_env(eventhub, admin_http_port, 8445),
|
||||
|
||||
Dispatch = cowboy_router:compile([
|
||||
{'_', [
|
||||
{"/admin/health", admin_handler_health, []},
|
||||
{"/admin/stats", admin_handler_stats, []},
|
||||
{"/admin/users", admin_handler_users, []},
|
||||
{"/admin/users/:id", admin_handler_user_by_id, []}
|
||||
]}
|
||||
]),
|
||||
|
||||
Middlewares = [
|
||||
cowboy_router,
|
||||
cowboy_handler
|
||||
],
|
||||
|
||||
Env = #{dispatch => Dispatch},
|
||||
|
||||
cowboy:start_clear(admin_http, [{port, Port}], #{
|
||||
env => Env,
|
||||
middlewares => Middlewares
|
||||
}),
|
||||
|
||||
io:format("Admin HTTP server started on port ~p~n", [Port]).
|
||||
7
src/handlers/admin_handler_health.erl
Normal file
7
src/handlers/admin_handler_health.erl
Normal file
@@ -0,0 +1,7 @@
|
||||
-module(admin_handler_health).
|
||||
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, _Opts) ->
|
||||
Body = jsx:encode(#{status => <<"ok">>, service => <<"admin">>}),
|
||||
cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
78
src/handlers/admin_handler_stats.erl
Normal file
78
src/handlers/admin_handler_stats.erl
Normal file
@@ -0,0 +1,78 @@
|
||||
-module(admin_handler_stats).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([init/2]).
|
||||
-export([count_users/0, count_calendars/0, count_events/0, count_bookings/0,
|
||||
count_reviews/0, count_reports/0, count_tickets/0, count_subscriptions/0]).
|
||||
-export([is_admin/1]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
handle(Req, Opts).
|
||||
|
||||
handle(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> -> get_stats(Req);
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
get_stats(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, AdminId, Req1} ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
Stats = #{
|
||||
users => count_users(),
|
||||
calendars => count_calendars(),
|
||||
events => count_events(),
|
||||
bookings => count_bookings(),
|
||||
reviews => count_reviews(),
|
||||
reports => count_reports(),
|
||||
tickets => count_tickets(),
|
||||
subscriptions => count_subscriptions()
|
||||
},
|
||||
send_json(Req1, 200, Stats);
|
||||
false ->
|
||||
send_error(Req1, 403, <<"Admin access required">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% Вспомогательные функции
|
||||
is_admin(UserId) ->
|
||||
case core_user:get_by_id(UserId) of
|
||||
{ok, User} -> User#user.role =:= admin;
|
||||
_ -> false
|
||||
end.
|
||||
|
||||
count_users() ->
|
||||
length(mnesia:dirty_match_object(#user{_ = '_'})).
|
||||
|
||||
count_calendars() ->
|
||||
length(mnesia:dirty_match_object(#calendar{_ = '_'})).
|
||||
|
||||
count_events() ->
|
||||
length(mnesia:dirty_match_object(#event{is_instance = false, _ = '_'})).
|
||||
|
||||
count_bookings() ->
|
||||
length(mnesia:dirty_match_object(#booking{_ = '_'})).
|
||||
|
||||
count_reviews() ->
|
||||
length(mnesia:dirty_match_object(#review{_ = '_'})).
|
||||
|
||||
count_reports() ->
|
||||
length(mnesia:dirty_match_object(#report{_ = '_'})).
|
||||
|
||||
count_tickets() ->
|
||||
length(mnesia:dirty_match_object(#ticket{_ = '_'})).
|
||||
|
||||
count_subscriptions() ->
|
||||
length(mnesia:dirty_match_object(#subscription{_ = '_'})).
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
127
src/handlers/admin_handler_user_by_id.erl
Normal file
127
src/handlers/admin_handler_user_by_id.erl
Normal file
@@ -0,0 +1,127 @@
|
||||
-module(admin_handler_user_by_id).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([init/2]).
|
||||
-export([user_to_json/1, convert_updates/1]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
handle(Req, Opts).
|
||||
|
||||
handle(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> -> get_user(Req);
|
||||
<<"PUT">> -> update_user(Req);
|
||||
<<"DELETE">> -> delete_user(Req);
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
get_user(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, AdminId, Req1} ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
UserId = cowboy_req:binding(id, Req1),
|
||||
case core_user:get_by_id(UserId) of
|
||||
{ok, User} ->
|
||||
case User#user.status of
|
||||
deleted -> send_error(Req1, 404, <<"User not found">>);
|
||||
_ -> send_json(Req1, 200, user_to_json(User))
|
||||
end;
|
||||
{error, not_found} ->
|
||||
send_error(Req1, 404, <<"User not found">>)
|
||||
end;
|
||||
false ->
|
||||
send_error(Req1, 403, <<"Admin access required">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
update_user(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, AdminId, Req1} ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
UserId = cowboy_req:binding(id, Req1),
|
||||
{ok, Body, Req2} = cowboy_req:read_body(Req1),
|
||||
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
|
||||
{ok, User} ->
|
||||
send_json(Req2, 200, user_to_json(User));
|
||||
{error, not_found} ->
|
||||
send_error(Req2, 404, <<"User not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req2, 500, <<"Internal server error">>)
|
||||
end;
|
||||
_ ->
|
||||
send_error(Req2, 400, <<"Invalid JSON">>)
|
||||
catch
|
||||
_:_ -> send_error(Req2, 400, <<"Invalid JSON format">>)
|
||||
end;
|
||||
false ->
|
||||
send_error(Req1, 403, <<"Admin access required">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
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} ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
UserId = cowboy_req:binding(id, Req1),
|
||||
case core_user:delete(UserId) of
|
||||
{ok, _} ->
|
||||
send_json(Req1, 200, #{status => <<"deleted">>});
|
||||
{error, not_found} ->
|
||||
send_error(Req1, 404, <<"User not found">>)
|
||||
end;
|
||||
false ->
|
||||
send_error(Req1, 403, <<"Admin access required">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
is_admin(UserId) ->
|
||||
case core_user:get_by_id(UserId) of
|
||||
{ok, User} -> User#user.role =:= admin;
|
||||
_ -> false
|
||||
end.
|
||||
|
||||
user_to_json(User) ->
|
||||
#{
|
||||
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)
|
||||
}.
|
||||
|
||||
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])).
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
57
src/handlers/admin_handler_users.erl
Normal file
57
src/handlers/admin_handler_users.erl
Normal file
@@ -0,0 +1,57 @@
|
||||
-module(admin_handler_users).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
handle(Req, Opts).
|
||||
|
||||
handle(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> -> list_users(Req);
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
list_users(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, AdminId, Req1} ->
|
||||
case is_admin(AdminId) of
|
||||
true ->
|
||||
Users = mnesia:dirty_match_object(#user{_ = '_'}),
|
||||
ActiveUsers = [U || U <- Users, U#user.status =/= deleted],
|
||||
Response = [user_to_json(U) || U <- ActiveUsers],
|
||||
send_json(Req1, 200, Response);
|
||||
false ->
|
||||
send_error(Req1, 403, <<"Admin access required">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
is_admin(UserId) ->
|
||||
case core_user:get_by_id(UserId) of
|
||||
{ok, User} -> User#user.role =:= admin;
|
||||
_ -> false
|
||||
end.
|
||||
|
||||
user_to_json(User) ->
|
||||
#{
|
||||
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)
|
||||
}.
|
||||
|
||||
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])).
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
Reference in New Issue
Block a user