-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusuariocontroller.cpp
74 lines (63 loc) · 2.15 KB
/
usuariocontroller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "usuariocontroller.h"
#include <chrono>
#include <folly/Synchronized.h>
#include <folly/Singleton.h>
#include <proxygen/lib/utils/CryptUtil.h>
#include <folly/Range.h>
using namespace folly;
namespace cxxdoor {
static std::string map_key = "CXX_MAP_KEY";
namespace {
struct UsuarioControllerTag {};
}
static Singleton<UsuarioController, UsuarioControllerTag> the_instance;
UsuarioController::UsuarioController() {
auto locked = _usuarios.lock();
//_db = DbManager::getInstance();
DbManager::getInstance()->load<UserMap>(map_key, *locked, false);
}
UsuarioController::~UsuarioController() {
auto locked = _usuarios.lock();
DbManager::getInstance()->save<UserMap>(map_key, *locked, false);
}
bool UsuarioController::crearUsuario(std::string nombre, std::string password,
std::string email) {
auto locked = _usuarios.lock();
auto found = locked->find(nombre);
if (found == locked->end()) {
auto user = std::make_shared<Usuario>();
user->setNombre(nombre);
user->setPassword(md5(password));
user->setEmail(email);
user->setId(RocksEntity::generateId());
locked->emplace(std::make_pair(std::string(nombre), user));
return true;
}
return false;
}
boost::optional<std::shared_ptr<TokenInfo>>
UsuarioController::authenticate(std::string nombre, std::string password) {
auto locked = _usuarios.lock();
auto result = locked->find(nombre);
if (result != locked->end()) {
auto user = result->second;//(*locked[nombre];
if (user->getPassword() == md5(password)) {
auto tokens = _tokens.lock();
auto t = std::make_shared<TokenInfo>(RocksEntity::generateId(), user);
tokens->emplace(std::make_pair(std::string(t->token), t));
return t;
}
}
return boost::none;
}
std::shared_ptr<UsuarioController>
UsuarioController::getInstance() {
return the_instance.try_get();
}
std::string UsuarioController::md5(std::string message) {
return proxygen::md5Encode(folly::StringPiece(message));
}
TokenInfo::TokenInfo(const std::string &token, const std::shared_ptr<Usuario> &usuario)
: token(token), usuario(usuario), timestamp(std::chrono::system_clock::now()) {
}
}