Implement Service Refactoring
<UserStory> 1952: Service Refactoring </UserStory> UserStory #1952 <Changes> 1. Enhanced DataStore::getUsers to load SerializedUser records, refresh cache, and attach notifications to recipient users with validation for recipient IDs. 2. Updated DataStore::saveUsers to persist SerializedUser records and save notifications alongside user data. 3. Refactored AuthenticationManagementService::login to use DataStoreLockGuard and tracked user map with SerializedUser-backed records. 4. Modified AuthenticationManagementService::changePassword to ensure thread-safe updates, mark user record as MODIFIED, and persist changes via DataStore::saveUsers. 5. Added dependencies for Utility.h and DataStoreLockGuard.h in AuthenticationManagementService.cpp to support safe record handling. </Changes> <Test> N/A </Test> <Review> Sreeja Reghukumar </Review>
This commit is contained in:
+1
@@ -157,6 +157,7 @@
|
||||
<ClInclude Include="core\patterns\Observer.h" />
|
||||
<ClInclude Include="core\patterns\Subject.h" />
|
||||
<ClInclude Include="datastores\DataStore.h" />
|
||||
<ClInclude Include="datastores\DataStoreLockGuard.h" />
|
||||
<ClInclude Include="datastores\sharedmemory\FileHeader.h" />
|
||||
<ClInclude Include="datastores\sharedmemory\MappingInfo.h" />
|
||||
<ClInclude Include="datastores\sharedmemory\RecordState.h" />
|
||||
|
||||
+3
@@ -278,5 +278,8 @@
|
||||
<ClInclude Include="datastores\sharedmemory\SharedMemory.h">
|
||||
<Filter>Header Files\DataStores\SharedMemory</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="datastores\DataStoreLockGuard.h">
|
||||
<Filter>Header Files\DataStores</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -228,6 +228,22 @@ Returns:
|
||||
*/
|
||||
util::Map<std::string, TrackedRecord<User>>& DataStore::getUsers()
|
||||
{
|
||||
auto users = loadRecords<User, SerializedUser>(m_users);
|
||||
refreshCache(m_userCache, users);
|
||||
auto& notifications = getNotifications();
|
||||
int numberOfNotifications = m_notificationCache.getSize();
|
||||
for (int index = 0; index < numberOfNotifications; index++)
|
||||
{
|
||||
Notification* notification = notifications.getValueAt(index).data;
|
||||
const std::string& recipientUserId = notification->getRecipientUserId();
|
||||
int userIndex = m_userCache.find(recipientUserId);
|
||||
if (userIndex == -1)
|
||||
{
|
||||
throw std::runtime_error("Invalid recipient user ID");
|
||||
}
|
||||
User* user = m_userCache.getValueAt(userIndex).data;
|
||||
user->addNotification(notification);
|
||||
}
|
||||
return m_userCache;
|
||||
}
|
||||
|
||||
@@ -371,6 +387,8 @@ Returns:
|
||||
*/
|
||||
void DataStore::saveUsers()
|
||||
{
|
||||
saveRecords<User, SerializedUser>(m_users, m_userCache);
|
||||
saveNotifications();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
File: DataStoreLockGuard.h
|
||||
Description: Defines the DataStoreLockGuard class used to manage DataStore
|
||||
locking and unlocking automatically within a scope.
|
||||
Author: Trenser
|
||||
Date: 12-June-2026
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "DataStore.h"
|
||||
|
||||
class DataStoreLockGuard
|
||||
{
|
||||
public:
|
||||
explicit DataStoreLockGuard(DataStore& dataStore)
|
||||
: m_dataStore(dataStore)
|
||||
{
|
||||
m_dataStore.lockDataStore();
|
||||
}
|
||||
~DataStoreLockGuard()
|
||||
{
|
||||
m_dataStore.unlockDataStore();
|
||||
}
|
||||
DataStoreLockGuard(const DataStoreLockGuard&) = delete;
|
||||
DataStoreLockGuard& operator=(const DataStoreLockGuard&) = delete;
|
||||
private:
|
||||
DataStore& m_dataStore;
|
||||
};
|
||||
+16
-4
@@ -10,6 +10,8 @@ Date:19-May-2026
|
||||
#include <stdexcept>
|
||||
#include "AuthenticationManagementService.h"
|
||||
#include "User.h"
|
||||
#include "Utility.h"
|
||||
#include "DataStoreLockGuard.h"
|
||||
|
||||
User* AuthenticationManagementService::m_authenticatedUser = nullptr;
|
||||
|
||||
@@ -24,11 +26,12 @@ 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++)
|
||||
DataStoreLockGuard lock(m_dataStore);
|
||||
auto& trackedUserMap = m_dataStore.getUsers();
|
||||
int trackedUserMapSize = trackedUserMap.getSize();
|
||||
for (int index = 0; index < trackedUserMapSize; index++)
|
||||
{
|
||||
User* user = users.getValueAt(index);
|
||||
User* user = trackedUserMap.getValueAt(index).data;
|
||||
if (username == user->getUserName())
|
||||
{
|
||||
if (password == user->getPassword())
|
||||
@@ -74,9 +77,18 @@ Return type: void
|
||||
*/
|
||||
void AuthenticationManagementService::changePassword(const std::string& newPassword)
|
||||
{
|
||||
DataStoreLockGuard lock(m_dataStore);
|
||||
auto& trackedUsersMap = m_dataStore.getUsers();
|
||||
int index = trackedUsersMap.find(m_authenticatedUser->getId());
|
||||
if (index == -1)
|
||||
{
|
||||
throw std::runtime_error("User does not exist!\n");
|
||||
}
|
||||
if (m_authenticatedUser == nullptr)
|
||||
{
|
||||
throw std::runtime_error("There is no user currently logged in!");
|
||||
}
|
||||
m_authenticatedUser->setPassword(newPassword);
|
||||
trackedUsersMap.getValueAt(index).state = RecordState::MODIFIED;
|
||||
m_dataStore.saveUsers();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user