26
1
mirror of https://github.com/processone/ejabberd.git synced 2025-01-03 18:02:28 +01:00

Reformat try_set_password/4 function

This commit is contained in:
Evgeny Khramtsov 2019-04-23 21:25:10 +03:00
parent cbf3fec2c8
commit fc043dd8cf

View File

@ -273,39 +273,41 @@ try_register_or_set_password(User, Server, Password,
xmpp:make_error(IQ, xmpp:err_not_allowed()) xmpp:make_error(IQ, xmpp:err_not_allowed())
end. end.
%% @doc Try to change password and return IQ response try_set_password(User, Server, Password) ->
try_set_password(User, Server, Password, #iq{lang = Lang, meta = M} = IQ) ->
case is_strong_password(Server, Password) of case is_strong_password(Server, Password) of
true -> true ->
case ejabberd_auth:set_password(User, Server, Password) of ejabberd_auth:set_password(User, Server, Password);
error_preparing_password ->
{error, invalid_password};
false ->
{error, weak_password}
end.
try_set_password(User, Server, Password, #iq{lang = Lang, meta = M} = IQ) ->
case try_set_password(User, Server, Password) of
ok -> ok ->
?INFO_MSG("~s has changed password from ~s", ?INFO_MSG("~s has changed password from ~s",
[jid:encode({User, Server, <<"">>}), [jid:encode({User, Server, <<"">>}),
ejabberd_config:may_hide_data( ejabberd_config:may_hide_data(
misc:ip_to_list(maps:get(ip, M, {0,0,0,0})))]), misc:ip_to_list(maps:get(ip, M, {0,0,0,0})))]),
xmpp:make_iq_result(IQ); xmpp:make_iq_result(IQ);
{error, empty_password} ->
Txt = <<"Empty password">>,
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang));
{error, not_allowed} -> {error, not_allowed} ->
Txt = <<"Changing password is not allowed">>, Txt = ?T("Changing password is not allowed"),
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang)); xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
{error, invalid_jid} -> {error, invalid_jid = Why} ->
xmpp:make_error(IQ, xmpp:err_jid_malformed()); xmpp:make_error(IQ, xmpp:err_jid_malformed(format_error(Why), Lang));
{error, invalid_password} -> {error, invalid_password = Why} ->
Txt = <<"Incorrect password">>, xmpp:make_error(IQ, xmpp:err_not_allowed(format_error(Why), Lang));
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang)); {error, weak_password = Why} ->
Err -> xmpp:make_error(IQ, xmpp:err_not_acceptable(format_error(Why), Lang));
?ERROR_MSG("failed to register user ~s@~s: ~p", {error, empty_password = Why} ->
[User, Server, Err]), xmpp:make_error(IQ, xmpp:err_bad_request(format_error(Why), Lang));
xmpp:make_error(IQ, xmpp:err_internal_server_error()) {error, db_failure = Why} ->
end; xmpp:make_error(IQ, xmpp:err_internal_server_error(format_error(Why), Lang));
error_preparing_password -> {error, Why} ->
ErrText = <<"The password contains unacceptable characters">>, ?ERROR_MSG("Failed to change password for user ~s@~s: ~s",
xmpp:make_error(IQ, xmpp:err_not_acceptable(ErrText, Lang)); [User, Server, format_error(Why)]),
false -> xmpp:make_error(IQ, xmpp:err_internal_server_error(format_error(Why), Lang))
ErrText = <<"The password is too weak">>,
xmpp:make_error(IQ, xmpp:err_not_acceptable(ErrText, Lang))
end. end.
try_register(User, Server, Password, SourceRaw) -> try_register(User, Server, Password, SourceRaw) ->
@ -333,7 +335,7 @@ try_register(User, Server, Password, SourceRaw) ->
false -> false ->
remove_timeout(Source), remove_timeout(Source),
{error, weak_password}; {error, weak_password};
_ -> error_preparing_password ->
remove_timeout(Source), remove_timeout(Source),
{error, invalid_password} {error, invalid_password}
end; end;
@ -385,6 +387,8 @@ format_error(weak_password) ->
?T("The password is too weak"); ?T("The password is too weak");
format_error(invalid_password) -> format_error(invalid_password) ->
?T("The password contains unacceptable characters"); ?T("The password contains unacceptable characters");
format_error(empty_password) ->
?T("Empty password");
format_error(not_allowed) -> format_error(not_allowed) ->
?T("Not allowed"); ?T("Not allowed");
format_error(exists) -> format_error(exists) ->