diff --git a/src/language/en-GB/en-GB.plg_api_users.ini b/src/language/en-GB/en-GB.plg_api_users.ini index 1e75e9c..1f81371 100755 --- a/src/language/en-GB/en-GB.plg_api_users.ini +++ b/src/language/en-GB/en-GB.plg_api_users.ini @@ -15,3 +15,6 @@ PLG_API_USERS_UNSUPPORTED_METHOD_POST="unsupported method,please use get method" PLG_API_USERS_USERS="users/" PLG_API_USERS_IN_DELETE="in delete" PLG_API_USERS_IN_POST="in post" + +PLG_API_USERS_NEW_USER_EMAIL_BODY="Hello %s,\n\n\nYou have been added as a User to %s by an Administrator.\n\nThis email has your username and password to log in to %s\n\nUsername: %s\nPassword: %s\n\n\nPlease do not respond to this message as it is automatically generated and is for information purposes only." +PLG_API_USERS_NEW_USER_EMAIL_SUBJECT="New User Details" diff --git a/src/users/user.php b/src/users/user.php index bee8825..9d19c4a 100644 --- a/src/users/user.php +++ b/src/users/user.php @@ -8,6 +8,7 @@ // No direct access. defined('_JEXEC') or die(); +use Joomla\Registry\Registry; /** * User Api. @@ -19,6 +20,8 @@ */ class UsersApiResourceUser extends ApiResource { + public $shouldSendMail = 1; + /** * Function to create and edit user record. * @@ -44,6 +47,16 @@ public function post() return; } + if (isset($formData['password'])) + { + $formData['password_clear'] = $formData['password']; + } + + if (isset($formData['sendmail'])) + { + $this->shouldSendMail = $formData['sendmail']; + } + // Get current logged in user. $my = JFactory::getUser(); @@ -243,6 +256,11 @@ private function storeUser($user, $formData, $isNew = 0) if ($isNew) { + if ($this->shouldSendMail) + { + $mail_sent = $this->sendNewUserEmail($formData); + } + $response->message = JText::_('PLG_API_USERS_ACCOUNT_CREATED_SUCCESSFULLY_MESSAGE'); } else @@ -358,4 +376,69 @@ private function retriveUser($xidentifier, $userIdentifier) return $user; } + + /** + * Send new user create mail + * + * @param array $user this contains user data. + * + * @return object + */ + public function sendNewUserEmail($user) + { + $app = JFactory::getApplication(); + $lang = JFactory::getLanguage(); + $defaultLocale = $lang->getTag(); + + /** + * Look for user language. Priority: + * 1. User frontend language + * 2. User backend language + */ + $userParams = new Registry($user['params']); + $userLocale = $userParams->get('language', $userParams->get('admin_language', $defaultLocale)); + + if ($userLocale !== $defaultLocale) + { + $lang->setLanguage($userLocale); + } + + $lang->load('plg_api_users', JPATH_ADMINISTRATOR); + + // Compute the mail subject. + $emailSubject = JText::sprintf( + 'PLG_API_USERS_NEW_USER_EMAIL_SUBJECT', + $user['name'], + $app->get('sitename') + ); + + // Compute the mail body. + $emailBody = JText::sprintf( + 'PLG_API_USERS_NEW_USER_EMAIL_BODY', + $user['name'], + $app->get('sitename'), + JUri::root(), + $user['username'], + $user['password_clear'] + ); + + $res = JFactory::getMailer()->sendMail( + $app->get('mailfrom'), + $app->get('fromname'), + $user['email'], + $emailSubject, + $emailBody + ); + + if ($res === false) + { + $app->enqueueMessage(JText::_('JERROR_SENDING_EMAIL'), 'warning'); + } + + // Set application language back to default if we changed it + if ($userLocale !== $defaultLocale) + { + $lang->setLanguage($defaultLocale); + } + } }