diff options
| author | Nikita Kostovsky <nikita@kostovsky.me> | 2025-06-23 22:03:49 +0200 |
|---|---|---|
| committer | Nikita Kostovsky <nikita@kostovsky.me> | 2025-06-23 22:03:49 +0200 |
| commit | 0915fc1494df1cd15fc9c09bbf622f137406c84c (patch) | |
| tree | ef81d75f0e62143b1d072f6fd911eed9c5d2230f /src/user.h | |
| parent | c9fcceb74d861525b2defec8219374edb9c1455a (diff) | |
add User(login) constructor, fetch db data inside
Diffstat (limited to 'src/user.h')
| -rw-r--r-- | src/user.h | 47 |
1 files changed, 39 insertions, 8 deletions
@@ -14,22 +14,53 @@ public: /*! * \brief User - create user with given `login` and `password`. `salt` and * `passwordHash` will be generated and stored instead of real `password`. - * Will *not* be committed to db automatically. + * Will *not* be committed to db automatically. Should be used to create a + * new user + * \code + * User user{"admin", "123"}; + * auto id = user.createInDb(); + * if (id == rsshit::db::IdNotFound) // error + * ... * \param login - max len is 32 * \param password - max len is 32 */ explicit User(const QString &login, const QString &password); + /*! + * \brief User - create user with given `login` and try to fetch data from db + * automatically. Should be used for existing users + * \code + * User user{"admin"}; + * if (user.getDbId() == rsshit::db::IdNotFound) // user not found + * ... + * if (!user.verifyPassword("123)) // wrong password + * ... + */ + explicit User(const QString &login); + public: /*! * \brief getDbId - check if user with corresponding `link` exists in db - * and return db id if any + * and return db id if any (fills `id` var as well) * \return id on success, 0 otherwise */ int getDbId(); /*! - * \brief createInDb - create user in db + * \brief fetchFromDb - fetch db user record with corresponding `login` + * \return existing `id` on success, 0 otherwise + */ + int fetchFromDb(); + + /*! + * \brief existsInDb - helper function, checks if `this` has valid `id`, or + * db record with corresponding `login` exists (calls `getDbId` under the hood) + * \return true if exists, false otherwise + */ + bool existsInDb(); + + /*! + * \brief createInDb - create user in db, fill `id` * \return new user id on success, 0 otherwise */ int createInDb(); @@ -43,11 +74,11 @@ public: bool verifyPassword(const QString &password); -public: - int id{0}; - QString login; - QByteArray salt; - QByteArray passwordHash; +private: + int m_id{0}; + QString m_login; + QByteArray m_salt; + QByteArray m_passwordHash; private: QByteArray hashPassword(const QString &password); |
