Explorar o código

feat: added admin button to resend email verification email

Kristian Vos %!s(int64=3) %!d(string=hai) anos
pai
achega
160c53a77f
Modificáronse 2 ficheiros con 68 adicións e 0 borrados
  1. 56 0
      backend/logic/actions/users.js
  2. 12 0
      frontend/src/components/modals/EditUser.vue

+ 56 - 0
backend/logic/actions/users.js

@@ -2658,6 +2658,62 @@ export default {
 		);
 	},
 
+	/**
+	 * Resends the verify email email
+	 *
+	 * @param {object} session - the session object automatically added by the websocket
+	 * @param {string} userId - the user id of the person to resend the email to
+	 * @param {Function} cb - gets called with the result
+	 */
+	 resendVerifyEmail: isAdminRequired(async function resendVerifyEmail(session, userId, cb) {
+		const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
+		const verifyEmailSchema = await MailModule.runJob("GET_SCHEMA", { schemaName: "verifyEmail" }, this);
+
+		async.waterfall(
+			[
+				next => {
+					return userModel.findOne({ _id: userId }, next);
+				},
+
+				(user, next) => {
+					if (!user) return next("User not found.");
+					if (user.email.verified) return next("The user's email is already verified.");
+					return next(null, user);
+				},
+
+				(user, next) => {
+					verifyEmailSchema(user.email.address, user.username, user.email.verificationToken, err => {
+						next(err);
+					});
+				}
+			],
+			async err => {
+				if (err && err !== true) {
+					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+
+					this.log(
+						"ERROR",
+						"RESEND_VERIFY_EMAIL",
+						`Couldn't resend verify email for user "${userId}". '${err}'`
+					);
+
+					return cb({ status: "error", message: err });
+				}
+
+				this.log(
+					"SUCCESS",
+					"RESEND_VERIFY_EMAIL",
+					`Resent verify email for user "${userId}".`
+				);
+
+				return cb({
+					status: "success",
+					message: "Email resent successfully."
+				});
+			}
+		);
+	}),
+
 	/**
 	 * Bans a user by userId
 	 *

+ 12 - 0
frontend/src/components/modals/EditUser.vue

@@ -76,6 +76,9 @@
 				</div>
 			</template>
 			<template #footer>
+				<confirm @confirm="resendVerificationEmail()">
+					<a class="button is-warning"> Resend verification email </a>
+				</confirm>
 				<confirm @confirm="removeSessions()">
 					<a class="button is-warning"> Remove all sessions </a>
 				</confirm>
@@ -232,6 +235,15 @@ export default {
 				}
 			);
 		},
+		resendVerificationEmail() {
+			this.socket.dispatch(
+				`users.resendVerifyEmail`,
+				this.user._id,
+				res => {
+					new Toast(res.message);
+				}
+			);
+		},
 		removeAccount() {
 			this.socket.dispatch(`users.adminRemove`, this.user._id, res => {
 				new Toast(res.message);