diff --git a/lang/fr.json b/lang/fr.json index f05603923011e2bfcd8edb97c1899257956efe22..2adc3540097f9a3ec113645b36b7d23f6b644f21 100755 --- a/lang/fr.json +++ b/lang/fr.json @@ -79,6 +79,10 @@ "notificationForgotPasswordBody" : "Bonjour,<br/>Vous avez demandé à réinitialiser le mot de passe de votre compte Maarch Parapheur.<br/>Pour modifier votre mot de passe, merci de bien vouloir cliquer sur le lien ci-dessous :<br/>", "notificationForgotPasswordFooter" : "<br/><br/>Si vous n'êtes pas à l'origine de cette demande, merci d'ignorer ce courriel.<br/>Pour toutes questions, merci de contacter l'administrateur technique de la solution.", "notificationForgotPasswordSubject" : "[Maarch Parapheur] Demande de réinitialisation de mot de passe", + "notificationNewAccountSubject" : "[Maarch Parapheur] Activer votre compte utilisateur", + "notificationNewAccountBody" : "Bienvenue,<br/><br/>Vous disposez maintenant d'un compte dans l'application Maarch Parapheur.<br/><br/>Pour vous connecter et définir votre mot de passe, merci de bien vouloir cliquer sur le lien ci-dessous :<br/><br/>", + "notificationNewAccountId" : "<br/><br/><b>Votre identifiant :</b>", + "notificationNewAccountFooter" : "<br/><br/>Ce message est envoyé automatiquement à la suite d'une action de l'administrateur.<br/>Merci de ne pas y répondre.<br/><br/>Pour toutes questions, merci de contacter l'administrateur technique de la solution.", "notifications" : "Notifications", "numberRequired" : "1 chiffre requis", "onRange" : "le", diff --git a/src/app/user/controllers/UserController.php b/src/app/user/controllers/UserController.php index 03f2c975e581bc155bf03fa31bd750e7820c21ed..f60ec2beda759f88d82fe79b1b3f7e5b07b7e426 100755 --- a/src/app/user/controllers/UserController.php +++ b/src/app/user/controllers/UserController.php @@ -163,6 +163,10 @@ class UserController 'message' => "{userAdded} : {$body['firstname']} {$body['lastname']}" ]); + if (empty($body['isRest'])) { + AuthenticationController::sendAccountActivationNotification(['userId' => $id, 'userEmail' => $body['email']]); + } + return $response->withJson(['id' => $id]); } @@ -581,7 +585,7 @@ class UserController $GLOBALS['id'] = $user['id']; - $resetToken = AuthenticationController::getResetJWT(); + $resetToken = AuthenticationController::getResetJWT(['id' => $GLOBALS['id'], 'expirationTime' => 3600]); UserModel::update(['set' => ['reset_token' => $resetToken], 'where' => ['id = ?'], 'data' => [$user['id']]]); $user['preferences'] = json_decode($user['preferences'], true); diff --git a/src/app/user/models/UserModel.php b/src/app/user/models/UserModel.php index 19bca24fb12f9352ffbc2e90f18fa0ee517bce35..87e245b07c4fda3c24cf52d108950aa8e135fdef 100755 --- a/src/app/user/models/UserModel.php +++ b/src/app/user/models/UserModel.php @@ -82,6 +82,9 @@ class UserModel ValidatorModel::notEmpty($args, ['login', 'email', 'firstname', 'lastname', 'picture']); ValidatorModel::stringType($args, ['login', 'email', 'firstname', 'lastname', 'picture', 'mode', 'signatureModes']); + if (empty($args['password'])) { + $args['password'] = AuthenticationModel::generatePassword(); + } $nextSequenceId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'users_id_seq']); DatabaseModel::insert([ @@ -90,7 +93,7 @@ class UserModel 'id' => $nextSequenceId, 'login' => $args['login'], 'email' => $args['email'], - 'password' => AuthenticationModel::getPasswordHash('maarch'), + 'password' => $args['password'], 'firstname' => $args['firstname'], 'lastname' => $args['lastname'], '"isRest"' => empty($args['isRest']) ? 'false' : 'true', diff --git a/src/core/controllers/AuthenticationController.php b/src/core/controllers/AuthenticationController.php index e2345441d16d0230b0355c05ce4e92c26883c8ac..65e0f8a9412b1a478e5a7b0d243e3b7ef4aa0d4a 100755 --- a/src/core/controllers/AuthenticationController.php +++ b/src/core/controllers/AuthenticationController.php @@ -15,11 +15,14 @@ namespace SrcCore\controllers; use Configuration\models\ConfigurationModel; +use Email\controllers\EmailController; use Firebase\JWT\JWT; use History\controllers\HistoryController; use Respect\Validation\Validator; use Slim\Http\Request; use Slim\Http\Response; +use SrcCore\controllers\LanguageController; +use SrcCore\controllers\UrlController; use SrcCore\models\AuthenticationModel; use SrcCore\models\CoreConfigModel; use SrcCore\models\PasswordModel; @@ -299,12 +302,12 @@ class AuthenticationController return $jwt; } - public static function getResetJWT() + public static function getResetJWT($args = []) { $token = [ - 'exp' => time() + 3600, + 'exp' => time() + $args['expirationTime'], 'user' => [ - 'id' => $GLOBALS['id'] + 'id' => $args['id'] ], 'connection' => ConfigurationModel::getConnection() ]; @@ -314,6 +317,29 @@ class AuthenticationController return $jwt; } + public static function sendAccountActivationNotification(array $args) + { + $resetToken = AuthenticationController::getResetJWT(['id' => $args['userId'], 'expirationTime' => 1209600]); // 14 days + UserModel::update(['set' => ['reset_token' => $resetToken], 'where' => ['id = ?'], 'data' => [$args['userId']]]); + + $user = UserModel::getById(['select' => ['login'], 'id' => $args['userId']]); + $lang = LanguageController::get(['lang' => 'fr']); + + $url = UrlController::getCoreUrl() . 'dist/update-password?token=' . $resetToken; + EmailController::createEmail([ + 'userId' => $args['userId'], + 'data' => [ + 'sender' => 'Notification', + 'recipients' => [$args['userEmail']], + 'subject' => $lang['notificationNewAccountSubject'], + 'body' => $lang['notificationNewAccountBody'] . $url . $lang['notificationNewAccountId'] . ' ' . $user['login'] . $lang['notificationNewAccountFooter'], + 'isHtml' => true + ] + ]); + + return true; + } + public static function isRouteAvailable(array $args) { ValidatorModel::notEmpty($args, ['userId', 'currentRoute']); diff --git a/src/core/models/AuthenticationModel.php b/src/core/models/AuthenticationModel.php index 97017ee2a6a3851288134b20520be8aa049b49ba..8b96f7ccdf6f332c15f0e50ed51aeea0437f7217 100755 --- a/src/core/models/AuthenticationModel.php +++ b/src/core/models/AuthenticationModel.php @@ -68,4 +68,17 @@ class AuthenticationModel return $password; } + + public static function generatePassword() + { + $length = rand(50, 70); + $chars = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz!@$%^*_=+,.?'; + $count = mb_strlen($chars); + for ($i = 0, $password = ''; $i < $length; $i++) { + $index = rand(0, $count - 1); + $password .= mb_substr($chars, $index, 1); + } + + return $password; + } }