Files
joelthomastrenser dd29c7324f Fix: Prevent duplicate usernames across all user states
Changes:
- Updated username duplicate validation to consider all existing users.
- Prevented reuse of usernames belonging to deleted/disabled accounts.
- Fixed authentication conflicts caused by duplicate usernames.

Fixes #1809
2026-06-01 17:40:27 +05:30

173 lines
4.9 KiB
C++

/*
* File: Validator.cpp
* Description: Validates inputs like phone number, email, password
* Author: Trenser
* Created: 18-May-2026
*/
#include <string>
#include <cctype>
#include "Validator.h"
/*
* Function: isPhoneNumberValid
* Description: Validates whether the given string is a valid phone number.
* Parameters:
* phoneNumber - string containing the phone number to validate
* Returns:
* bool - true if the phone number is valid (10 digits, all numeric), false otherwise
*/
bool util::isPhoneNumberValid(const std::string& phoneNumber)
{
if (phoneNumber.size() != 10)
{
return false;
}
return std::all_of(phoneNumber.begin(), phoneNumber.end(),
[](char character)
{
return std::isdigit(character);
}
);
}
/*
* Function: isEmailValid
* Description: Validates whether the given string is a properly formatted email address.
* Parameters:
* email - string containing the email address to validate
* Returns:
* bool - true if the email contains exactly one '@' character and is not at the start or end, false otherwise
*/
bool util::isEmailValid(const std::string& email)
{
size_t index = email.find('@');
if (index == std::string::npos)
{
return false;
}
if (email.find('@', index + 1) != std::string::npos)
{
return false;
}
if (index == 0 || index == email.size() - 1)
{
return false;
}
return true;
}
/*
* Function: isPasswordValid
* Description: Validates whether the given string meets password requirements.
* Parameters:
* password - string containing the password to validate
* Returns:
* bool - true if the password is valid, false otherwise
* Notes:
* - Must not equal the default password
* - Must be at least 8 characters long
* - Must contain at least one uppercase letter, one lowercase letter,
* one digit, and one special character
* - Must not contain whitespace
*/
bool util::isPasswordValid(const std::string& password)
{
if (password.length() < 8)
{
return false;
}
bool hasUpper = false;
bool hasLower = false;
bool hasDigit = false;
bool hasSpecial = false;
for (char character : password)
{
if (std::isspace(static_cast<unsigned char>(character)))
{
return false;
}
if (std::isupper(static_cast<unsigned char>(character)))
{
hasUpper = true;
}
else if (std::islower(static_cast<unsigned char>(character)))
{
hasLower = true;
}
else if (std::isdigit(static_cast<unsigned char>(character)))
{
hasDigit = true;
}
else
{
hasSpecial = true;
}
}
return hasUpper && hasLower && hasDigit && hasSpecial;
}
/*
* Function: isUsernameDuplicate
* Description: Checks if the given username already exists among active users.
* Parameters:
* username - string containing the username to validate
* usersMap - map of user objects keyed by identifier
* Returns:
* bool - true if the username is already in use by an active user, false otherwise
*/
bool util::isUsernameDuplicate(const std::string& username, const util::Map<std::string, User*>& usersMap)
{
int index = usersMap.findIf(
[&](const std::string&, User* user)
{
return (user->getUserName() == username);
}
);
return index != -1;
}
/*
* Function: isPhoneDuplicate
* Description: Checks if the given phone number already exists among active users.
* Parameters:
* phone - string containing the phone number to validate
* usersMap - map of user objects keyed by identifier
* Returns:
* bool - true if the phone number is already in use by an active user, false otherwise
* Notes:
* - Only considers users with state util::State::ACTIVE
*/
bool util::isPhoneDuplicate(const std::string& phone, const util::Map<std::string, User*>& usersMap)
{
int index = usersMap.findIf(
[&](const std::string&, User* user)
{
return (user->getPhone() == phone && user->getState() == util::State::ACTIVE);
}
);
return index != -1;
}
/*
* Function: isEmailDuplicate
* Description: Checks if the given email address already exists among active users.
* Parameters:
* email - string containing the email address to validate
* usersMap - map of user objects keyed by identifier
* Returns:
* bool - true if the email address is already in use by an active user, false otherwise
* Notes:
* - Only considers users with state util::State::ACTIVE
*/
bool util::isEmailDuplicate(const std::string& email, const util::Map<std::string, User*>& usersMap)
{
int index = usersMap.findIf(
[&](const std::string&, User* user)
{
return (user->getEmail() == email && user->getState() == util::State::ACTIVE);
}
);
return index != -1;
}