83 lines
2.2 KiB
C++
83 lines
2.2 KiB
C++
/*
|
||
File: AuthenticationManagementService.cpp
|
||
Description: Implementation file containing the method definitions of the
|
||
AuthenticationManagementService class, including logout and
|
||
password change logic.
|
||
Author: Trenser
|
||
Date:19-May-2026
|
||
*/
|
||
|
||
#include <stdexcept>
|
||
#include "AuthenticationManagementService.h"
|
||
#include "User.h"
|
||
|
||
User* AuthenticationManagementService::m_authenticatedUser = nullptr;
|
||
|
||
/*
|
||
Function: login
|
||
Description: Authenticates a user by checking the provided username and password
|
||
against the stored users in the DataStore. If successful, sets the
|
||
authenticated user.
|
||
Parameter: const std::string& username - user’s username
|
||
const std::string& password - user’s password
|
||
Return type: bool - true if login successful, false otherwise
|
||
*/
|
||
bool AuthenticationManagementService::login(const std::string& username, const std::string& password)
|
||
{
|
||
util::Map<std::string, User*> users = m_dataStore.getUsers();
|
||
int usersMapSize = users.getSize();
|
||
for (int index = 0; index < usersMapSize; index++)
|
||
{
|
||
User* user = users.getValueAt(index);
|
||
if (username == user->getUserName())
|
||
{
|
||
if (password == user->getPassword())
|
||
{
|
||
m_authenticatedUser = user;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/*
|
||
Function: getAuthenticatedUser
|
||
Description: Retrieves the currently authenticated user.
|
||
Parameter: None
|
||
Return type: User* - pointer to the authenticated user
|
||
*/
|
||
User* AuthenticationManagementService::getAuthenticatedUser()
|
||
{
|
||
return m_authenticatedUser;
|
||
}
|
||
|
||
/*
|
||
Function: logout
|
||
Description: Logs out the currently authenticated user by clearing the
|
||
static authenticated user pointer.
|
||
Parameter: None
|
||
Return type: void
|
||
*/
|
||
void AuthenticationManagementService::logout()
|
||
{
|
||
m_authenticatedUser = nullptr;
|
||
}
|
||
|
||
/*
|
||
Function: changePassword
|
||
Description: Changes the password of the currently authenticated user.
|
||
Throws an exception if no user is logged in.
|
||
Parameter: const std::string& newPassword - new password to set
|
||
Return type: void
|
||
*/
|
||
void AuthenticationManagementService::changePassword(const std::string& newPassword)
|
||
{
|
||
if (m_authenticatedUser == nullptr)
|
||
{
|
||
throw std::runtime_error("There is no user currently logged in!");
|
||
}
|
||
m_authenticatedUser->setPassword(newPassword);
|
||
}
|