40 lines
1.1 KiB
Ruby
40 lines
1.1 KiB
Ruby
module Users
|
|
class ChangePassword
|
|
Result = Struct.new(:ok?, :error, keyword_init: true)
|
|
|
|
def self.call(user:, current_password:, password:, password_confirmation:)
|
|
new(
|
|
user: user,
|
|
current_password: current_password,
|
|
password: password,
|
|
password_confirmation: password_confirmation
|
|
).call
|
|
end
|
|
|
|
def initialize(user:, current_password:, password:, password_confirmation:)
|
|
@user = user
|
|
@current_password = current_password.to_s
|
|
@password = password.to_s
|
|
@password_confirmation = password_confirmation.to_s
|
|
end
|
|
|
|
def call
|
|
unless @user.authenticate(@current_password)
|
|
return Result.new(ok?: false, error: :current_incorrect)
|
|
end
|
|
|
|
if @password.blank? || @password.length < 8
|
|
return Result.new(ok?: false, error: :too_short)
|
|
end
|
|
|
|
if @password != @password_confirmation
|
|
return Result.new(ok?: false, error: :mismatch)
|
|
end
|
|
|
|
@user.update!(password: @password)
|
|
@user.clear_password_reset!
|
|
Result.new(ok?: true)
|
|
end
|
|
end
|
|
end
|