chore: refactor init_default_admin_user/0 function

This commit is contained in:
zhongwencool 2022-10-28 21:49:16 +08:00
parent 93924f567f
commit 9167055f56
1 changed files with 23 additions and 21 deletions

View File

@ -44,7 +44,7 @@
, change_password/3 , change_password/3
, all_users/0 , all_users/0
, check/2 , check/2
, add_bootstrap_users/0 , init_bootstrap_users/0
]). ]).
%% gen_server Function Exports %% gen_server Function Exports
@ -197,18 +197,18 @@ check(Username, Password) ->
{error, <<"Username/Password error">>} {error, <<"Username/Password error">>}
end. end.
add_bootstrap_users() -> init_bootstrap_users() ->
Bootstrap = application:get_env(emqx_dashboard, bootstrap_users_file, undefined), Bootstrap = application:get_env(emqx_dashboard, bootstrap_users_file, undefined),
Size = mnesia:table_info(mqtt_admin, size), Size = mnesia:table_info(mqtt_admin, size),
add_bootstrap_users(Bootstrap, Size). init_bootstrap_users(Bootstrap, Size).
add_bootstrap_users(undefined, _) -> ok; init_bootstrap_users(undefined, _) -> ok;
add_bootstrap_users(_File, Size)when Size > 0 -> ok; init_bootstrap_users(_File, Size)when Size > 0 -> ok;
add_bootstrap_users(File, 0) -> init_bootstrap_users(File, 0) ->
case file:open(File, [read, binary]) of case file:open(File, [read, binary]) of
{ok, Dev} -> {ok, Dev} ->
{ok, MP} = re:compile(<<"(\.+):(\.+$)">>, [ungreedy]), {ok, MP} = re:compile(<<"(\.+):(\.+$)">>, [ungreedy]),
case add_bootstrap_users(File, Dev, MP) of case init_bootstrap_users(File, Dev, MP) of
ok -> ok; ok -> ok;
Error -> Error ->
%% if failed add bootstrap users, we should clear all bootstrap users %% if failed add bootstrap users, we should clear all bootstrap users
@ -223,7 +223,7 @@ add_bootstrap_users(File, 0) ->
Error Error
end. end.
add_bootstrap_users(File, Dev, MP) -> init_bootstrap_users(File, Dev, MP) ->
try try
add_bootstrap_user(File, Dev, MP, 1) add_bootstrap_user(File, Dev, MP, 1)
catch catch
@ -270,21 +270,23 @@ is_valid_pwd(<<Salt:4/binary, Hash/binary>>, Password) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
init([]) -> init([]) ->
case add_bootstrap_users() of case init_bootstrap_users() of
ok -> ok -> init_default_admin_user();
case binenv(default_user_username) of {error, Error} -> {stop, Error}
<<>> -> ok;
UserName ->
%% Add default admin user
{ok, _} = mnesia:subscribe({table, mqtt_admin, simple}),
PasswordHash = ensure_default_user_in_db(UserName),
ok = ensure_default_user_passwd_hashed_in_pt(PasswordHash),
ok = maybe_warn_default_pwd()
end,
{ok, state};
Error -> {stop, Error}
end. end.
init_default_admin_user() ->
case binenv(default_user_username) of
<<>> -> ok;
UserName ->
%% Add default admin user
{ok, _} = mnesia:subscribe({table, mqtt_admin, simple}),
PasswordHash = ensure_default_user_in_db(UserName),
ok = ensure_default_user_passwd_hashed_in_pt(PasswordHash),
ok = maybe_warn_default_pwd()
end,
{ok, state}.
handle_call(_Req, _From, State) -> handle_call(_Req, _From, State) ->
{reply, error, State}. {reply, error, State}.