Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send mail when password, username or mail is updated #509

Merged
merged 1 commit into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SoftwareMill Bootzooka password change notification
Dear {{userName}},

Your password has been changed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SoftwareMill Bootzooka profile details change notification
Dear {{userName}},

Your user name and/or email has been changed.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ class EmailTemplates {
def passwordReset(userName: String, resetLink: String): EmailSubjectContent = {
EmailTemplateRenderer("resetPassword", Map("userName" -> userName, "resetLink" -> resetLink))
}

def passwordChangeNotification(userName: String): EmailSubjectContent = {
EmailTemplateRenderer("passwordChangeNotification", Map("userName" -> userName))
}

def profileDetailsChangeNotification(userName: String): EmailSubjectContent = {
EmailTemplateRenderer("profileDetailsChangeNotification", Map("userName" -> userName))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,47 @@ class UserService(
} yield apiKey

def changeUser(userId: Id @@ User, newLogin: String, newEmail: String): ConnectionIO[Unit] = {
def changeLogin(newLogin: String): ConnectionIO[Unit] = {
def changeLogin(newLogin: String): ConnectionIO[Boolean] = {
val newLoginLowerCased = newLogin.lowerCased
userModel.findByLogin(newLoginLowerCased).flatMap {
case Some(user) if user.id != userId => Fail.IncorrectInput(LoginAlreadyUsed).raiseError[ConnectionIO, Unit]
case Some(user) if user.login == newLogin => ().pure[ConnectionIO]
case Some(user) if user.id != userId => Fail.IncorrectInput(LoginAlreadyUsed).raiseError[ConnectionIO, Boolean]
case Some(user) if user.login == newLogin => false.pure[ConnectionIO]
case _ =>
logger.debug(s"Changing login for user: $userId, to: $newLogin")
userModel.updateLogin(userId, newLogin, newLoginLowerCased)
userModel.updateLogin(userId, newLogin, newLoginLowerCased).map(_ => true)
}
}

def changeEmail(newEmail: String): ConnectionIO[Unit] = {
def changeEmail(newEmail: String): ConnectionIO[Boolean] = {
val newEmailLowerCased = newEmail.lowerCased
userModel.findByEmail(newEmailLowerCased).flatMap {
case Some(user) if user.id != userId => Fail.IncorrectInput(EmailAlreadyUsed).raiseError[ConnectionIO, Unit]
case Some(user) if user.emailLowerCased == newEmailLowerCased => ().pure[ConnectionIO]
case Some(user) if user.id != userId => Fail.IncorrectInput(EmailAlreadyUsed).raiseError[ConnectionIO, Boolean]
case Some(user) if user.emailLowerCased == newEmailLowerCased => false.pure[ConnectionIO]
case _ =>
logger.debug(s"Changing email for user: $userId, to: $newEmail")
userModel.updateEmail(userId, newEmailLowerCased)
userModel.updateEmail(userId, newEmailLowerCased).map(_ => true)
}
}

changeLogin(newLogin) >> changeEmail(newEmail)
def doChange(newLogin: String, newEmail: String): ConnectionIO[Boolean] = {
for {
loginUpdated <- changeLogin(newLogin)
emailUpdated <- changeEmail(newEmail)
} yield loginUpdated || emailUpdated
}

def sendMail(user: User) = {
val confirmationEmail = emailTemplates.profileDetailsChangeNotification(user.login)
emailScheduler(EmailData(user.emailLowerCased, confirmationEmail))
}

doChange(newLogin, newEmail) flatMap { anyUpdate =>
if (anyUpdate) {
findById(userId).flatMap(user => sendMail(user))
} else {
().pure[ConnectionIO]
}
}
}

def changePassword(userId: Id @@ User, currentPassword: String, newPassword: String): ConnectionIO[Unit] =
Expand All @@ -103,6 +121,8 @@ class UserService(
_ <- verifyPassword(user, currentPassword, validationErrorMsg = "Incorrect current password")
_ = logger.debug(s"Changing password for user: $userId")
_ <- userModel.updatePassword(userId, User.hashPassword(newPassword))
confirmationEmail = emailTemplates.passwordChangeNotification(user.login)
_ <- emailScheduler(EmailData(user.emailLowerCased, confirmationEmail))
} yield ()

private def userOrNotFound(op: ConnectionIO[Option[User]]): ConnectionIO[User] = {
Expand Down