Skip to content

Commit 1b507d5

Browse files
committed
Implemented first version (copied from fhq-server like a basic example)
1 parent db98970 commit 1b507d5

File tree

8 files changed

+122
-47
lines changed

8 files changed

+122
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@
4242

4343
# wsjcpp
4444
.wsjcpp
45+
.logs
4546
/tmp/
4647
/wsjcpp-user-session

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set(EXECUTABLE_OUTPUT_PATH ${wsjcpp-user-session_SOURCE_DIR})
1111
list (APPEND WSJCPP_INCLUDE_DIRS "src")
1212

1313
list (APPEND WSJCPP_SOURCES "src/main.cpp")
14+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_user_session.cpp")
1415

1516
#### BEGIN_WSJCPP_APPEND
1617
#### END_WSJCPP_APPEND

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
# wsjcpp-user-session
1+
# wsjcpp-user-session
2+
3+
```
4+
wsjcpp install https://github.com/wsjcpp/nlohmann_json:wsjcpp-copy-version
5+
wsjcpp install https://github.com/wsjcpp/wsjcpp-user-session:main
6+
```

src.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Automaticly generated by wsjcpp@v0.2.5
22
cmake_minimum_required(VERSION 3.0)
33

4-
add_definitions(-DWSJCPP_APP_VERSION="v0.0.1")
4+
add_definitions(-DWSJCPP_APP_VERSION="v1.0.0")
55
add_definitions(-DWSJCPP_APP_NAME="wsjcpp-user-session")
66

77
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

src/main.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <iostream>
33
#include <algorithm>
44
#include <wsjcpp_core.h>
5+
#include <wsjcpp_user_session.h>
56

67
int main(int argc, const char* argv[]) {
78
std::string TAG = "MAIN";
@@ -13,7 +14,26 @@ int main(int argc, const char* argv[]) {
1314
WsjcppLog::setPrefixLogFile("wsjcpp");
1415
WsjcppLog::setLogDirectory(".logs");
1516

16-
// TODO your code here
17+
WsjcppUserSession session;
18+
19+
nlohmann::json userInfo;
20+
userInfo["role"] = "admin";
21+
userInfo["nick"] = "Bug-man";
22+
userInfo["email"] = "admin@admin.com";
23+
userInfo["id"] = 55534;
24+
userInfo["uuid"] = "1312ba96-f9e0-49cd-89ba-6e07794e1ea1";
25+
nlohmann::json sessionInfo;
26+
sessionInfo["user"] = userInfo;
27+
28+
std::string sError = "";
29+
if (!session.fillFrom(sessionInfo, sError)) {
30+
WsjcppLog::err(TAG, sError);
31+
return -1;
32+
}
33+
34+
if (session.isAdmin()) {
35+
WsjcppLog::info(TAG, "user is admin with nickname " + session.nick());
36+
}
1737

1838
return 0;
1939
}

src/wsjcpp_user_session.cpp

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,39 @@
2222
* SOFTWARE.
2323
***********************************************************************************/
2424

25-
#include "wsjcpp_user_session.h"
26-
25+
#include "wsjcpp_user_session.h"
26+
#include <wsjcpp_core.h>
2727

2828
WsjcppUserSession::WsjcppUserSession() {
2929
TAG = "WsjcppUserSession";
3030
m_nUserID = -1;
3131
m_sRole = "";
3232
m_sEmail = "";
33-
m_sNick = "";
33+
m_sNickName = "";
3434
m_sUserUuid = "";
3535
}
3636

37-
WsjcppUserSession::WsjcppUserSession(nlohmann::json const &obj) : WsjcppUserSession() {
38-
this->fillFrom(obj);
37+
WsjcppUserSession::WsjcppUserSession(const nlohmann::json &obj) : WsjcppUserSession() {
38+
std::string sError;
39+
if (!this->fillFrom(obj, sError)) {
40+
WsjcppLog::err(TAG, sError);
41+
}
3942
}
4043

41-
void WsjcppUserSession::fillFrom(const nlohmann::json &obj) {
44+
bool WsjcppUserSession::fillFrom(const nlohmann::json &obj, std::string &sError) {
45+
bool bResult = true;
4246
if (obj.find("user") != obj.end()) {
4347
nlohmann::json user = obj.at("user");
4448

4549
// user.role
4650
try {
4751
m_sRole = user.at("role").get<std::string>();
4852
} catch (const std::exception &e) {
49-
WsjcppLog::err(TAG, "JSON: " + obj.dump());
50-
WsjcppLog::err(TAG, "Something wrong param user.role in struct. " + std::string(e.what()));
53+
// WsjcppLog::err(TAG, "JSON: " + obj.dump());
54+
// WsjcppLog::err(TAG, "Something wrong param user.role in struct. " + std::string(e.what()));
55+
sError = "Something wrong param user.role in struct. " + std::string(e.what());
5156
m_sRole = "";
57+
bResult = false;
5258
}
5359

5460
// TODO check allow roles
@@ -57,57 +63,92 @@ void WsjcppUserSession::fillFrom(const nlohmann::json &obj) {
5763
try {
5864
m_nUserID = user.at("id").get<int>();
5965
} catch (const std::exception &e) {
60-
WsjcppLog::err(TAG, "JSON: " + obj.dump());
61-
WsjcppLog::err(TAG, "Something wrong param user.id in struct. " + std::string(e.what()));
66+
// WsjcppLog::err(TAG, "JSON: " + obj.dump());
67+
// WsjcppLog::err(TAG, "Something wrong param user.id in struct. " + std::string(e.what()));
68+
if (sError.size() > 0) sError += "\n";
69+
sError += "Something wrong param user.id in struct. " + std::string(e.what());
6270
m_nUserID = -1;
71+
bResult = false;
6372
}
6473

6574
// user.email
6675
try {
6776
m_sEmail = user.at("email").get<std::string>();
6877
} catch (const std::exception &e) {
69-
WsjcppLog::err(TAG, "JSON: " + obj.dump());
70-
WsjcppLog::err(TAG, "Something wrong param user.email in struct. " + std::string(e.what()));
78+
// WsjcppLog::err(TAG, "JSON: " + obj.dump());
79+
// WsjcppLog::err(TAG, "Something wrong param user.email in struct. " + std::string(e.what()));
80+
if (sError.size() > 0) sError += "\n";
81+
sError += "Something wrong param user.email in struct. " + std::string(e.what());
7182
m_sEmail = "";
83+
bResult = false;
7284
}
7385

7486
// user.nick
7587
try {
76-
m_sNick = user.at("nick").get<std::string>();
88+
m_sNickName = user.at("nick").get<std::string>();
7789
} catch (const std::exception &e) {
78-
WsjcppLog::err(TAG, "JSON: " + obj.dump());
79-
WsjcppLog::err(TAG, "Something wrong param user.nick in struct. " + std::string(e.what()));
80-
m_sNick = "";
90+
// WsjcppLog::err(TAG, "JSON: " + obj.dump());
91+
// WsjcppLog::err(TAG, "Something wrong param user.nick in struct. " + std::string(e.what()));
92+
if (sError.size() > 0) sError += "\n";
93+
sError += "Something wrong param user.nick in struct. " + std::string(e.what());
94+
m_sNickName = "";
95+
bResult = false;
8196
}
8297

8398
// user.uuid
8499
try {
85100
m_sUserUuid = user.at("uuid").get<std::string>();
86101
} catch (const std::exception &e) {
87-
WsjcppLog::err(TAG, "JSON: " + obj.dump());
88-
WsjcppLog::err(TAG, "Something wrong param user.uuid in struct. " + std::string(e.what()));
102+
// WsjcppLog::err(TAG, "JSON: " + obj.dump());
103+
// WsjcppLog::err(TAG, "Something wrong param user.uuid in struct. " + std::string(e.what()));
104+
if (sError.size() > 0) sError += "\n";
105+
sError += "Something wrong param user.uuid in struct. " + std::string(e.what());
89106
m_sUserUuid = "";
107+
bResult = false;
90108
}
91-
92109
} else {
93-
WsjcppLog::warn(TAG, "Not found param 'user' in struct");
110+
sError = "Not found param 'user' in struct";
111+
bResult = false;
94112
}
113+
return bResult;
114+
}
115+
116+
nlohmann::json WsjcppUserSession::toJson() {
117+
nlohmann::json userInfo;
118+
userInfo["role"] = m_sRole;
119+
userInfo["nick"] = m_sNickName;
120+
userInfo["email"] = m_sEmail;
121+
userInfo["id"] = m_nUserID;
122+
userInfo["uuid"] = m_sUserUuid;
123+
nlohmann::json sessionInfo;
124+
sessionInfo["user"] = userInfo;
125+
return sessionInfo;
95126
}
96127

97-
bool WsjcppUserSession::isAdmin() { return m_sRole == "admin"; }
128+
void WsjcppUserSession::setRole(const std::string& role) { m_sRole = role; }
129+
130+
std::string WsjcppUserSession::role() const { return m_sRole; }
131+
132+
bool WsjcppUserSession::isAdmin() const { return m_sRole == "admin"; }
133+
134+
bool WsjcppUserSession::isUser() const { return m_sRole == "user"; }
135+
136+
bool WsjcppUserSession::isTester() const { return m_sRole == "tester"; }
137+
138+
bool WsjcppUserSession::hasRole() const { return m_sRole != ""; }
98139

99-
bool WsjcppUserSession::isUser() { return m_sRole == "user"; }
140+
void WsjcppUserSession::setNick(const std::string &sNickName) { m_sNickName = sNickName; }
100141

101-
bool WsjcppUserSession::isTester() { return m_sRole == "tester"; }
142+
std::string WsjcppUserSession::nick() const { return m_sNickName; }
102143

103-
bool WsjcppUserSession::hasRole() { return m_sRole != ""; }
144+
void WsjcppUserSession::setEmail(const std::string& sEmail) { m_sEmail = sEmail; }
104145

105-
std::string WsjcppUserSession::nick() { return m_sNickname; }
146+
std::string WsjcppUserSession::email() const { return m_sEmail; }
106147

107-
void WsjcppUserSession::setNick(QString sNick) { m_sNick = sNick.toStdString(); }
148+
void WsjcppUserSession::setUserId(int nUserId) { m_nUserID = nUserId; }
108149

109-
int WsjcppUserSession::userid() { return m_nUserID; }
150+
int WsjcppUserSession::userid() const { return m_nUserID; }
110151

111-
std::string WsjcppUserSession::userUuid() { return m_sUserUuid; }
152+
void WsjcppUserSession::setUserUuid(const std::string& sUserUuid) { m_sUserUuid = sUserUuid; }
112153

113-
std::string WsjcppUserSession::email() { return m_sEmail; }
154+
std::string WsjcppUserSession::userUuid() const { return m_sUserUuid; }

src/wsjcpp_user_session.h

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,37 @@
2525
#pragma once
2626

2727
#include <string>
28-
#include <json.hpp>
28+
#include <nlohmann/json.hpp>
2929

3030
class WsjcppUserSession {
3131
public:
3232
WsjcppUserSession();
33-
WsjcppUserSession(nlohmann::json const &obj);
34-
void fillFrom(nlohmann::json const &obj);
33+
WsjcppUserSession(const nlohmann::json &obj);
34+
bool fillFrom(const nlohmann::json &obj, std::string &sError);
35+
nlohmann::json toJson();
3536

36-
// IUserToken
37-
bool isAdmin();
38-
bool isUser();
39-
bool isTester();
40-
bool hasRole();
41-
std::string nick();
42-
void setNick(std::string);
43-
std::string email();
44-
int userid();
45-
std::string userUuid();
37+
void setRole(const std::string& role);
38+
std::string role() const;
39+
40+
// TODO extending roles somehow
41+
bool isAdmin() const;
42+
bool isUser() const;
43+
bool isTester() const;
44+
bool hasRole() const;
45+
void setNick(const std::string &sNickName);
46+
std::string nick() const;
47+
void setEmail(const std::string& sEmail);
48+
std::string email() const;
49+
void setUserId(int nUserId);
50+
int userid() const;
51+
void setUserUuid(const std::string& sUserUuid);
52+
std::string userUuid() const;
4653
// TODO json field for customization
4754

4855
private:
4956
std::string m_sRole;
5057
std::string m_sEmail;
51-
std::string m_sNick;
58+
std::string m_sNickName;
5259
int m_nUserID;
5360
std::string m_sUserUuid;
5461
std::string TAG;

wsjcpp.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "wsjcpp-user-session"
2-
version: "v0.0.1"
2+
version: v1.0.0
33
cmake_minimum_required: "3.0"
44
cmake_cxx_standard: "17"
55
description: "User Session"

0 commit comments

Comments
 (0)