generated from Sithas/conan_template
DAO - Полное завершение класса
This commit is contained in:
@@ -45,6 +45,9 @@ add_executable(App ./src/main.cpp
|
|||||||
./src/endpoints_handlers/IExecutor.h
|
./src/endpoints_handlers/IExecutor.h
|
||||||
./src/endpoints_handlers/AuthRegistrationExecutor.h
|
./src/endpoints_handlers/AuthRegistrationExecutor.h
|
||||||
./src/endpoints_handlers/RootExecutor.h
|
./src/endpoints_handlers/RootExecutor.h
|
||||||
|
src/DAO/IAuthDAO.h
|
||||||
|
src/DAO/MemoryAuthDAO.cpp
|
||||||
|
src/DAO/MemoryAuthDAO.h
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(App PRIVATE Boost::boost Boost::json Threads::Threads mysql::concpp)
|
target_link_libraries(App PRIVATE Boost::boost Boost::json Threads::Threads mysql::concpp)
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
|
||||||
|
#include "../entities/User.h"
|
||||||
|
|
||||||
|
namespace uad
|
||||||
|
{
|
||||||
|
class IAuthDAO
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual std::string Login(const std::string& registrated_user_uuid) = 0;
|
||||||
|
|
||||||
|
virtual bool HasAuthorizedUser(const std::string& auth_token) = 0;
|
||||||
|
|
||||||
|
virtual bool Logout(const std::string& user_token) = 0;
|
||||||
|
|
||||||
|
virtual ~IAuthDAO() = default;
|
||||||
|
};
|
||||||
|
}
|
||||||
+2
-1
@@ -1,7 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "../entities/User.h"
|
#include "../entities/User.h"
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
#include <boost/uuid.hpp>
|
||||||
|
|
||||||
|
#include "MemoryAuthDAO.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
namespace uad
|
||||||
|
{
|
||||||
|
MemoryAuthDAO::MemoryAuthDAO(mysqlx::Session& session): session_(session)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string MemoryAuthDAO::Login(const std::string& registrated_user_uuid)
|
||||||
|
{
|
||||||
|
boost::uuids::random_generator generator;
|
||||||
|
boost::uuids::uuid uuid = generator();
|
||||||
|
std::string auth_token = boost::uuids::to_string(uuid);
|
||||||
|
|
||||||
|
users_uuids_to_auth_tokens_[registrated_user_uuid] = auth_token;
|
||||||
|
auth_tokens_to_users_uuids_[auth_token] = registrated_user_uuid;
|
||||||
|
|
||||||
|
return auth_token;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MemoryAuthDAO::HasAuthorizedUser(const std::string& auth_token)
|
||||||
|
{
|
||||||
|
return auth_tokens_to_users_uuids_.count(auth_token) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MemoryAuthDAO::Logout(const std::string& auth_token)
|
||||||
|
{
|
||||||
|
string user_uuid = auth_tokens_to_users_uuids_[auth_token];
|
||||||
|
|
||||||
|
if (!HasAuthorizedUser()) return false;
|
||||||
|
|
||||||
|
users_uuids_to_auth_tokens_.erase(user_uuid);
|
||||||
|
auth_tokens_to_users_uuids_.erase(auth_token);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
#include <mysqlx/xdevapi.h>
|
||||||
|
|
||||||
|
#include "IAuthDAO.h"
|
||||||
|
|
||||||
|
namespace uad
|
||||||
|
{
|
||||||
|
class MemoryAuthDAO : public uad::IAuthDAO
|
||||||
|
{
|
||||||
|
std::unordered_map<std::string, std::string> users_uuids_to_auth_tokens_;
|
||||||
|
std::unordered_map<std::string, std::string> auth_tokens_to_users_uuids_;
|
||||||
|
|
||||||
|
mysqlx::Session& session_;
|
||||||
|
public:
|
||||||
|
explicit MemoryAuthDAO(mysqlx::Session& session);
|
||||||
|
|
||||||
|
std::string Login(const std::string& registrated_user_uuid) override;
|
||||||
|
|
||||||
|
bool HasAuthorizedUser(const std::string& auth_token) override;
|
||||||
|
|
||||||
|
bool Logout(const std::string& auth_token) override;
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user