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,
strict: [
email: :string,
password: :string,
disable: :boolean,
enable: :boolean,
user: :boolean,
@ -30,6 +31,7 @@ defmodule Mix.Tasks.Mobilizon.Users.Modify do
disable? = Keyword.get(options, :disable, false)
enable? = Keyword.get(options, :enable, false)
new_email = Keyword.get(options, :email)
new_password = Keyword.get(options, :password)
if disable? && enable? do
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 <- %{},
role <- calculate_role(admin?, moderator?, user?),
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 <-
if(disable? && !is_nil(user.confirmed_at),

View File

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

View File

@ -310,5 +310,20 @@ defmodule Mix.Tasks.Mobilizon.UsersTest do
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"
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