emqx_dashboard_cli.erl 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2021 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_dashboard_cli).
  17. -export([ load/0
  18. , admins/1
  19. , unload/0
  20. ]).
  21. load() ->
  22. emqx_ctl:register_command(admins, {?MODULE, admins}, []).
  23. admins(["add", Username, Password]) ->
  24. admins(["add", Username, Password, ""]);
  25. admins(["add", Username, Password, Tag]) ->
  26. case emqx_dashboard_admin:add_user(bin(Username), bin(Password), bin(Tag)) of
  27. ok ->
  28. emqx_ctl:print("ok~n");
  29. {error, already_existed} ->
  30. emqx_ctl:print("Error: already existed~n");
  31. {error, Reason} ->
  32. emqx_ctl:print("Error: ~p~n", [Reason])
  33. end;
  34. admins(["passwd", Username, Password]) ->
  35. Status = emqx_dashboard_admin:change_password(bin(Username), bin(Password)),
  36. emqx_ctl:print("~p~n", [Status]);
  37. admins(["del", Username]) ->
  38. Status = emqx_dashboard_admin:remove_user(bin(Username)),
  39. emqx_ctl:print("~p~n", [Status]);
  40. admins(_) ->
  41. emqx_ctl:usage([{"admins add <Username> <Password> <Tags>", "Add dashboard user"},
  42. {"admins passwd <Username> <Password>", "Reset dashboard user password"},
  43. {"admins del <Username>", "Delete dashboard user" }]).
  44. unload() ->
  45. emqx_ctl:unregister_command(admins).
  46. bin(S) -> iolist_to_binary(S).