PrivacyManager
in package
Privacy Manager Model
Handles all privacy-related database operations including account blocking, password change tokens, email change verification, and security tracking. Manages the security aspects of user account modifications.
Tags
Table of Contents
Constants
- BLOCK_DURATION_MINUTES = 30
- Block duration in minutes
- MAX_FAILED_ATTEMPTS = 10
- Maximum failed attempts before blocking
- MAX_RESEND_ATTEMPTS = 5
- Maximum code resend attempts
- RESEND_COOLDOWN_SECONDS = 60
- Resend code cooldown in seconds
Properties
- $pdo : PDO
- PDO database connection instance
Methods
- __construct() : void
- Constructor - Initialize the PrivacyManager
- blockUser() : bool
- Block user account for privacy modifications
- canResendCode() : array{can_resend: bool, wait_seconds: int, resend_count: int}
- Check if user can resend verification code
- canResendEmailCode() : array{can_resend: bool, wait_seconds: int, resend_count: int}
- Check if user can resend email verification code
- canResendPasswordCode() : array{can_resend: bool, wait_seconds: int, resend_count: int}
- Check if user can resend password verification code
- createEmailChangeToken() : bool
- Create an email change verification token
- createPasswordChangeToken() : bool
- Create a password change verification token
- deleteEmailChangeToken() : bool
- Delete email change token
- deletePasswordChangeToken() : bool
- Delete password change token
- generateVerificationCode() : string
- Generate a secure 6-digit verification code
- getEmailChangeAttempts() : int
- Get email change attempts count
- getMaxFailedAttempts() : int
- Get max failed attempts constant
- getMaxResendAttempts() : int
- Get max resend attempts constant
- getRemainingBlockTime() : int
- Get remaining block time in minutes
- getResendCooldown() : int
- Get resend cooldown constant
- isMaxEmailChangeAttemptsReached() : bool
- Check if max email change attempts reached
- isUserBlocked() : bool
- Check if user account is blocked for privacy modifications
- isValidEmailFormat() : bool
- Validate email format
- resendCode() : bool
- Update token for resend
- resendEmailCode() : bool
- Resend email verification code
- resendPasswordCode() : bool
- Resend password verification code
- resetEmailChangeAttempts() : bool
- Reset email change attempts counter
- trackEmailChangeAttempt() : int
- Track email change attempt
- unblockUser() : bool
- Unblock user account
- validatePasswordFormat() : array{valid: bool, errors: string[]}
- Validate password format
- verifyEmailChangeToken() : array{valid: bool, message: string, attempts: int}
- Verify email change token
- verifyPasswordChangeToken() : array{valid: bool, message: string, attempts: int}
- Verify password change token
Constants
BLOCK_DURATION_MINUTES
Block duration in minutes
private
int
BLOCK_DURATION_MINUTES
= 30
MAX_FAILED_ATTEMPTS
Maximum failed attempts before blocking
private
int
MAX_FAILED_ATTEMPTS
= 10
MAX_RESEND_ATTEMPTS
Maximum code resend attempts
private
int
MAX_RESEND_ATTEMPTS
= 5
RESEND_COOLDOWN_SECONDS
Resend code cooldown in seconds
private
int
RESEND_COOLDOWN_SECONDS
= 60
Properties
$pdo
PDO database connection instance
private
PDO
$pdo
Methods
__construct()
Constructor - Initialize the PrivacyManager
public
__construct() : void
Retrieves the database connection from the Database singleton.
blockUser()
Block user account for privacy modifications
public
blockUser(int $userId) : bool
Sets the is_blocked flag and blocked_until timestamp for the user.
Parameters
- $userId : int
-
The user ID to block
Return values
bool —True if successful, false otherwise
canResendCode()
Check if user can resend verification code
public
canResendCode(int $userId) : array{can_resend: bool, wait_seconds: int, resend_count: int}
Verifies if the cooldown period has passed and max resends not reached. Uses SQL TIMESTAMPDIFF for consistent timezone handling.
Parameters
- $userId : int
-
The user ID
Return values
array{can_resend: bool, wait_seconds: int, resend_count: int} —Resend status
canResendEmailCode()
Check if user can resend email verification code
public
canResendEmailCode(int $userId) : array{can_resend: bool, wait_seconds: int, resend_count: int}
Verifies if the cooldown period has passed and max resends not reached. Uses SQL TIMESTAMPDIFF for consistent timezone handling.
Parameters
- $userId : int
-
The user ID
Return values
array{can_resend: bool, wait_seconds: int, resend_count: int} —Resend status
canResendPasswordCode()
Check if user can resend password verification code
public
canResendPasswordCode(int $userId) : array{can_resend: bool, wait_seconds: int, resend_count: int}
Alias for canResendCode, specifically for password changes.
Parameters
- $userId : int
-
The user ID
Return values
array{can_resend: bool, wait_seconds: int, resend_count: int} —Resend status
createEmailChangeToken()
Create an email change verification token
public
createEmailChangeToken(int $userId, string $code, string $newEmail) : bool
Stores a 6-digit verification code for email change confirmation. Uses the PASSWORD_RESET_TOKEN table with a special marker. The code expires after 10 minutes.
Parameters
- $userId : int
-
The user ID
- $code : string
-
The 6-digit verification code
- $newEmail : string
-
The new email address to store
Return values
bool —True if successful, false otherwise
createPasswordChangeToken()
Create a password change verification token
public
createPasswordChangeToken(int $userId, string $code) : bool
Stores a 6-digit verification code for password change confirmation. Uses the existing PASSWORD_RESET_TOKEN table. The code expires after 10 minutes.
Parameters
- $userId : int
-
The user ID
- $code : string
-
The 6-digit verification code
Return values
bool —True if successful, false otherwise
deleteEmailChangeToken()
Delete email change token
public
deleteEmailChangeToken(int $userId) : bool
Removes the verification token after successful verification or max attempts.
Parameters
- $userId : int
-
The user ID
Return values
bool —True if successful, false otherwise
deletePasswordChangeToken()
Delete password change token
public
deletePasswordChangeToken(int $userId) : bool
Removes the verification token after successful verification or max attempts.
Parameters
- $userId : int
-
The user ID
Return values
bool —True if successful, false otherwise
generateVerificationCode()
Generate a secure 6-digit verification code
public
generateVerificationCode() : string
Creates a cryptographically secure random 6-digit code.
Return values
string —The 6-digit verification code
getEmailChangeAttempts()
Get email change attempts count
public
getEmailChangeAttempts(int $userId) : int
Returns the current number of failed email change attempts.
Parameters
- $userId : int
-
The user ID
Return values
int —Current attempt count
getMaxFailedAttempts()
Get max failed attempts constant
public
getMaxFailedAttempts() : int
Return values
int —Maximum failed attempts allowed
getMaxResendAttempts()
Get max resend attempts constant
public
getMaxResendAttempts() : int
Return values
int —Maximum resend attempts allowed
getRemainingBlockTime()
Get remaining block time in minutes
public
getRemainingBlockTime(int $userId) : int
Returns the number of minutes remaining until the account is unblocked.
Parameters
- $userId : int
-
The user ID to check
Return values
int —Minutes remaining, 0 if not blocked
getResendCooldown()
Get resend cooldown constant
public
getResendCooldown() : int
Return values
int —Cooldown seconds between resends
isMaxEmailChangeAttemptsReached()
Check if max email change attempts reached
public
isMaxEmailChangeAttemptsReached(int $userId) : bool
Parameters
- $userId : int
-
The user ID
Return values
bool —True if max attempts reached, false otherwise
isUserBlocked()
Check if user account is blocked for privacy modifications
public
isUserBlocked(int $userId) : bool
Verifies if the user's account is currently blocked and if the block period has expired. Automatically unblocks if time has passed.
Parameters
- $userId : int
-
The user ID to check
Return values
bool —True if blocked, false otherwise
isValidEmailFormat()
Validate email format
public
isValidEmailFormat(string $email) : bool
Checks if the provided email has a valid format.
Parameters
- $email : string
-
The email to validate
Return values
bool —True if valid format, false otherwise
resendCode()
Update token for resend
public
resendCode(int $userId, string $newCode) : bool
Updates the verification code and increments resend counter. Uses MySQL functions for consistent timezone handling.
Parameters
- $userId : int
-
The user ID
- $newCode : string
-
The new verification code
Return values
bool —True if successful, false otherwise
resendEmailCode()
Resend email verification code
public
resendEmailCode(int $userId, string $newCode) : bool
Updates the verification code and increments resend counter. Preserves the pending email address. Uses MySQL functions for consistent timezone handling.
Parameters
- $userId : int
-
The user ID
- $newCode : string
-
The new verification code
Return values
bool —True if successful, false otherwise
resendPasswordCode()
Resend password verification code
public
resendPasswordCode(int $userId, string $newCode) : bool
Alias for resendCode, specifically for password changes.
Parameters
- $userId : int
-
The user ID
- $newCode : string
-
The new verification code
Return values
bool —True if successful, false otherwise
resetEmailChangeAttempts()
Reset email change attempts counter
public
resetEmailChangeAttempts(int $userId) : bool
Resets the failed attempt counter after successful email change.
Parameters
- $userId : int
-
The user ID
Return values
bool —True if successful, false otherwise
trackEmailChangeAttempt()
Track email change attempt
public
trackEmailChangeAttempt(int $userId) : int
Increments the failed attempt counter for email changes. Returns the current attempt count.
Parameters
- $userId : int
-
The user ID
Return values
int —Current attempt count
unblockUser()
Unblock user account
public
unblockUser(int $userId) : bool
Removes the block from the user's account.
Parameters
- $userId : int
-
The user ID to unblock
Return values
bool —True if successful, false otherwise
validatePasswordFormat()
Validate password format
public
validatePasswordFormat(string $password) : array{valid: bool, errors: string[]}
Checks if the password meets requirements:
- Minimum 10 characters
- At least 1 uppercase letter
- At least 1 digit
- At least 1 special character
Parameters
- $password : string
-
The password to validate
Return values
array{valid: bool, errors: string[]} —Validation result with errors
verifyEmailChangeToken()
Verify email change token
public
verifyEmailChangeToken(int $userId, string $code) : array{valid: bool, message: string, attempts: int}
Validates the 6-digit code and increments attempt counter. Returns status indicating if code is valid, expired, or max attempts reached. Uses MySQL for expiration check for consistent timezone handling.
Parameters
- $userId : int
-
The user ID
- $code : string
-
The verification code to check
Return values
array{valid: bool, message: string, attempts: int} —Verification result
verifyPasswordChangeToken()
Verify password change token
public
verifyPasswordChangeToken(int $userId, string $code) : array{valid: bool, message: string, attempts: int}
Validates the 6-digit code and increments attempt counter. Returns status indicating if code is valid, expired, or max attempts reached. Uses MySQL for expiration check for consistent timezone handling.
Parameters
- $userId : int
-
The user ID
- $code : string
-
The verification code to check
Return values
array{valid: bool, message: string, attempts: int} —Verification result