Archived
3
0

Crystal bindings: authd: right calls to the mailer.

This commit is contained in:
Philippe Pittoli 2023-02-03 10:23:24 +01:00
parent 37b460d52d
commit d72d85294a
3 changed files with 50 additions and 57 deletions

View File

@ -20,9 +20,9 @@ class Baguette::Configuration
property storage : String = "storage" property storage : String = "storage"
property registrations : Bool = false property registrations : Bool = false
property require_email : Bool = false property require_email : Bool = false
property activation_url : String? = nil property activation_template : String = "email-activation"
property field_subject : String? = nil property recovery_template : String = "email-recovery"
property field_from : String? = nil property mailer_exe : String = "mailer"
property read_only_profile_keys : Array(String) = Array(String).new property read_only_profile_keys : Array(String) = Array(String).new
property print_password_recovery_parameters : Bool = false property print_password_recovery_parameters : Bool = false
@ -135,6 +135,9 @@ class AuthD::Service < IPC
def run def run
Baguette::Log.title "Starting authd" Baguette::Log.title "Starting authd"
Baguette::Log.info "(mailer) Email activation template: #{@configuration.activation_template}"
Baguette::Log.info "(mailer) Email recovery template: #{@configuration.recovery_template}"
self.loop do |event| self.loop do |event|
case event.type case event.type
when LibIPC::EventType::Timer when LibIPC::EventType::Timer
@ -201,18 +204,19 @@ begin
configuration.require_email = true configuration.require_email = true
end end
parser.on "-t subject", "--subject title", "Subject of the email." do |s| parser.on "-t activation-template-name", "--activation-template name", "Email activation template." do |opt|
configuration.field_subject = s configuration.activation_template = opt
end end
parser.on "-f from-email", "--from email", "'From:' field to use in activation email." do |f| parser.on "-r recovery-template-name", "--recovery-template name", "Email recovery template." do |opt|
configuration.field_from = f configuration.recovery_template = opt
end end
parser.on "-u", "--activation-url url", "Activation URL." do |opt| parser.on "-m mailer-exe", "--mailer mailer-exe", "Application to send registration emails." do |opt|
configuration.activation_url = opt configuration.mailer_exe = opt
end end
parser.on "-x key", "--read-only-profile-key key", "Marks a user profile key as being read-only." do |key| parser.on "-x key", "--read-only-profile-key key", "Marks a user profile key as being read-only." do |key|
configuration.read_only_profile_keys.push key configuration.read_only_profile_keys.push key
end end

View File

@ -90,32 +90,29 @@ class AuthD::Request
authd.users_per_uid.update user.uid.to_s, user authd.users_per_uid.update user.uid.to_s, user
unless (activation_url = authd.configuration.activation_url).nil?
field_from = authd.configuration.field_from.not_nil!
activation_url = authd.configuration.activation_url.not_nil!
# Once the user is created and stored, we try to contact him # Once the user is created and stored, we try to contact him
if authd.configuration.print_password_recovery_parameters if authd.configuration.print_password_recovery_parameters
pp! user.login, pp! user.login,
user.contact.email.not_nil!, user.contact.email.not_nil!,
field_from,
activation_url,
user.password_renew_key.not_nil! user.password_renew_key.not_nil!
end end
unless Process.run("password-recovery-mailer", [ mailer_exe = authd.configuration.mailer_exe
"-l", user.login, template_name = authd.configuration.recovery_template
"-e", user.contact.email.not_nil!,
"-t", "Password recovery email",
"-f", field_from,
"-u", activation_url,
"-a", user.password_renew_key.not_nil!
]).success?
return Response::Error.new "cannot contact the user for password recovery" u_login = user.login
end u_email = user.contact.email.not_nil!
u_token = user.password_renew_key.not_nil!
# Once the user is created and stored, we try to contact him.
unless Process.run(mailer_exe,
# PARAMETERS
[ "send", template_name, u_email ],
# ENV
{ "LOGIN" => u_login, "TOKEN" => u_token },
true # clear environment
).success?
raise "cannot contact user #{u_login} address #{u_email}"
end end
Response::PasswordRecoverySent.new user.to_public Response::PasswordRecoverySent.new user.to_public

View File

@ -22,12 +22,6 @@ class AuthD::Request
return Response::Error.new "email required" return Response::Error.new "email required"
end end
activation_url = authd.configuration.activation_url
if activation_url.nil?
# In this case we should not accept its registration.
return Response::Error.new "No activation URL were entered. Cannot send activation mails."
end
if ! @email.nil? if ! @email.nil?
# Test on the email address format. # Test on the email address format.
grok = Grok.new [ "%{EMAILADDRESS:email}" ] grok = Grok.new [ "%{EMAILADDRESS:email}" ]
@ -58,27 +52,25 @@ class AuthD::Request
user.date_registration = Time.local user.date_registration = Time.local
begin begin
field_subject = authd.configuration.field_subject.not_nil! mailer_exe = authd.configuration.mailer_exe
field_from = authd.configuration.field_from.not_nil! template_name = authd.configuration.activation_template
activation_url = authd.configuration.activation_url.not_nil!
u_login = user.login u_login = user.login
u_email = user.contact.email.not_nil! u_email = user.contact.email.not_nil!
u_activation_key = user.contact.activation_key.not_nil! u_activation_key = user.contact.activation_key.not_nil!
# Once the user is created and stored, we try to contact him # Once the user is created and stored, we try to contact him.
unless Process.run("activation-mailer", [ unless Process.run(mailer_exe,
"-l", u_login, # PARAMETERS
"-e", u_email, [ "send", template_name, u_email ],
"-t", field_subject, # ENV
"-f", field_from, { "LOGIN" => u_login, "TOKEN" => u_activation_key },
"-u", activation_url, true # clear environment
"-a", u_activation_key ).success?
]).success? raise "cannot contact user #{u_login} address #{u_email}"
raise "cannot contact user #{user.login} address #{user.contact.email}"
end end
rescue e rescue e
Baguette::Log.error "activation-mailer: #{e}" Baguette::Log.error "mailer: #{e}"
return Response::Error.new "cannot contact the user (not registered)" return Response::Error.new "cannot contact the user (not registered)"
end end