Skip to content

Commit

Permalink
Merge pull request #21 from hieu79115/main
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
mimhle authored Nov 30, 2023
2 parents 622a7f5 + 531d41a commit 113365c
Show file tree
Hide file tree
Showing 9 changed files with 357 additions and 165 deletions.
2 changes: 1 addition & 1 deletion LinkedList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void LinkedList<DataType>::selectionSort() {
}

template<class DataType>
DataType LinkedList<DataType>::operator[](int index) const {
DataType& LinkedList<DataType>::operator[](int index) const {
Node<DataType>* _p = _pHead;
for (int i = 0; i < index; i++)
_p = _p->_pNext;
Expand Down
2 changes: 1 addition & 1 deletion LinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class LinkedList {
* @details Overload operator []
* @return DataType
*/
DataType operator[](int index) const;
DataType& operator[](int index) const;
};

#endif // CTDL_GK_LINKEDLIST_CPP
Expand Down
73 changes: 39 additions & 34 deletions UserAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
#include "Users.h"
#include "UserData.h"


UserAction::UserAction(std::string role) {
_strRole = std::move(role);
_usersList.importUserData();
_usersList.importUserData();
}

UserAction::UserAction() {
_strRole = "";
_usersList.importUserData();
}

void UserAction::setRole(std::string role) {
_strRole = role;
_usersList.~Users();
_usersList.importUserData();
}

bool UserAction::addUser(const UserData& user) {
if (!(_strRole == "Admin" && _usersList.addUser(user))) return false;
CsvFile csvFileEmployee("Employees.txt");
Expand All @@ -35,9 +40,8 @@ bool UserAction::addUser(const UserData& user) {
return true;
}


bool UserAction::deleteUser(const std::string& userName) {
if (_strRole == "Admin") {
if (_strRole == "Admin" && _usersList.removeUser(userName)) {
CsvFile csvFileEmployee("Employees.txt");
std::vector<std::vector<std::string>> vtEmployeeAccounts = csvFileEmployee.read();
int iLine = -1;
Expand All @@ -51,50 +55,50 @@ bool UserAction::deleteUser(const std::string& userName) {
csvFileEmployee.remove(iLine);
CsvFile csvFileDelete(userName + ".txt");
csvFileDelete.del();
_usersList.removeUser(findUser(userName));
return true;
}
}
return false;
}


UserData UserAction::findUser(const std::string& userName) {
if (!(_strRole == "Admin")) return {};
if (!( _usersList.searchUser(userName)!=-1)) return {};
std::vector<UserData> vtUsersDataList = _usersList.listUsers();
int iUserNum = _usersList.searchUser(userName);
return vtUsersDataList[iUserNum];
}

void UserAction::updateUserInformation(const std::string& userName, const UserData& editedUser) {
_usersList.editUser(userName, editedUser);
CsvFile csvFileUser(userName + ".txt");
csvFileUser.write(
{
{editedUser.getName()},
{editedUser.getDateOfBirth()},
{editedUser.getAddress()},
{editedUser.getPhoneNumber()},
{editedUser.getEmail()}
}
);
bool UserAction::updateUserInformation(const std::string& userName, const UserData& user) {
if(!(_usersList.editUser(userName,user))) return false;
CsvFile csvFileEmployee("Employees.txt");
std::vector<std::vector<std::string>> vtEmployeeAccounts = csvFileEmployee.read();
int iLine = -1;
for (int i = 0; i < vtEmployeeAccounts.size(); i++) {
if (vtEmployeeAccounts[i][0] == editedUser.getName()) {
for (int i = 1; i <= vtEmployeeAccounts.size(); i++) {
if (vtEmployeeAccounts[i][0] == userName) {
iLine = i;
break;
}
}
if (iLine != 1) {
if (iLine != -1) {
csvFileEmployee.remove(iLine);
csvFileEmployee.append(
{
{editedUser.getUserName(), ",", editedUser.getPassword()}
{user.getUserName() + "," + user.getPassword()}
}
);
CsvFile csvFileUser(userName + ".txt");
csvFileUser.write(
{
{user.getName()},
{user.getDateOfBirth()},
{user.getAddress()},
{user.getPhoneNumber()},
{user.getEmail()}
}
);
return true;
}
return false;
}

std::string UserAction::getUserInformation(const std::string& userName) {
Expand Down Expand Up @@ -125,19 +129,20 @@ std::vector<std::string> UserAction::getAllUsersInformation() {
}

bool UserAction::authenticateUser(const std::string& userName, const std::string& password) {
std::vector<std::vector<std::string>> vtUserAccounts;
std::vector<std::vector<std::string>> vtAccountsUser;
if (_strRole == "Admin") {
CsvFile csvFileUsers("Administrators.txt");
vtUserAccounts = csvFileUsers.read();
} else {
CsvFile csvFileUsers("Employees.txt");
vtUserAccounts = csvFileUsers.read();
CsvFile csvFileAdmin("Administrators.txt");
vtAccountsUser = csvFileAdmin.read();
for (int i = 1; i < vtAccountsUser.size(); i++) {
if (vtAccountsUser[i][0] == userName && vtAccountsUser[i][1] == password) return true;
}
}
if (!vtUserAccounts.empty()) {
for (auto row: vtUserAccounts) {
if (row[0] == userName && row[1] == password) return true;
else {
CsvFile csvFileEmployee("Employees.txt");
vtAccountsUser = csvFileEmployee.read();
for (int i = 1; i < vtAccountsUser.size(); i++) {
if (vtAccountsUser[i][0] == userName && vtAccountsUser[i][1] == password) return true;
}
}
return false;
}

}
68 changes: 44 additions & 24 deletions UserAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,57 @@

/**
* @class UserAction
* @brief Class for user actions
* @details This class provides functionality for performing various user-related actions,
* such as adding, deleting, finding, editing, and displaying user information.
* @brief Manages actions related to user data and authentication
*
* This class allows creating, reading, updating, and deleting user records
* from a user list. It also handles user authentication. Methods are
* provided for adding new users, finding existing users, updating user
* information, displaying user data, and validating username/password.
*
* Access to full user data is restricted based on the user role set for
* the class instance. The default role has read-only access. The "admin"
* role allows full access to all methods.
*/
class UserAction {
private:
Users _usersList;
std::string _strRole;

public:
/**
* @brief Constructor with role parameter
* @param role User role (default is empty)
*/
explicit UserAction(std::string role = "");

/**
* @brief Constructor
* Creates an empty UserAction instance with default empty role.
* @param role User role to assign ("employee" or "admin")
*/
explicit UserAction(std::string role = "");

/**
* @brief Default constructor
*/
UserAction();

/**
* @brief Add a new user
* @param user UserData object representing the user
*/
bool addUser(const UserData& user);
/**
* @brief Set user role
* @param role User role to assign ("employee" or "admin")
*/
void setRole(std::string role);

/**
* @brief Delete a user.
* @param userName Username of the user to delete
*/
bool deleteUser(const std::string& userName);
/**
* @brief Add new user record
* Adds a provided UserData object as a new user record in the user list.
* Accessible only if the instance role is "admin".
* @param user UserData object with new user data
* @return true if the user was added successfully, false otherwise
*/
bool addUser(const UserData& user);

/**
* @brief Delete a user
* @param userName Username of the user to be deleted
* @return true if the user was deleted successfully, false otherwise
*/
bool deleteUser(const std::string& userName);

/**
* @brief Find a user by username
Expand All @@ -46,12 +65,13 @@ class UserAction {
*/
UserData findUser(const std::string& userName);

/**
* @brief Edit user information
* @param userName Username of the user to edit
* @param editedUser UserData object representing the edited user information
*/
void updateUserInformation(const std::string& userName, const UserData& editedUser);
/**
* @brief Edit user information
* @param userName Username of the user to edit
* @param editedUser UserData object representing the edited user information
* @return true if the user information was edited successfully, false otherwise
*/
bool updateUserInformation(const std::string& userName, const UserData& user);

/**
* @brief Display user information
Expand Down
17 changes: 16 additions & 1 deletion UserData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,19 @@ std::string UserData::getRole() const { return _strRole; }

bool UserData::operator!=(const UserData& user) const {
return _strUserName != user._strUserName || _strPassword != user._strPassword || _strRole != user._strRole || _strName != user._strName || _strDateOfBirth != user._strDateOfBirth || _iAge != user._iAge || _strAddress != user._strAddress || _strPhoneNumber != user._strPhoneNumber || _strEmail != user._strEmail;
}
}

UserData& UserData::operator=(const UserData& user) {
if (this != &user) {
_strName = user._strName;
_strDateOfBirth = user._strDateOfBirth;
_iAge = user._iAge;
_strAddress = user._strAddress;
_strPhoneNumber = user._strPhoneNumber;
_strEmail = user._strEmail;
_strUserName = user._strUserName;
_strPassword = user._strPassword;
_strRole = user._strRole;
}
return *this;
}
7 changes: 7 additions & 0 deletions UserData.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ class UserData {
* @return True if the two users are not equal, false otherwise
*/
bool operator!=(const UserData& user) const;

/**
* @brief Overloads the = operator
* @details This function overloads the = operator to assign the value of a user to another.
* @return The user with the new value
*/
UserData& operator=(const UserData& user);
};

#endif // CTDL_GK_USERDATA_CPP
Expand Down
6 changes: 3 additions & 3 deletions Users.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ bool Users::addUser(const UserData& user) {
return true;
}

bool Users::removeUser(const UserData& user) {
bool Users::removeUser(const std::string& userName) {
for (int i = 0; i < _list.getSize(); i++) {
if (_list[i].getUserName() == user.getUserName()) {
_list.remove(user);
if (_list[i].getUserName() == userName) {
_list.remove(_list[i]);
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Users.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class Users {

/**
* @brief Remove user from list
* @param user User to remove
* @param userName Username of user to remove
* @return True if success, false if failed
*/
bool removeUser(const UserData& user);
bool removeUser(const std::string& userName);

/**
* @brief Edit user in list
Expand Down
Loading

0 comments on commit 113365c

Please sign in to comment.