2222 * SOFTWARE.
2323 ***********************************************************************************/
2424
25- #include " wsjcpp_user_session.h"
26-
25+ #include " wsjcpp_user_session.h"
26+ # include < wsjcpp_core.h >
2727
2828WsjcppUserSession::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 ; }
0 commit comments