Browse Source

refactor: Update user validation and constraints

Owen Diffey 1 week ago
parent
commit
1777b4343d

+ 12 - 15
backend/src/modules/DataModule/migrations/1725485641-create-users-table.ts

@@ -10,11 +10,13 @@ export const up = async ({
 			// @ts-ignore
 			// @ts-ignore
 			type: DataTypes.OBJECTID,
 			type: DataTypes.OBJECTID,
 			autoNull: false,
 			autoNull: false,
-			primaryKey: true
+			primaryKey: true,
+			unique: true
 		},
 		},
 		username: {
 		username: {
-			type: DataTypes.STRING,
-			allowNull: false
+			type: DataTypes.CITEXT,
+			allowNull: false,
+			unique: true
 		},
 		},
 		role: {
 		role: {
 			type: DataTypes.ENUM("admin", "moderator", "user"),
 			type: DataTypes.ENUM("admin", "moderator", "user"),
@@ -27,11 +29,13 @@ export const up = async ({
 		},
 		},
 		emailVerificationToken: {
 		emailVerificationToken: {
 			type: DataTypes.STRING,
 			type: DataTypes.STRING,
-			allowNull: true
+			allowNull: true,
+			unique: true
 		},
 		},
 		emailAddress: {
 		emailAddress: {
-			type: DataTypes.STRING,
-			allowNull: false
+			type: DataTypes.CITEXT,
+			allowNull: false,
+			unique: true
 		},
 		},
 		avatarType: {
 		avatarType: {
 			type: DataTypes.ENUM("gravatar", "initials"),
 			type: DataTypes.ENUM("gravatar", "initials"),
@@ -58,20 +62,13 @@ export const up = async ({
 		},
 		},
 		passwordResetCode: {
 		passwordResetCode: {
 			type: DataTypes.STRING,
 			type: DataTypes.STRING,
-			allowNull: true
+			allowNull: true,
+			unique: true
 		},
 		},
 		passwordResetExpiresAt: {
 		passwordResetExpiresAt: {
 			type: DataTypes.DATE,
 			type: DataTypes.DATE,
 			allowNull: true
 			allowNull: true
 		},
 		},
-		passwordSetCode: {
-			type: DataTypes.STRING,
-			allowNull: true
-		},
-		passwordSetExpiresAt: {
-			type: DataTypes.DATE,
-			allowNull: true
-		},
 		songsRequested: {
 		songsRequested: {
 			type: DataTypes.BIGINT,
 			type: DataTypes.BIGINT,
 			allowNull: false,
 			allowNull: false,

+ 12 - 20
backend/src/modules/DataModule/models/User.ts

@@ -55,10 +55,6 @@ export class User extends Model<
 
 
 	declare passwordResetExpiresAt: CreationOptional<Date | null>;
 	declare passwordResetExpiresAt: CreationOptional<Date | null>;
 
 
-	declare passwordSetCode: CreationOptional<string | null>;
-
-	declare passwordSetExpiresAt: CreationOptional<Date | null>;
-
 	// End services
 	// End services
 
 
 	// Statistics
 	// Statistics
@@ -164,11 +160,13 @@ export const schema = {
 	_id: {
 	_id: {
 		type: DataTypes.OBJECTID,
 		type: DataTypes.OBJECTID,
 		autoNull: false,
 		autoNull: false,
-		primaryKey: true
+		primaryKey: true,
+		unique: true
 	},
 	},
 	username: {
 	username: {
-		type: DataTypes.STRING,
-		allowNull: false
+		type: DataTypes.CITEXT,
+		allowNull: false,
+		unique: true
 	},
 	},
 	role: {
 	role: {
 		type: DataTypes.ENUM(...Object.values(UserRole)),
 		type: DataTypes.ENUM(...Object.values(UserRole)),
@@ -181,11 +179,13 @@ export const schema = {
 	},
 	},
 	emailVerificationToken: {
 	emailVerificationToken: {
 		type: DataTypes.STRING,
 		type: DataTypes.STRING,
-		allowNull: true
+		allowNull: true,
+		unique: true
 	},
 	},
 	emailAddress: {
 	emailAddress: {
-		type: DataTypes.STRING,
-		allowNull: false
+		type: DataTypes.CITEXT,
+		allowNull: false,
+		unique: true
 	},
 	},
 	avatarType: {
 	avatarType: {
 		type: DataTypes.ENUM(...Object.values(UserAvatarType)),
 		type: DataTypes.ENUM(...Object.values(UserAvatarType)),
@@ -205,20 +205,13 @@ export const schema = {
 	},
 	},
 	passwordResetCode: {
 	passwordResetCode: {
 		type: DataTypes.STRING,
 		type: DataTypes.STRING,
-		allowNull: true
+		allowNull: true,
+		unique: true
 	},
 	},
 	passwordResetExpiresAt: {
 	passwordResetExpiresAt: {
 		type: DataTypes.DATE,
 		type: DataTypes.DATE,
 		allowNull: true
 		allowNull: true
 	},
 	},
-	passwordSetCode: {
-		type: DataTypes.STRING,
-		allowNull: true
-	},
-	passwordSetExpiresAt: {
-		type: DataTypes.DATE,
-		allowNull: true
-	},
 	songsRequested: {
 	songsRequested: {
 		type: DataTypes.BIGINT,
 		type: DataTypes.BIGINT,
 		allowNull: false,
 		allowNull: false,
@@ -289,7 +282,6 @@ export const options = {
 				"emailVerificationToken",
 				"emailVerificationToken",
 				"password",
 				"password",
 				"passwordResetCode",
 				"passwordResetCode",
-				"passwordSetCode",
 			]
 			]
 		}
 		}
 	}
 	}

+ 34 - 3
backend/src/modules/DataModule/models/User/jobs/UpdateById.ts

@@ -5,6 +5,7 @@ import { UserAvatarType } from "../UserAvatarType";
 import { UserAvatarColor } from "../UserAvatarColor";
 import { UserAvatarColor } from "../UserAvatarColor";
 import isAdmin from "@/modules/DataModule/permissions/isAdmin";
 import isAdmin from "@/modules/DataModule/permissions/isAdmin";
 import isSelf from "@/modules/DataModule/permissions/modelPermissions/isSelf";
 import isSelf from "@/modules/DataModule/permissions/modelPermissions/isSelf";
+import { Op } from "sequelize";
 
 
 export default class UpdateById extends UpdateByIdJob {
 export default class UpdateById extends UpdateByIdJob {
 	protected static _model = User;
 	protected static _model = User;
@@ -18,8 +19,8 @@ export default class UpdateById extends UpdateByIdJob {
 			.pattern(/^[0-9a-fA-F]{24}$/)
 			.pattern(/^[0-9a-fA-F]{24}$/)
 			.required(),
 			.required(),
 		query: Joi.object({
 		query: Joi.object({
-			username: Joi.string().max(64).optional(), // TODO: format and unique validation
-			emailAddress: Joi.string().max(64).optional(), // TODO: format and unique validation
+			username: Joi.string().min(2).max(32).regex(/^_*[a-zA-Z0-9][a-zA-Z0-9_]*$/).optional(),
+			emailAddress: Joi.string().email().min(3).max(254).optional(), // TODO: Whitelist regex
 			name: Joi.string().max(64).optional(),
 			name: Joi.string().max(64).optional(),
 			location: Joi.string().max(50).optional().allow(null, ""),
 			location: Joi.string().max(50).optional().allow(null, ""),
 			bio: Joi.string().max(200).optional().allow(null, ""), // TODO: Nullify empty strings
 			bio: Joi.string().max(200).optional().allow(null, ""), // TODO: Nullify empty strings
@@ -33,5 +34,35 @@ export default class UpdateById extends UpdateByIdJob {
 			anonymousSongRequests: Joi.boolean().optional(),
 			anonymousSongRequests: Joi.boolean().optional(),
 			activityWatch: Joi.boolean().optional()
 			activityWatch: Joi.boolean().optional()
 		}).required()
 		}).required()
-	});
+	})
+		.external(async ({ _id, query }) => {
+			if (query.emailAddress === undefined) return;
+
+			const user = await User.findOne({
+				where: {
+					[Op.not]: {
+						_id
+					},
+					username: query.username
+				}
+			});
+
+			if (user instanceof User)
+				throw new Error("A user with that username already exists.");
+		}, "uniqueUsername")
+		.external(async ({ _id, query }) => {
+			if (query.emailAddress === undefined) return;
+
+			const user = await User.findOne({
+				where: {
+					[Op.not]: {
+						_id
+					},
+					emailAddress: query.emailAddress
+				}
+			});
+
+			if (user instanceof User)
+				throw new Error("A user with that email already exists.");
+		}, "uniqueEmail");
 }
 }