95 lines
2.6 KiB
C++
95 lines
2.6 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"
|
||
#include "Utility.h"
|
||
#include "DataStoreLockGuard.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)
|
||
{
|
||
DataStoreLockGuard lock(m_dataStore);
|
||
auto& trackedUserMap = m_dataStore.getUsers();
|
||
int trackedUserMapSize = trackedUserMap.getSize();
|
||
for (int index = 0; index < trackedUserMapSize; index++)
|
||
{
|
||
User* user = trackedUserMap.getValueAt(index).data;
|
||
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)
|
||
{
|
||
DataStoreLockGuard lock(m_dataStore);
|
||
auto& trackedUsersMap = m_dataStore.getUsers();
|
||
if (m_authenticatedUser == nullptr)
|
||
{
|
||
throw std::runtime_error("There is no user currently logged in!");
|
||
}
|
||
int index = trackedUsersMap.find(m_authenticatedUser->getId());
|
||
if (index == -1)
|
||
{
|
||
throw std::runtime_error("User does not exist!\n");
|
||
}
|
||
m_authenticatedUser->setPassword(newPassword);
|
||
trackedUsersMap.getValueAt(index).state = RecordState::MODIFIED;
|
||
m_dataStore.saveUsers();
|
||
}
|