diff --git a/src/cpp/controller/User.cpp b/src/cpp/controller/User.cpp index 14d098907..19c9df5a9 100644 --- a/src/cpp/controller/User.cpp +++ b/src/cpp/controller/User.cpp @@ -16,7 +16,6 @@ #include "Poco/Timestamp.h" - namespace controller { User::User(model::table::User* dbModel) : mPassword(nullptr), mGradidoKeyPair(nullptr), mCanDecryptPrivateKey(false), mGradidoCurrentBalance(0) diff --git a/src/cpp/model/table/User.cpp b/src/cpp/model/table/User.cpp index 806338856..e6f694c71 100644 --- a/src/cpp/model/table/User.cpp +++ b/src/cpp/model/table/User.cpp @@ -39,6 +39,7 @@ namespace model { void User::setPrivateKey(const MemoryBin* privateKey) { + std::unique_lock _lock(mSharedMutex); if (!privateKey) { mPrivateKey = Poco::Nullable(); } @@ -50,6 +51,7 @@ namespace model { void User::setPublicKey(const unsigned char* publicKey) { + std::unique_lock _lock(mSharedMutex); if (!publicKey) { mPublicKey = Poco::Nullable(); } @@ -217,6 +219,30 @@ namespace model { return 0; } + size_t User::updateFieldsFromCommunityServer() + { + //! \brief update first_name, last_name, disabled and language + assert(mID > 0); + Poco::ScopedLock _lock(mWorkMutex); + auto cm = ConnectionManager::getInstance(); + auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER); + + Poco::Data::Statement update(session); + update << "UPDATE users SET first_name = ?, last_name = ?, disabled = ?, language = ? where id = ?;", + use(mFirstName), use(mLastName), use(mDisabled), use(mLanguageKey), use(mID); + + + try { + return update.execute(); + } + catch (Poco::Exception& ex) { + addError(new ParamError(getTableName(), "[updateFieldsFromCommunityServer] mysql error by update", ex.displayText().data())); + addError(new ParamError(getTableName(), "data set: \n", toString().data())); + } + return 0; + } + + /* std::string mEmail; @@ -299,6 +325,7 @@ namespace model { std::string User::getPublicKeyHex() const { + std::shared_lock _lock(mSharedMutex); auto mm = MemoryManager::getInstance(); auto pubkeyHex = mm->getFreeMemory(65); diff --git a/src/cpp/model/table/User.h b/src/cpp/model/table/User.h index 7f097308c..7f8e35c0a 100644 --- a/src/cpp/model/table/User.h +++ b/src/cpp/model/table/User.h @@ -9,6 +9,8 @@ //#include "Poco/Nullable.h" //#include "Poco/Data/LOB.h" +#include + #include "UserRoles.h" namespace model { @@ -47,34 +49,38 @@ namespace model { size_t updatePublickey(); size_t updatePrivkeyAndPasswordHash(); size_t updatePubkeyAndPrivkey(); + + //! \brief update first_name, last_name, disabled and language + size_t updateFieldsFromCommunityServer(); // default getter unlocked - inline const std::string& getEmail() const { return mEmail; } - inline const std::string& getFirstName() const { return mFirstName; } - inline const std::string& getLastName() const { return mLastName; } - inline std::string getNameWithEmailHtml() const { return mFirstName + " " + mLastName + " <" + mEmail + ">"; } - inline const Poco::UInt64& getPasswordHashed() const { return mPasswordHashed; } - inline RoleType getRole() const { if (mRole.isNull()) return ROLE_NONE; return static_cast(mRole.value()); } + inline const std::string getEmail() const { std::shared_lock _lock(mSharedMutex); return mEmail; } + inline const std::string getFirstName() const { std::shared_lock _lock(mSharedMutex); return mFirstName; } + inline const std::string getLastName() const { std::shared_lock _lock(mSharedMutex); return mLastName; } + inline std::string getNameWithEmailHtml() const { std::shared_lock _lock(mSharedMutex); return mFirstName + " " + mLastName + " <" + mEmail + ">"; } + inline const Poco::UInt64 getPasswordHashed() const { std::shared_lock _lock(mSharedMutex); return mPasswordHashed; } + inline RoleType getRole() const { std::shared_lock _lock(mSharedMutex); if (mRole.isNull()) return ROLE_NONE; return static_cast(mRole.value()); } inline const unsigned char* getPublicKey() const { if (mPublicKey.isNull()) return nullptr; return mPublicKey.value().content().data(); } std::string getPublicKeyHex() const; - inline bool hasPrivateKeyEncrypted() const { return !mPrivateKey.isNull(); } + inline bool hasPrivateKeyEncrypted() const { std::shared_lock _lock(mSharedMutex); return !mPrivateKey.isNull(); } inline const std::vector& getPrivateKeyEncrypted() const { return mPrivateKey.value().content(); } - inline bool isEmailChecked() const { return mEmailChecked; } - inline const std::string& getLanguageKey() const { return mLanguageKey; } - inline bool isDisabled() const { return mDisabled; } + inline bool isEmailChecked() const { std::shared_lock _lock(mSharedMutex); return mEmailChecked; } + inline const std::string getLanguageKey() const { std::shared_lock _lock(mSharedMutex); return mLanguageKey; } + inline bool isDisabled() const { std::shared_lock _lock(mSharedMutex); return mDisabled; } // default setter unlocked - inline void setEmail(const std::string& email) { mEmail = email; } - inline void setFirstName(const std::string& first_name) { mFirstName = first_name; } - inline void setLastName(const std::string& last_name) { mLastName = last_name; } - inline void setPasswordHashed(const Poco::UInt64& passwordHashed) { mPasswordHashed = passwordHashed; } + inline void setEmail(const std::string& email) { std::unique_lock _lock(mSharedMutex); mEmail = email; } + inline void setFirstName(const std::string& first_name) { std::unique_lock _lock(mSharedMutex); mFirstName = first_name; } + inline void setLastName(const std::string& last_name) { std::unique_lock _lock(mSharedMutex); mLastName = last_name; } + inline void setPasswordHashed(const Poco::UInt64& passwordHashed) { std::unique_lock _lock(mSharedMutex); mPasswordHashed = passwordHashed; } void setPublicKey(const unsigned char* publicKey); //! \brief set encrypted private key //! \param privateKey copy data, didn't move memory bin void setPrivateKey(const MemoryBin* privateKey); - inline void setEmailChecked(bool emailChecked) { mEmailChecked = emailChecked; } - inline void setLanguageKey(const std::string& languageKey) { mLanguageKey = languageKey; } + inline void setEmailChecked(bool emailChecked) { std::unique_lock _lock(mSharedMutex); mEmailChecked = emailChecked; } + inline void setLanguageKey(const std::string& languageKey) { std::unique_lock _lock(mSharedMutex); mLanguageKey = languageKey; } + inline void setDisabled(bool disabled) { std::unique_lock _lock(mSharedMutex); mDisabled = disabled; } Poco::JSON::Object getJson(); @@ -107,6 +113,7 @@ namespace model { // from neighbor tables Poco::Nullable mRole; + mutable std::shared_mutex mSharedMutex; }; } }