adding email utilities for password reset and account confirmation.
Project: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/repo Commit: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/commit/ff030373 Tree: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/tree/ff030373 Diff: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/diff/ff030373 Branch: refs/heads/develop Commit: ff030373c3850f78e97afd6e829b6f7f35094233 Parents: d5b8836 Author: scnakandala <[email protected]> Authored: Fri Apr 28 19:24:23 2017 -0400 Committer: scnakandala <[email protected]> Committed: Fri Apr 28 19:24:23 2017 -0400 ---------------------------------------------------------------------- app/config/email_templates.json | 27 ++++++++ app/config/email_templates.json.template | 27 ++++++++ app/config/pga_config.php.template | 5 ++ app/libraries/EmailUtilities.php | 91 +++++++++++++++++++++++++++ 4 files changed, 150 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/ff030373/app/config/email_templates.json ---------------------------------------------------------------------- diff --git a/app/config/email_templates.json b/app/config/email_templates.json new file mode 100644 index 0000000..0420948 --- /dev/null +++ b/app/config/email_templates.json @@ -0,0 +1,27 @@ +{ + "account_verification" : { + "subject" : "Verify Your Email Account", + "body" : [ + "<div><p>", + "Dear $firstName $lastName,<br/>", + "Someone has created a account with this email address. If this was you, click the link below to verify your email address<br/>", + "<a href=\"$url\">$url</a><br/>", + "This link will expire within $validTime minutes.<br/>", + "If you didn't create this account, just ignore this message.", + "</p></div>" + ] + }, + + "password_reset" : { + "subject" : "Password Reset Request", + "body" : [ + "<div><p>", + "Dear $firstName $lastName,<br/>", + "Someone has initiated a password reset request for the account associated with this email address. If this was you, click the link below to verify it was you.<br/>", + "<a href=\"$url\">$url</a><br/>", + "This link will expire within $validTime minutes.<br/>", + "If you didn't request a password reset, just ignore this message.", + "</p></div>" + ] + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/ff030373/app/config/email_templates.json.template ---------------------------------------------------------------------- diff --git a/app/config/email_templates.json.template b/app/config/email_templates.json.template new file mode 100644 index 0000000..7737844 --- /dev/null +++ b/app/config/email_templates.json.template @@ -0,0 +1,27 @@ +{ + "account_verification" : { + "subject" : "Verify Your Email Account", + "body" : [ + "<div><p>", + "Dear $firstName $lastName,<br/>", + "Someone has created a account with this email address. If this was you, click the link below to verify your email address<br/>", + "<a href=\"$url\">$url</a><br/>", + "This link will expire within $validTime minutes.<br/>", + "If you didn't create this account, just ignore this message.", + "</p></div>" + ] + }, + + "password_reset" : { + "subject" : "Password Reset Request", + "body" : [ + "<div><p>", + "Dear $firstName $lastName,<br/>", + "Someone has initiated a password reset request for the account associated with this email address. If this was you, click the link below to verify it was you.<br/>", + "<a href=\"$url\">$url</a><br/>", + "This link will expire within $validTime minutes.<br/>", + "If you didn't request a password reset, just ignore this message.", + "</p></div>" + ] + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/ff030373/app/config/pga_config.php.template ---------------------------------------------------------------------- diff --git a/app/config/pga_config.php.template b/app/config/pga_config.php.template index e225cf0..cd7f1df 100644 --- a/app/config/pga_config.php.template +++ b/app/config/pga_config.php.template @@ -215,6 +215,11 @@ return array( 'portal-smtp-server-port' => '587', /** + * Email verification code valid time interval in minutes + */ + 'email-verify-code-valid-time' => 360, + + /** * Set this to true if theme has set links to login */ 'theme-based-login-links-configured' => false, http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/ff030373/app/libraries/EmailUtilities.php ---------------------------------------------------------------------- diff --git a/app/libraries/EmailUtilities.php b/app/libraries/EmailUtilities.php new file mode 100644 index 0000000..c2d6741 --- /dev/null +++ b/app/libraries/EmailUtilities.php @@ -0,0 +1,91 @@ +<?php + + +class EmailUtilities +{ + + public static function sendVerifyEmailAccount($username, $firstName, $lastName, $email){ + $validTime = Config::get('pga_config.portal')['mail-verify-code-valid-time']; + $code = uniqid(); + Cache::put('PGA-VERIFY-EMAIL-' . $username, $code, $validTime); + + $emailTemplates = json_decode(File::get(app_path() . '/config/email_templates.json')); + $subject = $emailTemplates->account_verification->subject; + $body = trim(implode($emailTemplates->account_verification->body)); + + $body = str_replace("\$url", URL::to('/') . '/confirmAccountCreation?username=' . $username . '&code=' . $code, $body); + $body = str_replace("\$firstName", $firstName, $body); + $body = str_replace("\$lastName", $lastName, $body); + $body = str_replace("\$validTime", $validTime, $body); + + EmailUtilities::sendEmail($subject, [$email], $body); + } + + public static function verifyEmailVerification($username, $code){ + if(Cache::has('PGA-VERIFY-EMAIL-' . $username)){ + $storedCode = Cache::get('PGA-VERIFY-EMAIL-' . $username); + Cache::forget('PGA-VERIFY-EMAIL-' . $username); + return $storedCode == $code; + }else{ + return false; + } + } + + public static function sendPasswordResetEmail($username, $firstName, $lastName, $email){ + $validTime = Config::get('pga_config.portal')['mail-verify-code-valid-time']; + $code = uniqid(); + Cache::put('PGA-RESET-PASSWORD-' . $username, $code, $validTime); + + $emailTemplates = json_decode(File::get(app_path() . '/config/email_templates.json')); + $subject = $emailTemplates->password_reset->subject; + $body = trim(implode($emailTemplates->password_reset->body)); + + $body = str_replace("\$url", URL::to('/'). '/resetPassword?username=' . $username . '&code='.$code, $body); + $body = str_replace("\$firstName", $firstName, $body); + $body = str_replace("\$lastName", $lastName, $body); + $body = str_replace("\$validTime", $validTime, $body); + + EmailUtilities::sendEmail($subject, [$email], $body); + } + + public static function verifyPasswordResetCode($username, $code){ + if(Cache::has('PGA-RESET-PASSWORD-' . $username)){ + $storedCode = Cache::get('PGA-RESET-PASSWORD-' . $username); + Cache::forget('PGA-RESET-PASSWORD-' . $username); + return $storedCode == $code; + }else{ + return false; + } + } + + public static function sendEmail($subject, $recipients, $body){ + + $mail = new PHPMailer(); + + $mail->isSMTP(); + $mail->SMTPDebug = 3; + $mail->Host = Config::get('pga_config.portal')['portal-smtp-server-host']; + + $mail->SMTPAuth = true; + + $mail->Username = Config::get('pga_config.portal')['portal-email-username']; + $mail->Password = Config::get('pga_config.portal')['portal-email-password']; + + $mail->SMTPSecure = "tls"; + $mail->Port = intval(Config::get('pga_config.portal')['portal-smtp-server-port']); + + $mail->From = Config::get('pga_config.portal')['portal-email-username']; + $mail->FromName = "Airavata PHP Gateway"; + + $mail->Encoding = '8bit'; + $mail->ContentType = 'text/html; charset=utf-8\r\n'; + + foreach($recipients as $recipient){ + $mail->addAddress($recipient); + } + + $mail->Subject = $subject; + $mail->Body = html_entity_decode($body); + $mail->send(); + } +} \ No newline at end of file
