Skip to content

Commit

Permalink
Merge pull request #4228 from badlop/webadmin-commands
Browse files Browse the repository at this point in the history
Use commands in WebAdmin to generate page content
  • Loading branch information
badlop authored Jun 19, 2024
2 parents be847a7 + 899b77c commit 0380695
Show file tree
Hide file tree
Showing 29 changed files with 3,962 additions and 2,011 deletions.
15 changes: 14 additions & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ _build/edoc/docs.md: edoc_compile
_build/edoc/logo.png: edoc_compile
wget https://docs.ejabberd.im/assets/img/footer_logo_e.png -O _build/edoc/logo.png

#.
#' format / indent
#

format:
tools/rebar3-format.sh $(REBAR)

indent:
tools/emacs-indent.sh

#.
#' copy-files
#
Expand Down Expand Up @@ -650,7 +660,7 @@ test:
.PHONY: src edoc dialyzer Makefile TAGS clean clean-rel distclean prod rel \
install uninstall uninstall-binary uninstall-all translations deps test \
all dev doap help install-rel relive scripts uninstall-rel update \
erlang_plt deps_plt ejabberd_plt xref hooks options
erlang_plt deps_plt ejabberd_plt xref hooks options format indent

#.
#' help
Expand Down Expand Up @@ -682,6 +692,9 @@ help:
@echo " translations Extract translation files"
@echo " TAGS Generate tags file for text editors"
@echo ""
@echo " format Format source code using rebar3_format [rebar3]"
@echo " indent Indent source code using erlang-mode [emacs]"
@echo ""
@echo " dialyzer Run Dialyzer static analyzer"
@echo " hooks Run hooks validator"
@echo " test Run Common Tests suite [rebar3]"
Expand Down
21 changes: 19 additions & 2 deletions include/ejabberd_commands.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,30 @@
%%%----------------------------------------------------------------------

-type aterm() :: {atom(), atype()}.
-type atype() :: integer | string | binary |
-type atype() :: integer | string | binary | any | atom |
{tuple, [aterm()]} | {list, aterm()}.
-type rterm() :: {atom(), rtype()}.
-type rtype() :: integer | string | atom |
-type rtype() :: integer | string | atom | any |
{tuple, [rterm()]} | {list, rterm()} |
rescode | restuple.

%% The 'any' and 'atom' argument types and 'any' result type
%% should only be used %% by commands with tag 'internal',
%% which are meant to be used only internally in ejabberd,
%% and not called using external frontends.

%% The purpose of a command can either be:
%% - informative: its purpose is to obtain information
%% - modifier: its purpose is to produce some change in the server
%%
%% A modifier command should be designed just to produce its desired side-effect,
%% and its result term should just be success or failure: rescode or restuple.
%%
%% ejabberd_web_admin:make_command/2 considers that commands
%% with result type different than rescode or restuple
%% are commands that can be safely executed automatically
%% to get information and build the web page.

-type oauth_scope() :: atom().

%% ejabberd_commands OAuth ReST ACL definition:
Expand Down
22 changes: 19 additions & 3 deletions include/ejabberd_web_admin.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
[{<<"type">>, Type}, {<<"name">>, Name},
{<<"value">>, Value}])).

-define(INPUTPH(Type, Name, Value, PlaceHolder),
?XA(<<"input">>,
[{<<"type">>, Type}, {<<"name">>, Name},
{<<"value">>, Value}, {<<"placeholder">>, PlaceHolder}])).

-define(INPUTT(Type, Name, Value),
?INPUT(Type, Name, (translate:translate(Lang, Value)))).

Expand Down Expand Up @@ -95,16 +100,27 @@
-define(XRES(Text),
?XAC(<<"p">>, [{<<"class">>, <<"result">>}], Text)).

-define(DIVRES(Elements),
?XAE(<<"div">>, [{<<"class">>, <<"result">>}], Elements)).

%% Guide Link
-define(XREST(Text), ?XRES((translate:translate(Lang, Text)))).

-define(GL(Ref, Title),
?XAE(<<"div">>, [{<<"class">>, <<"guidelink">>}],
[?XAE(<<"a">>,
[{<<"href">>, <<"https://docs.ejabberd.im/admin/configuration/", Ref/binary>>},
[{<<"href">>, <<"https://docs.ejabberd.im/", Ref/binary>>},
{<<"target">>, <<"_blank">>}],
[?C(<<"docs: ", Title/binary>>)])])).

%% h1 with a Guide Link
-define(H1GL(Name, Ref, Title),
[?XC(<<"h1">>, Name), ?GL(Ref, Title)]).
-define(H1GLraw(Name, Ref, Title),
[?XC(<<"h1">>, Name), ?GL(Ref, Title), ?BR]).
-define(H1GL(Name, RefConf, Title),
?H1GLraw(Name, <<"admin/configuration/", RefConf/binary>>, Title)).

-define(ANCHORL(Ref),
?XAE(<<"div">>, [{<<"class">>, <<"anchorlink">>}],
[?XAE(<<"a">>,
[{<<"href">>, <<"#", Ref/binary>>}],
[?C(<<"<=">>)])])).
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ defmodule Ejabberd.MixProject do
if_version_below(~c"24", [{:d, :SYSTOOLS_APP_DEF_WITHOUT_OPTIONAL}]) ++
if_version_below(~c"24", [{:d, :OTP_BELOW_24}]) ++
if_version_below(~c"25", [{:d, :OTP_BELOW_25}]) ++
if_version_below(~c"26", [{:d, :OTP_BELOW_26}]) ++
if_version_below(~c"27", [{:d, :OTP_BELOW_27}]) ++
if_type_exported(:odbc, {:opaque, :connection_reference, 0}, [{:d, :ODBC_HAS_TYPES}])
defines = for {:d, value} <- result, do: {:d, value}
Expand Down
42 changes: 30 additions & 12 deletions priv/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,7 @@ ul li #navhead a, ul li #navheadsub a, ul li #navheadsubsub a {
margin-bottom: -1px;
}
thead tr td {
background: #3eaffa;
color: #fff;
}
thead tr td a {
color: #fff;
background: #cae7e4;
}
td.copy {
text-align: center;
Expand Down Expand Up @@ -227,22 +223,29 @@ h3 {
padding-top: 25px;
width: 70%;
}
div.anchorlink {
display: inline-block;
float: right;
margin-top: 1em;
margin-right: 1em;
}
div.anchorlink a {
padding: 3px;
background: #cae7e4;
font-size: 0.75em;
color: black;
}
div.guidelink,
p[dir=ltr] {
display: inline-block;
float: right;

margin: 0;
margin-top: 1em;
margin-right: 1em;
}
div.guidelink a,
p[dir=ltr] a {
display: inline-block;
border-radius: 3px;
padding: 3px;

background: #3eaffa;

font-size: 0.75em;
color: #fff;
}
Expand All @@ -265,7 +268,7 @@ input,
select {
font-size: 1em;
}
p.result {
.result {
border: 1px;
border-style: dashed;
border-color: #FE8A02;
Expand All @@ -284,3 +287,18 @@ p.result {
color: #cb2431;
transition: none;
}
h3.api {
border-bottom: 1px solid #b6b6b6;
}
details > summary {
background-color: #dbeceb;
border: none;
cursor: pointer;
list-style: none;
padding: 8px;
}
details > pre, details > p {
background-color: #e6f1f0;
margin: 0;
padding: 10px;
}
1 change: 1 addition & 0 deletions priv/css/sortable.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed priv/img/admin-logo-fill.png
Binary file not shown.
Binary file modified priv/img/admin-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions priv/js/sortable.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
{if_version_below, "24", {d, 'SYSTOOLS_APP_DEF_WITHOUT_OPTIONAL'}},
{if_version_below, "24", {d, 'OTP_BELOW_24'}},
{if_version_below, "25", {d, 'OTP_BELOW_25'}},
{if_version_below, "26", {d, 'OTP_BELOW_26'}},
{if_version_below, "27", {d, 'OTP_BELOW_27'}},
{if_var_false, debug, no_debug_info},
{if_var_true, debug, debug_info},
Expand All @@ -152,7 +153,9 @@
{git, "https://github.com/bsanyi/rebar_mix.git",
{branch, "consolidation_fix"}}}
}]}}.
{if_rebar3, {project_plugins, [configure_deps]}}.
{if_rebar3, {project_plugins, [configure_deps,
{if_var_true, tools, rebar3_format}
]}}.
{if_not_rebar3, {plugins, [
deps_erl_opts, override_deps_versions2, override_opts, configure_deps
]}}.
Expand Down
4 changes: 2 additions & 2 deletions src/ejabberd_access_permissions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,10 @@ validator(from) ->
fun(L) when is_list(L) ->
lists:map(
fun({K, V}) -> {(econf:enum([tag]))(K), (econf:binary())(V)};
(A) -> (econf:enum([ejabberd_xmlrpc, mod_cron, mod_http_api, ejabberd_ctl]))(A)
(A) -> (econf:enum([ejabberd_xmlrpc, mod_cron, mod_http_api, ejabberd_ctl, ejabberd_web_admin]))(A)
end, lists:flatten(L));
(A) ->
[(econf:enum([ejabberd_xmlrpc, mod_cron, mod_http_api, ejabberd_ctl]))(A)]
[(econf:enum([ejabberd_xmlrpc, mod_cron, mod_http_api, ejabberd_ctl, ejabberd_web_admin]))(A)]
end;
validator(what) ->
econf:and_then(
Expand Down
24 changes: 24 additions & 0 deletions src/ejabberd_acme.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
%% WebAdmin
-export([webadmin_menu_node/3, webadmin_page_node/3]).

-include("logger.hrl").
-include("ejabberd_commands.hrl").
-include("ejabberd_http.hrl").
-include("ejabberd_web_admin.hrl").
-include_lib("public_key/include/public_key.hrl").
-include_lib("stdlib/include/ms_transform.hrl").
-include_lib("xmpp/include/xmpp.hrl").

-define(CALL_TIMEOUT, timer:minutes(10)).

Expand Down Expand Up @@ -108,6 +113,8 @@ init([]) ->
ejabberd_hooks:add(config_reloaded, ?MODULE, register_certfiles, 40),
ejabberd_hooks:add(ejabberd_started, ?MODULE, ejabberd_started, 110),
ejabberd_hooks:add(config_reloaded, ?MODULE, ejabberd_started, 110),
ejabberd_hooks:add(webadmin_menu_node, ?MODULE, webadmin_menu_node, 110),
ejabberd_hooks:add(webadmin_page_node, ?MODULE, webadmin_page_node, 110),
ejabberd_commands:register_commands(get_commands_spec()),
register_certfiles(),
{ok, #state{}}.
Expand Down Expand Up @@ -153,6 +160,8 @@ terminate(_Reason, _State) ->
ejabberd_hooks:delete(config_reloaded, ?MODULE, register_certfiles, 40),
ejabberd_hooks:delete(ejabberd_started, ?MODULE, ejabberd_started, 110),
ejabberd_hooks:delete(config_reloaded, ?MODULE, ejabberd_started, 110),
ejabberd_hooks:delete(webadmin_menu_node, ?MODULE, webadmin_menu_node, 110),
ejabberd_hooks:delete(webadmin_page_node, ?MODULE, webadmin_page_node, 110),
ejabberd_commands:unregister_commands(get_commands_spec()).

code_change(_OldVsn, State, _Extra) ->
Expand Down Expand Up @@ -547,6 +556,21 @@ list_certificates() ->
{Domain, Path, sets:is_element(E, Used)}
end, Known)).

%%%===================================================================
%%% WebAdmin
%%%===================================================================

webadmin_menu_node(Acc, _Node, _Lang) ->
Acc ++ [{<<"acme">>, <<"ACME">>}].

webadmin_page_node(_, Node, #request{path = [<<"acme">>]} = R) ->
Head = ?H1GLraw(<<"ACME Certificates">>, <<"admin/configuration/basic/#acme">>, <<"ACME">>),
Set = [ejabberd_cluster:call(Node, ejabberd_web_admin, make_command, [request_certificate, R]),
ejabberd_cluster:call(Node, ejabberd_web_admin, make_command, [revoke_certificate, R])],
Get = [ejabberd_cluster:call(Node, ejabberd_web_admin, make_command, [list_certificates, R])],
{stop, Head ++ Get ++ Set};
webadmin_page_node(Acc, _, _) -> Acc.

%%%===================================================================
%%% Other stuff
%%%===================================================================
Expand Down
Loading

0 comments on commit 0380695

Please sign in to comment.