Skip to content

Commit

Permalink
Added ability to change password
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLazarescu committed Aug 20, 2023
1 parent 687844c commit 07091a4
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 36 deletions.
8 changes: 8 additions & 0 deletions src/adapters/controllers/user_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ UserController::UserController(application::IUserService* userService) :
connect(m_userService, &application::IUserService::profilePictureChanged,
this, &UserController::profilePictureChanged);

connect(m_userService, &application::IUserService::passwordChangeFinished,
this, &UserController::passwordChangeFinished);


// Tag insertion
connect(m_userService, &application::IUserService::tagInsertionStarted,
Expand Down Expand Up @@ -56,6 +59,11 @@ void UserController::syncWithServer()
m_userService->downloadUser();
}

void UserController::changePassword(const QString& newPassword)
{
m_userService->changePassword(newPassword);
}

QString UserController::getTagUuidForName(QString name)
{
// Tags are always capitalized, make sure to search for the correct one
Expand Down
1 change: 1 addition & 0 deletions src/adapters/controllers/user_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ADAPTERS_LIBRARY UserController : public IUserController
void loadUser(bool rememberUser) override;
void deleteUser() override;
void syncWithServer() override;
void changePassword(const QString& newPassword) override;

QString getTagUuidForName(QString name) override;
QString addTag(const QString& name) override;
Expand Down
9 changes: 9 additions & 0 deletions src/adapters/gateways/user_storage_gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ UserStorageGateway::UserStorageGateway(IUserStorageAccess* userStorageAccess) :

connect(m_userStorageAccess, &IUserStorageAccess::profilePictureReady, this,
&UserStorageGateway::profilePictureReady);

connect(m_userStorageAccess, &IUserStorageAccess::passwordChangeFinished,
this, &UserStorageGateway::passwordChangeFinished);
}

void UserStorageGateway::getUser(const QString& authToken)
Expand Down Expand Up @@ -54,6 +57,12 @@ void UserStorageGateway::changeEmail(const QString& authToken,
m_userStorageAccess->changeEmail(authToken, newEmail);
}

void UserStorageGateway::changePassword(const QString& authToken,
const QString& newPassword)
{
m_userStorageAccess->changePassword(authToken, newPassword);
}

void UserStorageGateway::changeProfilePicture(const QString& authToken,
const QString& path)
{
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/gateways/user_storage_gateway.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class ADAPTERS_LIBRARY UserStorageGateway
const QString& newLastName) override;
void changeEmail(const QString& authToken,
const QString& newEmail) override;
void changePassword(const QString& authToken,
const QString& newPassword) override;
void changeProfilePicture(const QString& authToken,
const QString& path) override;
void deleteProfilePicture(const QString& authToken) override;
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/interfaces/controllers/i_user_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class IUserController : public QObject
Q_INVOKABLE virtual void deleteUser() = 0;
Q_INVOKABLE virtual void syncWithServer() = 0;
Q_INVOKABLE virtual void deleteProfilePicture() = 0;
Q_INVOKABLE virtual void changePassword(const QString& newPassword) = 0;

Q_INVOKABLE virtual QString getTagUuidForName(QString name) = 0;
Q_INVOKABLE virtual QString addTag(const QString& name) = 0;
Expand Down Expand Up @@ -67,6 +68,7 @@ class IUserController : public QObject
void usedBookStorageChanged();
void bookStorageLimitChanged();
void profilePictureChanged();
void passwordChangeFinished(bool success, const QString& reason);
};

} // namespace adapters
3 changes: 3 additions & 0 deletions src/adapters/interfaces/persistance/i_user_storage_access.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class ADAPTERS_LIBRARY IUserStorageAccess : public QObject
const QString& newLastName) = 0;
virtual void changeEmail(const QString& authToken,
const QString& newEmail) = 0;
virtual void changePassword(const QString& authToken,
const QString& newPassword) = 0;
virtual void changeProfilePicture(const QString& authToken,
const QString& path) = 0;
virtual void deleteProfilePicture(const QString& authToken) = 0;
Expand All @@ -43,6 +45,7 @@ class ADAPTERS_LIBRARY IUserStorageAccess : public QObject
void userReady(const QByteArray& data);
void gettingUserFailed();
void profilePictureReady(QByteArray& data);
void passwordChangeFinished(bool success, const QString& reason);
};

} // namespace adapters
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class APPLICATION_LIBRARY IUserStorageGateway : public QObject
const QString& newLastName) = 0;
virtual void changeEmail(const QString& authToken,
const QString& newEmail) = 0;
virtual void changePassword(const QString& authToken,
const QString& newPassword) = 0;
virtual void changeProfilePicture(const QString& authToken,
const QString& path) = 0;
virtual void deleteProfilePicture(const QString& authToken) = 0;
Expand All @@ -43,6 +45,7 @@ class APPLICATION_LIBRARY IUserStorageGateway : public QObject
signals:
void finishedGettingUser(const domain::entities::User& user, bool success);
void profilePictureReady(QByteArray& data);
void passwordChangeFinished(bool success, const QString& reason);
};

} // namespace application
2 changes: 2 additions & 0 deletions src/application/interfaces/services/i_user_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class APPLICATION_LIBRARY IUserService : public QObject
virtual void setProfilePicturePath(const QString& path) = 0;

virtual void deleteProfilePicture() = 0;
virtual void changePassword(const QString& newPassword) = 0;

virtual const std::vector<domain::entities::Tag>& getTags() const = 0;
virtual QUuid addTag(const domain::entities::Tag& tag) = 0;
Expand All @@ -56,6 +57,7 @@ class APPLICATION_LIBRARY IUserService : public QObject
void tagDeletionEnded();
void tagsChanged(int index);
void bookStorageDataUpdated(long usedStorage, long bookStorageLimit);
void passwordChangeFinished(bool success, const QString& reason);

public slots:
virtual void setupUserData(const QString& token, const QString& email) = 0;
Expand Down
9 changes: 9 additions & 0 deletions src/application/services/user_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ UserService::UserService(IUserStorageGateway* userStorageGateway) :
updateProfilePictureUI(path);
});

connect(m_userStorageGateway, &IUserStorageGateway::passwordChangeFinished,
this, &UserService::passwordChangeFinished);


// Tag insertion
connect(&m_user, &User::tagInsertionStarted, this,
&UserService::tagInsertionStarted);
Expand Down Expand Up @@ -164,6 +168,11 @@ void UserService::deleteProfilePicture()
m_userStorageGateway->changeHasProfilePicture(m_authenticationToken, false);
}

void UserService::changePassword(const QString& newPassword)
{
m_userStorageGateway->changePassword(m_authenticationToken, newPassword);
}

QString UserService::saveProfilePictureToFile(QByteArray& data)
{
auto userDir = getUserProfileDir();
Expand Down
1 change: 1 addition & 0 deletions src/application/services/user_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class APPLICATION_LIBRARY UserService : public IUserService
void setProfilePicturePath(const QString& path) override;

void deleteProfilePicture() override;
void changePassword(const QString& newPassword) override;

const std::vector<domain::entities::Tag>& getTags() const override;
QUuid addTag(const domain::entities::Tag& tag) override;
Expand Down
1 change: 1 addition & 0 deletions src/infrastructure/data/endpoints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ inline const QString getUsedBookStorageEndpoint { domain +
inline const QString userGetEndpoint { domain + "/api/user" };
inline const QString userPatchEndpoint { domain + "/api/user" };
inline const QString userDeleteEndpoint { domain + "/api/user" };
inline const QString userChangePasswordEndpoint { domain + "/api/user" };
inline const QString userProfilePictureEndpoint { domain +
"/api/user/profilePicture" };

Expand Down
34 changes: 34 additions & 0 deletions src/infrastructure/persistance/user_storage_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <QVariant>
#include "api_error_helper.hpp"
#include "endpoints.hpp"
#include "qjsondocument.h"
#include "qjsonobject.h"

namespace infrastructure::persistence
{
Expand Down Expand Up @@ -141,6 +143,38 @@ void UserStorageAccess::changeEmail(const QString& authToken,
// TODO: Implement
}

void UserStorageAccess::changePassword(const QString& authToken,
const QString& newPassword)
{
auto request = createRequest(data::userChangePasswordEndpoint, authToken);
QJsonObject jsonObject;
jsonObject["Input"] = newPassword;
auto data = QJsonDocument(jsonObject).toJson();

auto reply = m_networkAccessManager.post(request, data);


// Make sure to release the reply's memory
connect(reply, &QNetworkReply::finished, this,
[this, reply]()
{
if(api_error_helper::apiRequestFailed(reply, 200))
{
auto jsonReply =
QJsonDocument::fromJson(reply->readAll()).object();
auto message = jsonReply["message"].toString();
qWarning() << "Changing password failed: " << message;

emit passwordChangeFinished(false, message);
reply->deleteLater();
return;
}

emit passwordChangeFinished(true, "");
reply->deleteLater();
});
}

void UserStorageAccess::changeProfilePicture(const QString& authToken,
const QString& path)
{
Expand Down
2 changes: 2 additions & 0 deletions src/infrastructure/persistance/user_storage_access.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class UserStorageAccess : public adapters::IUserStorageAccess
const QString& newLastName) override;
void changeEmail(const QString& authToken,
const QString& newEmail) override;
void changePassword(const QString& authToken,
const QString& newPassword) override;
void changeProfilePicture(const QString& authToken,
const QString& path) override;
void deleteProfilePicture(const QString& authToken) override;
Expand Down
57 changes: 54 additions & 3 deletions src/presentation/settings/accountPage/MAccountPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ MFlickWrapper {
width: 10
policy: "AlwaysOn"
}

Connections
{
target: UserController

function onPasswordChangeFinished(success, reason)
{
if(success)
{
passwordInput.text = ""
passwordConfirmationInput.text = ""
}
else
{
passwordInput.errorText = reason;
passwordInput.setError();
}
}
}

Page {
id: page
Expand Down Expand Up @@ -236,7 +255,14 @@ MFlickWrapper {
image: Icons.eyeOn
toggledImage: Icons.eyeOff

onEdited: internal.unsavedChanges = true
onEdited:
{
internal.unsavedChanges = true

// Clear error for password confirmation input as well
passwordConfirmationInput.errorText = "";
passwordConfirmationInput.clearError();
}
}

MLabeledInputBox {
Expand All @@ -251,7 +277,13 @@ MFlickWrapper {
image: Icons.eyeOn
toggledImage: Icons.eyeOff

onEdited: internal.unsavedChanges = true
onEdited: {
internal.unsavedChanges = true

// Clear error for password input as well
passwordInput.errorText = "";
passwordInput.clearError();
}
}
}
}
Expand Down Expand Up @@ -447,11 +479,30 @@ MFlickWrapper {
id: internal
property bool unsavedChanges: false
property int boxPadding: 40

function changePassword()
{
if(passwordInput.text === passwordConfirmationInput.text)
{
UserController.changePassword(passwordInput.text)
}
else
{
passwordInput.errorText = "Passwords don't match!";
passwordInput.setError();

passwordConfirmationInput.errorText = "Passwords don't match!";
passwordConfirmationInput.setError();
}
}
}

function saveAccountSettings() {
UserController.firstName = firstNameInput.text
UserController.lastName = lastNameInput.text

if(passwordInput.text !== "")
internal.changePassword();

if (profilePictureArea.currentImage == "!")
UserController.deleteProfilePicture()
Expand All @@ -461,4 +512,4 @@ MFlickWrapper {
profilePictureArea.currentImage = ""
internal.unsavedChanges = false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class UserServiceMock : public application::IUserService
MOCK_METHOD(void, setLastName, (const QString&), (override));
MOCK_METHOD(QString, getEmail, (), (const, override));
MOCK_METHOD(void, setEmail, (const QString&), (override));
MOCK_METHOD(void, changePassword, (const QString&), (override));
MOCK_METHOD(long, getUsedBookStorage, (), (const, override));
MOCK_METHOD(long, getBookStorageLimit, (), (const, override));
MOCK_METHOD(QString, getProfilePicturePath, (), (const, override));
Expand Down
27 changes: 11 additions & 16 deletions tests/adapters_unit_tests/gateways/user_storage_gateway_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,23 @@ namespace tests::adapters
class UserStorageAccessMock : public IUserStorageAccess
{
public:
MOCK_METHOD(void, getUser, (const QString& authToken), (override));
MOCK_METHOD(void, deleteUser, (const QString& authToken), (override));
MOCK_METHOD(void, getProfilePicture, (const QString& authToken),
MOCK_METHOD(void, getUser, (const QString&), (override));
MOCK_METHOD(void, deleteUser, (const QString&), (override));
MOCK_METHOD(void, getProfilePicture, (const QString&), (override));
MOCK_METHOD(void, changeFirstName, (const QString&, const QString&),
(override));
MOCK_METHOD(void, changeFirstName,
(const QString& authToken, const QString& newFirstName),
MOCK_METHOD(void, changeLastName, (const QString&, const QString&),
(override));
MOCK_METHOD(void, changeLastName,
(const QString& authToken, const QString& newFirstName),
MOCK_METHOD(void, changeEmail, (const QString&, const QString&),
(override));
MOCK_METHOD(void, changeEmail,
(const QString& authToken, const QString& newFirstName),
MOCK_METHOD(void, changePassword, (const QString&, const QString&),
(override));
MOCK_METHOD(void, changeProfilePicture,
(const QString& authToken, const QString& path), (override));
MOCK_METHOD(void, deleteProfilePicture, (const QString& authToken),
MOCK_METHOD(void, changeProfilePicture, (const QString&, const QString&),
(override));
MOCK_METHOD(void, deleteProfilePicture, (const QString&), (override));
MOCK_METHOD(void, changeProfilePictureLastUpdated,
(const QString& authToken, const QString& newDateTime),
(override));
MOCK_METHOD(void, changeHasProfilePicture,
(const QString& authToken, const QString& newValue),
(const QString&, const QString&), (override));
MOCK_METHOD(void, changeHasProfilePicture, (const QString&, const QString&),
(override));
MOCK_METHOD(void, deleteTag, (const QString&, const QString&), (override));
MOCK_METHOD(void, renameTag, (const QString&, const QJsonObject&),
Expand Down
Loading

0 comments on commit 07091a4

Please sign in to comment.