Allow to change an user's password through the users.modify mix task

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-10-04 17:38:37 +02:00
parent 4c66162e7e
commit 5dd24e1c9e
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
3 changed files with 19 additions and 0 deletions

View file

@ -16,6 +16,7 @@ defmodule Mix.Tasks.Mobilizon.Users.Modify do
rest, rest,
strict: [ strict: [
email: :string, email: :string,
password: :string,
disable: :boolean, disable: :boolean,
enable: :boolean, enable: :boolean,
user: :boolean, user: :boolean,
@ -30,6 +31,7 @@ defmodule Mix.Tasks.Mobilizon.Users.Modify do
disable? = Keyword.get(options, :disable, false) disable? = Keyword.get(options, :disable, false)
enable? = Keyword.get(options, :enable, false) enable? = Keyword.get(options, :enable, false)
new_email = Keyword.get(options, :email) new_email = Keyword.get(options, :email)
new_password = Keyword.get(options, :password)
if disable? && enable? do if disable? && enable? do
shell_error("Can't use both --enable and --disable options at the same time.") shell_error("Can't use both --enable and --disable options at the same time.")
@ -41,6 +43,7 @@ defmodule Mix.Tasks.Mobilizon.Users.Modify do
attrs <- %{}, attrs <- %{},
role <- calculate_role(admin?, moderator?, user?), role <- calculate_role(admin?, moderator?, user?),
attrs <- process_new_value(attrs, :email, new_email, user.email), attrs <- process_new_value(attrs, :email, new_email, user.email),
attrs <- process_new_value(attrs, :password, new_password, nil),
attrs <- process_new_value(attrs, :role, role, user.role), attrs <- process_new_value(attrs, :role, role, user.role),
attrs <- attrs <-
if(disable? && !is_nil(user.confirmed_at), if(disable? && !is_nil(user.confirmed_at),

View file

@ -107,6 +107,7 @@ defmodule Mobilizon.Users.User do
|> validate_required(@required_attrs) |> validate_required(@required_attrs)
|> unique_constraint(:email, message: dgettext("errors", "This email is already used.")) |> unique_constraint(:email, message: dgettext("errors", "This email is already used."))
|> Checker.validate_changeset() |> Checker.validate_changeset()
|> hash_password()
|> validate_length(:password, |> validate_length(:password,
min: 6, min: 6,
max: 200, max: 200,

View file

@ -310,5 +310,20 @@ defmodule Mix.Tasks.Mobilizon.UsersTest do
assert output_received == assert output_received ==
"An user has been modified with the following information:\n - email: #{@modified_email}\n - Role: #{user.role}\n - account status: Activated on #{confirmed_at} (UTC)\n" "An user has been modified with the following information:\n - email: #{@modified_email}\n - Role: #{user.role}\n - account status: Activated on #{confirmed_at} (UTC)\n"
end end
@modified_password "new one"
test "change an user's password" do
insert(:user, email: @email)
Modify.run([@email, "--password", @modified_password])
assert {:ok, %User{}} =
Mobilizon.Service.Auth.MobilizonAuthenticator.login(@email, @modified_password)
Modify.run([@email, "--password", "changed again"])
assert {:error, :bad_password} =
Mobilizon.Service.Auth.MobilizonAuthenticator.login(@email, @modified_password)
end
end end
end end