TreeFrogFramework  2.8
tactioncontroller.h
Go to the documentation of this file.
1 #pragma once
2 #include <QHostAddress>
3 #include <QtCore>
4 #include <TAbstractController>
5 #include <TAccessValidator>
6 #include <TActionHelper>
7 #include <TCookieJar>
8 #include <THttpRequest>
9 #include <THttpResponse>
10 #include <TActionContext>
11 #include <TSession>
12 #include <TGlobal>
13 
14 class TActionView;
15 class TAbstractUser;
16 class TFormValidator;
17 class TCache;
18 class QDomDocument;
19 
20 
22 public:
24  virtual ~TActionController();
25 
26  QString name() const override;
27  QString activeAction() const override;
28  QStringList arguments() const override;
29  const THttpRequest &request() const override;
30  const THttpRequest &httpRequest() const override { return request(); }
31  const THttpResponse &response() const { return _response; }
32  const THttpResponse &httpResponse() const { return response(); }
33  const TSession &session() const override { return _sessionStore; }
34  QString getRenderingData(const QString &templateName, const QVariantMap &vars = QVariantMap()) override;
35  virtual bool sessionEnabled() const { return true; }
36  virtual bool csrfProtectionEnabled() const { return true; }
37  virtual QStringList exceptionActionsOfCsrfProtection() const { return QStringList(); }
38  virtual bool transactionEnabled() const { return true; }
39  QByteArray authenticityToken() const override;
40  QString flash(const QString &name) const;
41  QHostAddress clientAddress() const;
42  virtual bool isUserLoggedIn() const override;
43  virtual QString identityKeyOfLoginUser() const;
44  void setFlash(const QString &name, const QVariant &value) override;
45  void reset() override;
46 
47  static void setCsrfProtectionInto(TSession &session);
48  static const QStringList &availableControllers();
49  static const QStringList &disabledControllers();
50  static QString loginUserNameKey();
51 
52 protected:
53  virtual bool preFilter() { return true; }
54  virtual void postFilter() {}
55  void setLayoutEnabled(bool enable);
56  void setLayoutDisabled(bool disable);
57  bool layoutEnabled() const;
58  void setLayout(const QString &layout);
59  QString layout() const;
60  void setStatusCode(int code);
61  int statusCode() const { return _statCode; }
62  void setFlashValidationErrors(const TFormValidator &validator, const QString &prefix = QString("err_"));
63  TSession &session() override { return _sessionStore; }
64  void setSession(const TSession &session);
65  bool addCookie(const TCookie &cookie) override;
66  bool addCookie(const QByteArray &name, const QByteArray &value, const QDateTime &expire = QDateTime(), const QString &path = QString(), const QString &domain = QString(), bool secure = false, bool httpOnly = false, const QByteArray &sameSite = "Lax") override;
67  bool addCookie(const QByteArray &name, const QByteArray &value, int64_t maxAge, const QString &path = QString(), const QString &domain = QString(), bool secure = false, bool httpOnly = false, const QByteArray &sameSite = "Lax") override;
68  QByteArray contentType() const;
69  void setContentType(const QByteArray &type);
70  bool render(const QString &action = QString(), const QString &layout = QString());
71  bool renderTemplate(const QString &templateName, const QString &layout = QString());
72  bool renderText(const QString &text, bool layoutEnable = false, const QString &layout = QString());
73  bool renderXml(const QDomDocument &document);
74  bool renderXml(const QVariantMap &map);
75  bool renderXml(const QVariantList &list);
76  bool renderXml(const QStringList &list);
77  bool renderJson(const QJsonDocument &document);
78  bool renderJson(const QJsonObject &object);
79  bool renderJson(const QJsonArray &array);
80  bool renderJson(const QVariantMap &map);
81  bool renderJson(const QVariantList &list);
82  bool renderJson(const QStringList &list);
83  bool renderAndCache(const QByteArray &key, int seconds, const QString &action = QString(), const QString &layout = QString());
84  bool renderOnCache(const QByteArray &key);
85  void removeCache(const QByteArray &key);
86 #if QT_VERSION >= 0x050c00 // 5.12.0
87  bool renderCbor(const QVariant &variant, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
88  bool renderCbor(const QVariantMap &map, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
89  bool renderCbor(const QVariantHash &hash, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
90  bool renderCbor(const QCborValue &value, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
91  bool renderCbor(const QCborMap &map, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
92  bool renderCbor(const QCborArray &array, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
93 #endif
94  bool renderErrorResponse(int statusCode);
95  void redirect(const QUrl &url, int statusCode = Tf::Found);
96  bool sendFile(const QString &filePath, const QByteArray &contentType, const QString &name = QString(), bool autoRemove = false);
97  bool sendData(const QByteArray &data, const QByteArray &contentType, const QString &name = QString());
98  void rollbackTransaction() { _rollback = true; }
99  void setAutoRemove(const QString &filePath);
100  bool validateAccess(const TAbstractUser *user);
101  // For WebSocket
102  void sendTextToWebSocket(int socket, const QString &text);
103  void sendBinaryToWebSocket(int socket, const QByteArray &binary);
104  void closeWebSokcet(int socket, int closeCode = Tf::NormalClosure);
105  void publish(const QString &topic, const QString &text);
106  void publish(const QString &topic, const QByteArray &binary);
107  void flushResponse();
108 
109  virtual bool userLogin(const TAbstractUser *user);
110  virtual void userLogout();
111  virtual void setAccessRules() {}
112 
115  THttpResponse &httpResponse() { return _response; }
116 
117 private:
118  enum WebSocketTaskType {
119  SendTextTo = 0,
120  SendBinaryTo,
121  SendCloseTo,
122  PublishText,
123  PublishBinary,
124  };
125 
126  enum class RenderState {
127  NotRendered = 0,
128  Rendered,
129  DataSent,
130  };
131 
132  void setActionName(const QString &name);
133  void setArguments(const QStringList &arguments) { _args = arguments; }
134  bool verifyRequest(const THttpRequest &request) const;
135  QByteArray renderView(TActionView *view);
136  void exportAllFlashVariants();
137  const TActionController *controller() const override { return this; }
138  bool rollbackRequested() const { return _rollback; }
139  static QString layoutClassName(const QString &layout);
140  static QString partialViewClassName(const QString &partial);
141 
142  mutable QString _ctrlName;
143  QString _actionName;
144  QStringList _args;
145  int _statCode {Tf::OK}; // 200 OK
146  RenderState _rendered {RenderState::NotRendered};
147  bool _layoutEnable {true};
148  QString _layoutName;
149  THttpResponse _response;
150  QVariantMap _flashVars;
151  TSession _sessionStore;
152  TCookieJar _cookieJar;
153  bool _rollback {false};
154  QStringList _autoRemoveFiles;
155  QList<QPair<int, QVariant>> _taskList;
156 
157  friend class TActionContext;
158  friend class TSessionCookieStore;
161 };
162 
163 
164 inline QString TActionController::activeAction() const
165 {
166  return _actionName;
167 }
168 
169 inline QStringList TActionController::arguments() const
170 {
171  return _args;
172 }
173 
174 inline void TActionController::setActionName(const QString &name)
175 {
176  _actionName = name;
177 }
178 
179 inline void TActionController::setLayoutEnabled(bool enable)
180 {
181  _layoutEnable = enable;
182 }
183 
184 inline void TActionController::setLayoutDisabled(bool disable)
185 {
186  _layoutEnable = !disable;
187 }
188 
190 {
191  return _layoutEnable;
192 }
193 
194 inline QString TActionController::layout() const
195 {
196  return _layoutName;
197 }
198 
199 inline void TActionController::setStatusCode(int code)
200 {
201  _statCode = code;
202 }
203 
204 inline QString TActionController::flash(const QString &name) const
205 {
206  return _flashVars.value(name).toString();
207 }
208 
209 inline QByteArray TActionController::contentType() const
210 {
211  return _response.header().contentType();
212 }
213 
214 inline void TActionController::setContentType(const QByteArray &type)
215 {
216  _response.header().setContentType(type);
217 }
218 
The TAbstractController class is the abstract base class of controllers, providing functionality comm...
Definition: tabstractcontroller.h:13
virtual QStringList arguments() const
Definition: tabstractcontroller.h:20
virtual QString getRenderingData(const QString &templateName, const QVariantMap &vars=QVariantMap())
Definition: tabstractcontroller.cpp:99
virtual void setFlash(const QString &name, const QVariant &value)
Definition: tabstractcontroller.cpp:93
virtual bool addCookie(const TCookie &cookie)
Definition: tabstractcontroller.cpp:129
virtual const THttpRequest & request() const
Definition: tabstractcontroller.cpp:111
virtual QByteArray authenticityToken() const
Definition: tabstractcontroller.h:25
virtual QString name() const =0
This function is reimplemented in subclasses to return a controller name.
virtual void reset()
Definition: tabstractcontroller.h:39
virtual bool isUserLoggedIn() const
Returns true if a user is logged in to the system; otherwise returns false.
Definition: tabstractcontroller.cpp:152
virtual QString activeAction() const =0
This function is reimplemented in subclasses to return a active action name.
The TAbstractUser class is the abstract base class of users, providing functionality common to users.
Definition: tabstractuser.h:6
The TAccessValidator class provides validation of user access.
Definition: taccessvalidator.h:11
The TActionController class is the base class of all action controllers.
Definition: tactioncontroller.h:21
void rollbackTransaction()
This function is called to rollback a transaction on the database.
Definition: tactioncontroller.h:98
virtual void setAccessRules()
Sets rules of access to this controller.
Definition: tactioncontroller.h:111
virtual void postFilter()
This function is called after actions on the controller are performed.
Definition: tactioncontroller.h:54
THttpResponse & httpResponse()
Returns a HTTP response to be sent.
Definition: tactioncontroller.h:115
void setStatusCode(int code)
Sets the status code to code.
Definition: tactioncontroller.h:199
QString activeAction() const override
Returns the active action name.
Definition: tactioncontroller.h:164
bool layoutEnabled() const
Returns true if the layout mechanism is enabled; otherwise returns false.
Definition: tactioncontroller.h:189
const TSession & session() const override
Returns the current HTTP session, allows associating information with individual visitors.
Definition: tactioncontroller.h:33
TSession & session() override
Returns the current HTTP session, allows associating information with individual visitors.
Definition: tactioncontroller.h:63
const THttpResponse & response() const
Definition: tactioncontroller.h:31
QString name() const override
Returns the controller name.
Definition: tactioncontroller.cpp:66
QStringList arguments() const override
Definition: tactioncontroller.h:169
const THttpResponse & httpResponse() const
Returns a HTTP response to be sent.
Definition: tactioncontroller.h:32
virtual bool transactionEnabled() const
Must be overridden by subclasses to disable transaction mechanism.
Definition: tactioncontroller.h:38
THttpRequest & httpRequest()
Definition: tactioncontroller.h:114
QString flash(const QString &name) const
Returns the flash message for name.
Definition: tactioncontroller.h:204
void setContentType(const QByteArray &type)
Sets the content type specified by type for a response message.
Definition: tactioncontroller.h:214
virtual bool preFilter()
This function is called before actions on the controller are performed, therefore can be overridden b...
Definition: tactioncontroller.h:53
void setLayoutDisabled(bool disable)
Disables the layout mechanism if disable is true, otherwise enables it.
Definition: tactioncontroller.h:184
int statusCode() const
Returns the status code of the HTTP response to be sent.
Definition: tactioncontroller.h:61
virtual bool sessionEnabled() const
Must be overridden by subclasses to enable a HTTP session.
Definition: tactioncontroller.h:35
const THttpRequest & httpRequest() const override
Definition: tactioncontroller.h:30
void setLayoutEnabled(bool enable)
Enables the layout mechanism if enable is true, otherwise disables it.
Definition: tactioncontroller.h:179
virtual QStringList exceptionActionsOfCsrfProtection() const
Must be overridden by subclasses to return a string list of actions excluded from CSRF protection whe...
Definition: tactioncontroller.h:37
QString layout() const
Returns the name of the layout template.
Definition: tactioncontroller.h:194
QByteArray contentType() const
Returns the content type for a response message.
Definition: tactioncontroller.h:209
virtual bool csrfProtectionEnabled() const
Must be overridden by subclasses to disable CSRF protection.
Definition: tactioncontroller.h:36
The TActionHelper class is the base class of all helpers.
Definition: tactionhelper.h:12
virtual const TAbstractController * controller() const =0
The TActionView class is the abstract base class of views, providing functionality common to view.
Definition: tactionview.h:14
The TCache class stores items so that can be served faster.
Definition: tcache.h:7
The TCookieJar class holds network cookies.
Definition: tcookiejar.h:12
The TCookie class holds one network cookie.
Definition: tcookie.h:7
The TFormValidator class provides form validation for a map-table-based dictionary.
Definition: tformvalidator.h:9
The THttpRequest class contains request information for HTTP.
Definition: thttprequest.h:34
The THttpResponse class contains response information for HTTP.
Definition: thttpresponse.h:10
THttpResponseHeader & header()
Return the HTTP header.
Definition: thttpresponse.h:16
void setContentType(const QByteArray &type)
Sets the value of the header field content-type to type.
Definition: tinternetmessageheader.cpp:123
QByteArray contentType() const
Returns the value of the header field content-type.
Definition: tinternetmessageheader.cpp:115
The TSession class holds information associated with individual visitors.
Definition: tsession.h:7
@ OK
Definition: tfnamespace.h:39
@ Found
Definition: tfnamespace.h:49
@ NormalClosure
Definition: tfnamespace.h:213
#define T_CORE_EXPORT
Definition: tdeclexport.h:28
#define T_DISABLE_COPY(Class)
Definition: tdeclexport.h:37
#define T_DISABLE_MOVE(Class)
Definition: tdeclexport.h:41