fhq-server  v0.2.33
Documentation for fhq-server. FreeHackQuest is an open source platform for competitions of computer security.
cmd_handlers.h
Go to the documentation of this file.
1 #ifndef CMD_HADNLERS_H
2 #define CMD_HADNLERS_H
3 
4 #include <map>
5 #include <fallen.h>
6 #include <wsjcpp_validators.h>
7 #include <json.hpp>
8 #include <QWebSocket>
9 #include <QSqlQuery> // TODO deprecated
10 #include <QSqlRecord> // TODO deprecated
11 #include <QString> // TODO deprecated
12 #include <QVariant> // TODO deprecated
13 
18 class WsjcppError {
19  public:
20  WsjcppError(int nCodeError, const std::string &sMessage);
21  int codeError();
22  std::string message();
23  private:
24  std::string m_sMessage;
26 };
27 
33  public:
35  WsjcppUserSession(nlohmann::json const& obj);
36  void fillFrom(nlohmann::json const& obj);
37 
38  // IUserToken
39  bool isAdmin();
40  bool isUser();
41  bool isTester();
42  bool hasRole();
43  QString nick();
44  void setNick(QString);
45  QString email();
46  int userid();
47  std::string userUuid();
48  // TODO json field for customization
49  private:
50 
51  std::string m_sRole;
52  std::string m_sEmail;
53  std::string m_sNick;
54  int m_nUserID;
55  std::string m_sUserUuid;
56  std::string TAG;
57 };
58 
63 class WsjcppSocketClient : public QObject {
64  private:
65  Q_OBJECT
66 
67  public:
68  WsjcppSocketClient(QWebSocket *pSocket);
70 
71  private:
72  std::string TAG;
74  QWebSocket *m_pSocket;
75 
76  private Q_SLOTS:
77  void processTextMessage(const QString &message);
78  void processBinaryMessage(QByteArray message);
79  void socketDisconnected();
80 };
81 
87  public:
88  virtual void sendMessage(QWebSocket *pClient, const nlohmann::json& jsonResponse) = 0;
89  virtual void sendMessageError(QWebSocket *pClient, const std::string &sCmd, const std::string & sM, WsjcppError error) = 0;
90  virtual void sendToAll(const nlohmann::json& jsonMessage) = 0;
91  virtual void sendToOne(QWebSocket *pClient, const nlohmann::json &jsonMessage) = 0;
92  virtual int getConnectedUsers() = 0;
93  virtual void setWsjcppUserSession(QWebSocket *pClient, WsjcppUserSession *pUserSession) = 0;
94  virtual WsjcppUserSession *getWsjcppUserSession(QWebSocket *pClient) = 0;
95 };
96 
101 class CmdInputDef {
102  public:
103  CmdInputDef(const std::string &sName, const std::string &sDescription);
104  CmdInputDef();
105  CmdInputDef & optional();
106  CmdInputDef & required();
107  CmdInputDef & string_();
108  CmdInputDef & integer_();
109  CmdInputDef & any_();
110  CmdInputDef & bool_();
111  CmdInputDef & description(const std::string &sDescription);
112  CmdInputDef & minval(int minval);
113  CmdInputDef & maxval(int maxval);
114  nlohmann::json toJson();
115 
116  const std::string &getType();
117  const std::string &getType() const;
118  const std::string &getName();
119  const std::string &getName() const;
120  const std::string &getRestrict();
121  const std::string &getRestrict() const;
122  const std::string &getDescription();
123  const std::string &getDescription() const;
124 
125  bool isRequired();
126  bool isInteger();
127  bool isString();
128  bool isBool();
129  bool isAny();
130 
131  bool isMinVal(); // TODO: redesign to validators
132  int getMinVal(); // TODO: redesign to validators
133  bool isMaxVal(); // TODO: redesign to validators
134  int getMaxVal(); // TODO: redesign to validators
135 
136  CmdInputDef &addValidator(WsjcppValidatorStringBase *pValidatorStringBase);
137 
138  const std::vector<WsjcppValidatorStringBase *> &listOfValidators();
139 
140  private:
141  std::string m_sType;
142  std::string m_sName;
143  std::string m_sRestrict;
144  std::string m_sDescription;
149 
150  std::string CMD_INPUT_DEF_TYPE_INTEGER = "integer";
151  std::string CMD_INPUT_DEF_TYPE_STRING = "string";
152  std::string CMD_INPUT_DEF_TYPE_BOOL = "boolean";
153  std::string CMD_INPUT_DEF_TYPE_ANY = "any";
154 
155  std::vector<WsjcppValidatorStringBase *> m_vValidatorsString;
156 };
157 
158 // ---------------------------------------------------------------------
159 
161  public:
162  ModelRequest(QWebSocket *pClient, IWebSocketServer *pWebSocketServer, nlohmann::json &jsonRequest_);
163  QWebSocket *client();
164  std::string getIpAddress();
165  IWebSocketServer *server();
166  WsjcppUserSession *getUserSession();
167  bool isAdmin();
168  bool isUser();
169  bool isUnauthorized();
170  // TODO set input defs
171 
172  const nlohmann::json& jsonRequest(); // TODO deprecated
173  bool hasInputParam(const std::string &sParamName);
174  std::string getInputString(const std::string &sParamName, const std::string &sDefaultValue);
175  int getInputInteger(const std::string &sParamName, int defaultValue);
176 
177  std::string m();
178  bool hasM();
179  std::string command();
180  bool hasCommand();
181  void sendMessageError(const std::string &cmd, WsjcppError error);
182  void sendMessageSuccess(const std::string &cmd, nlohmann::json& jsonResponse);
183  void sendResponse(nlohmann::json& jsonResult);
184 
185  // bool validateInputParameters(Error &error, CmdHandlerBase *pCmdHandler);
186  private:
187  std::string TAG;
188  QWebSocket *m_pClient;
191  nlohmann::json m_jsonRequest;
192  std::string m_sMessageId;
193  std::string m_sCommand;
194 };
195 
196 // ---------------------------------------------------------------------
197 
202 class CmdHandlerBase { // TODO rename to WJSCppHandler
203 
204  public:
205  CmdHandlerBase(const std::string &sCmd, const std::string &sDescription);
206  virtual std::string cmd();
207  virtual std::string description();
208  std::string activatedFromVersion();
209  std::string deprecatedFromVersion();
210  bool accessUnauthorized();
211  bool accessUser();
212  bool accessAdmin();
213  bool checkAccess(ModelRequest *pRequest);
214 
215  virtual const std::vector<CmdInputDef> &inputs();
216  virtual void handle(ModelRequest *pRequest) = 0;
217 
218  // virtual void success(nlohmann::json jsonResponse);
219  // virtual void error(int nCode, const std::string &sErrorMessage);
220 
221  protected:
222  void setAccessUnauthorized(bool bAccess);
223  void setAccessUser(bool bAccess);
224  void setAccessAdmin(bool bAccess);
225  void setActivatedFromVersion(const std::string &sActivatedFromVersion);
226  void setDeprecatedFromVersion(const std::string &sDeprecatedFromVersion);
227 
228  CmdInputDef &requireStringParam(const std::string &sName, const std::string &sDescription);
229  CmdInputDef &optionalStringParam(const std::string &sName, const std::string &sDescription);
230  CmdInputDef &requireIntegerParam(const std::string &sName, const std::string &sDescription);
231  CmdInputDef &optionalIntegerParam(const std::string &sName, const std::string &sDescription);
232  CmdInputDef &requireBooleanParam(const std::string &sName, const std::string &sDescription);
233  CmdInputDef &optionalBooleanParam(const std::string &sName, const std::string &sDescription);
234 
235  std::string TAG;
236  std::string m_sCmd;
237  std::string m_sDescription;
238 
239  private:
240  std::vector<CmdInputDef> m_vInputs; // TODO redesign to map
241  // std::map<std::string, CmdInputDef*> *m_vCmdInputDefs;
247 };
248 
249 extern std::map<std::string, CmdHandlerBase*> *g_pCmdHandlers;
250 
251 // ---------------------------------------------------------------------
252 
257 class CmdHandlers {
258  public:
259  static void initGlobalVariables();
260  static void addHandler(const std::string &sName, CmdHandlerBase* pCmdHandler);
261  static CmdHandlerBase *findCmdHandler(const std::string &sCmd);
262 };
263 
264 // RegistryCmdHandler
265 #define REGISTRY_CMD( classname ) \
266  static classname * pRegistry ## classname = new classname(); \
267 
268 // ---------------------------------------------------------------------
269 
275 
276  public:
278  virtual void handle(ModelRequest *pRequest);
279 };
280 
281 #endif // CMD_HADNLERS_H
std::string m_sDescription
Definition: cmd_handlers.h:144
WsjcppUserSession * m_pUserSession
Definition: cmd_handlers.h:73
std::map< std::string, CmdHandlerBase * > * g_pCmdHandlers
Definition: cmd_handlers.cpp:807
This handler will be return list of handlers - publish api interfaces.
Definition: cmd_handlers.h:274
std::string TAG
Definition: cmd_handlers.h:187
Definition: cmd_handlers.h:160
std::string m_sType
Definition: cmd_handlers.h:141
std::string TAG
Definition: cmd_handlers.h:56
std::string m_sName
Definition: cmd_handlers.h:142
std::string m_sRestrict
Definition: cmd_handlers.h:143
int m_nUserID
Definition: cmd_handlers.h:54
std::string TAG
Definition: cmd_handlers.h:72
std::string m_sEmail
Definition: cmd_handlers.h:52
std::string m_sDescription
Definition: cmd_handlers.h:237
std::string m_sMessage
Definition: cmd_handlers.h:24
IWebSocketServer * m_pServer
Definition: cmd_handlers.h:189
int m_nMinVal
Definition: cmd_handlers.h:145
std::string m_sUserUuid
Definition: cmd_handlers.h:55
std::string m_sDeprecatedFromVersion
Definition: cmd_handlers.h:243
std::string m_sCmd
Definition: cmd_handlers.h:236
Global collection with handlers.
Definition: cmd_handlers.h:257
std::string m_sActivatedFromVersion
Definition: cmd_handlers.h:242
std::string m_sMessageId
Definition: cmd_handlers.h:192
IWebSocketServer -.
Definition: cmd_handlers.h:86
WsjcppUserSession * m_pWsjcppUserSession
Definition: cmd_handlers.h:190
WsjcppError(int nCodeError, const std::string &sMessage)
WsjcppError -.
Definition: cmd_handlers.cpp:10
WsjcppError - helper class for errors.
Definition: cmd_handlers.h:18
bool m_bSettedMaxVal
Definition: cmd_handlers.h:148
WsjcppSocketClient -.
Definition: cmd_handlers.h:63
std::string m_sCommand
Definition: cmd_handlers.h:193
std::string TAG
Definition: cmd_handlers.h:235
std::string m_sNick
Definition: cmd_handlers.h:53
CmdInputDef - helper api for define input params and descrip it for docs.
Definition: cmd_handlers.h:101
bool m_bAccessAdmin
Definition: cmd_handlers.h:246
Definition: wsjcpp_validators.h:47
std::string message()
Definition: cmd_handlers.cpp:23
std::vector< WsjcppValidatorStringBase * > m_vValidatorsString
Definition: cmd_handlers.h:155
std::vector< CmdInputDef > m_vInputs
Definition: cmd_handlers.h:240
bool m_bAccessUser
Definition: cmd_handlers.h:245
std::string m_sRole
Definition: cmd_handlers.h:51
int codeError()
Definition: cmd_handlers.cpp:17
Api handler Base.
Definition: cmd_handlers.h:202
QWebSocket * m_pSocket
Definition: cmd_handlers.h:74
QWebSocket * m_pClient
Definition: cmd_handlers.h:188
WsjcppUserSession -.
Definition: cmd_handlers.h:32
int m_nCodeError
Definition: cmd_handlers.h:25
int m_nMaxVal
Definition: cmd_handlers.h:147
bool m_bAccessUnauthorized
Definition: cmd_handlers.h:244
bool m_bSettedMinVal
Definition: cmd_handlers.h:146
nlohmann::json m_jsonRequest
Definition: cmd_handlers.h:191