fix issue #450. Print a hint if the auth_username module is not enabled

This commit is contained in:
Feng 2016-03-03 11:12:08 +08:00
parent cf4dfe632f
commit 652f5f5767
2 changed files with 26 additions and 5 deletions

View File

@ -26,8 +26,9 @@
-behaviour(emqttd_auth_mod). -behaviour(emqttd_auth_mod).
-export([add_user/2, remove_user/1, -export([is_enabled/0]).
lookup_user/1, all_users/0]).
-export([add_user/2, remove_user/1, lookup_user/1, all_users/0]).
%% emqttd_auth callbacks %% emqttd_auth callbacks
-export([init/1, check/3, description/0]). -export([init/1, check/3, description/0]).
@ -41,19 +42,35 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
cli(["add", Username, Password]) -> cli(["add", Username, Password]) ->
?PRINT("~p~n", [add_user(iolist_to_binary(Username), iolist_to_binary(Password))]); if_enabled(fun() ->
?PRINT("~p~n", [add_user(iolist_to_binary(Username), iolist_to_binary(Password))])
end);
cli(["del", Username]) -> cli(["del", Username]) ->
?PRINT("~p~n", [remove_user(iolist_to_binary(Username))]); if_enabled(fun() ->
?PRINT("~p~n", [remove_user(iolist_to_binary(Username))])
end);
cli(_) -> cli(_) ->
?USAGE([{"users add <Username> <Password>", "Add User"}, ?USAGE([{"users add <Username> <Password>", "Add User"},
{"users del <Username>", "Delete User"}]). {"users del <Username>", "Delete User"}]).
if_enabled(Fun) ->
case is_enabled() of
true -> Fun();
false -> hint()
end.
hint() ->
?PRINT_MSG("Please enable '{username, []}' authentication in etc/emqttd.config first.~n").
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% API %% API
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
is_enabled() ->
lists:member(?AUTH_USERNAME_TAB, mnesia:system_info(tables)).
%% @doc Add User %% @doc Add User
-spec add_user(binary(), binary()) -> ok | {error, any()}. -spec add_user(binary(), binary()) -> ok | {error, any()}.
add_user(Username, Password) -> add_user(Username, Password) ->

View File

@ -28,7 +28,7 @@
-export([load/0]). -export([load/0]).
-export([status/1, broker/1, cluster/1, bridges/1, -export([status/1, broker/1, cluster/1, users/1, bridges/1,
clients/1, sessions/1, topics/1, subscriptions/1, clients/1, sessions/1, topics/1, subscriptions/1,
plugins/1, listeners/1, vm/1, mnesia/1, trace/1]). plugins/1, listeners/1, vm/1, mnesia/1, trace/1]).
@ -141,6 +141,10 @@ cluster(_) ->
{"cluster remove <Node>","Remove the node from cluster"}, {"cluster remove <Node>","Remove the node from cluster"},
{"cluster status", "Cluster status"}]). {"cluster status", "Cluster status"}]).
%%--------------------------------------------------------------------
%% @doc Users usage
users(Args) -> emqttd_auth_username:cli(Args).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Query clients %% @doc Query clients
clients(["list"]) -> clients(["list"]) ->