TreeFrogFramework  2.0
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;
43  virtual QString identityKeyOfLoginUser() const;
44  void setFlash(const QString &name, const QVariant &value) override;
45 
46  static void setCsrfProtectionInto(TSession &session);
47  static const QStringList &availableControllers();
48  static const QStringList &disabledControllers();
49  static QString loginUserNameKey();
50 
51 protected:
52  virtual bool preFilter() { return true; }
53  virtual void postFilter() {}
54  void setLayoutEnabled(bool enable);
55  void setLayoutDisabled(bool disable);
56  bool layoutEnabled() const;
57  void setLayout(const QString &layout);
58  QString layout() const;
59  void setStatusCode(int code);
60  int statusCode() const { return _statCode; }
61  void setFlashValidationErrors(const TFormValidator &validator, const QString &prefix = QString("err_"));
62  TSession &session() override { return _sessionStore; }
63  void setSession(const TSession &session);
64  bool addCookie(const TCookie &cookie) override;
65  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;
66  bool addCookie(const QByteArray &name, const QByteArray &value, qint64 maxAge, const QString &path = QString(), const QString &domain = QString(), bool secure = false, bool httpOnly = false, const QByteArray &sameSite = "Lax") override;
67  QByteArray contentType() const;
68  void setContentType(const QByteArray &type);
69  bool render(const QString &action = QString(), const QString &layout = QString());
70  bool renderTemplate(const QString &templateName, const QString &layout = QString());
71  bool renderText(const QString &text, bool layoutEnable = false, const QString &layout = QString());
72  bool renderXml(const QDomDocument &document);
73  bool renderXml(const QVariantMap &map);
74  bool renderXml(const QVariantList &list);
75  bool renderXml(const QStringList &list);
76  bool renderJson(const QJsonDocument &document);
77  bool renderJson(const QJsonObject &object);
78  bool renderJson(const QJsonArray &array);
79  bool renderJson(const QVariantMap &map);
80  bool renderJson(const QVariantList &list);
81  bool renderJson(const QStringList &list);
82  bool renderAndCache(const QByteArray &key, int seconds, const QString &action = QString(), const QString &layout = QString());
83  bool renderOnCache(const QByteArray &key);
84  void removeCache(const QByteArray &key);
85 #if QT_VERSION >= 0x050c00 // 5.12.0
86  bool renderCbor(const QVariant &variant, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
87  bool renderCbor(const QVariantMap &map, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
88  bool renderCbor(const QVariantHash &hash, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
89  bool renderCbor(const QCborValue &value, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
90  bool renderCbor(const QCborMap &map, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
91  bool renderCbor(const QCborArray &array, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
92 #endif
93  bool renderErrorResponse(int statusCode);
94  void redirect(const QUrl &url, int statusCode = Tf::Found);
95  bool sendFile(const QString &filePath, const QByteArray &contentType, const QString &name = QString(), bool autoRemove = false);
96  bool sendData(const QByteArray &data, const QByteArray &contentType, const QString &name = QString());
97  void rollbackTransaction() { _rollback = true; }
98  void setAutoRemove(const QString &filePath);
99  bool validateAccess(const TAbstractUser *user);
100  int socketId() const { return _sockId; }
101  // For WebSocket
102  void sendTextToWebSocket(int socketId, const QString &text);
103  void sendBinaryToWebSocket(int socketId, const QByteArray &binary);
104  void closeWebSokcet(int socketId, int closeCode = Tf::NormalClosure);
105  void publish(const QString &topic, const QString &text);
106  void publish(const QString &topic, const QByteArray &binary);
107 
108  virtual bool userLogin(const TAbstractUser *user);
109  virtual void userLogout();
110  virtual void setAccessRules() {}
111 
114  THttpResponse &httpResponse() { return _response; }
115 
116 private:
117  enum WebSocketTaskType {
118  SendTextTo,
119  SendBinaryTo,
120  SendCloseTo,
121  PublishText,
122  PublishBinary,
123  };
124 
125  void setActionName(const QString &name);
126  void setArguments(const QStringList &arguments) { _args = arguments; }
127  void setSocketId(int socketId) { _sockId = socketId; }
128  bool verifyRequest(const THttpRequest &request) const;
129  QByteArray renderView(TActionView *view);
130  void exportAllFlashVariants();
131  const TActionController *controller() const override { return this; }
132  bool rollbackRequested() const { return _rollback; }
133  static QString layoutClassName(const QString &layout);
134  static QString partialViewClassName(const QString &partial);
135 
136  mutable QString _ctrlName;
137  QString _actionName;
138  QStringList _args;
139  int _statCode {Tf::OK}; // 200 OK
140  bool _rendered {false};
141  bool _layoutEnable {true};
142  QString _layoutName;
143  THttpResponse _response;
144  QVariantMap _flashVars;
145  TSession _sessionStore;
146  TCookieJar _cookieJar;
147  bool _rollback {false};
148  QStringList _autoRemoveFiles;
149  QList<QPair<int, QVariant>> _taskList;
150  int _sockId {0};
151 
152  friend class TActionContext;
153  friend class TSessionCookieStore;
156 };
157 
158 
159 inline QString TActionController::activeAction() const
160 {
161  return _actionName;
162 }
163 
164 inline QStringList TActionController::arguments() const
165 {
166  return _args;
167 }
168 
169 inline void TActionController::setActionName(const QString &name)
170 {
171  _actionName = name;
172 }
173 
174 inline void TActionController::setLayoutEnabled(bool enable)
175 {
176  _layoutEnable = enable;
177 }
178 
179 inline void TActionController::setLayoutDisabled(bool disable)
180 {
181  _layoutEnable = !disable;
182 }
183 
185 {
186  return _layoutEnable;
187 }
188 
189 inline QString TActionController::layout() const
190 {
191  return _layoutName;
192 }
193 
194 inline void TActionController::setStatusCode(int code)
195 {
196  _statCode = code;
197 }
198 
199 inline QString TActionController::flash(const QString &name) const
200 {
201  return _flashVars.value(name).toString();
202 }
203 
204 inline QByteArray TActionController::contentType() const
205 {
206  return _response.header().contentType();
207 }
208 
209 inline void TActionController::setContentType(const QByteArray &type)
210 {
211  _response.header().setContentType(type);
212 }
213 
TActionController::socketId
int socketId() const
Definition: tactioncontroller.h:100
TActionController::activeAction
QString activeAction() const override
Definition: tactioncontroller.h:159
TActionController::transactionEnabled
virtual bool transactionEnabled() const
Definition: tactioncontroller.h:38
T_DISABLE_COPY
#define T_DISABLE_COPY(Class)
Definition: tglobal.h:250
TAbstractController::getRenderingData
virtual QString getRenderingData(const QString &templateName, const QVariantMap &vars=QVariantMap())=0
TAccessValidator
The TAccessValidator class provides the functionality of a validation for form data.
Definition: taccessvalidator.h:10
Tf::OK
@ OK
Definition: tfnamespace.h:38
TActionController::name
QString name() const override
Returns the controller name.
Definition: tactioncontroller.cpp:60
TActionController::httpResponse
THttpResponse & httpResponse()
Definition: tactioncontroller.h:114
TActionController::csrfProtectionEnabled
virtual bool csrfProtectionEnabled() const
Definition: tactioncontroller.h:36
TFormValidator
The TFormValidator class provides form validation for a map-table-based dictionary.
Definition: tformvalidator.h:9
TActionController
The TActionController class is the base class of all action controllers.
Definition: tactioncontroller.h:21
TActionController::layout
QString layout() const
Definition: tactioncontroller.h:189
THttpResponse
The THttpResponse class contains response information for HTTP.
Definition: thttpresponse.h:10
TActionController::setLayoutDisabled
void setLayoutDisabled(bool disable)
Definition: tactioncontroller.h:179
TAbstractController::activeAction
virtual QString activeAction() const =0
TInternetMessageHeader::contentType
QByteArray contentType() const
Returns the value of the header field content-type.
Definition: tinternetmessageheader.cpp:115
TAbstractController::addCookie
virtual bool addCookie(const TCookie &cookie)=0
TAbstractController::arguments
virtual QStringList arguments() const =0
TActionController::preFilter
virtual bool preFilter()
Definition: tactioncontroller.h:52
TActionController::response
const THttpResponse & response() const
Definition: tactioncontroller.h:31
TInternetMessageHeader::setContentType
void setContentType(const QByteArray &type)
Sets the value of the header field content-type to type.
Definition: tinternetmessageheader.cpp:123
TActionController::setAccessRules
virtual void setAccessRules()
Definition: tactioncontroller.h:110
TActionController::exceptionActionsOfCsrfProtection
virtual QStringList exceptionActionsOfCsrfProtection() const
Definition: tactioncontroller.h:37
TActionController::httpRequest
const THttpRequest & httpRequest() const override
Definition: tactioncontroller.h:30
TActionController::session
const TSession & session() const override
Definition: tactioncontroller.h:33
TAbstractController::request
virtual const THttpRequest & request() const =0
TActionController::arguments
QStringList arguments() const override
Definition: tactioncontroller.h:164
TActionController::postFilter
virtual void postFilter()
Definition: tactioncontroller.h:53
Tf::NormalClosure
@ NormalClosure
Definition: tfnamespace.h:208
T_DISABLE_MOVE
#define T_DISABLE_MOVE(Class)
Definition: tglobal.h:254
TActionController::session
TSession & session() override
Definition: tactioncontroller.h:62
TAbstractController::name
virtual QString name() const =0
TActionController::contentType
QByteArray contentType() const
Definition: tactioncontroller.h:204
TAbstractController
The TAbstractController class is the abstract base class of controllers, providing functionality comm...
Definition: tabstractcontroller.h:12
TActionHelper::controller
virtual const TAbstractController * controller() const =0
TAbstractUser
The TAbstractUser class is the abstract base class of users, providing functionality common to users.
Definition: tabstractuser.h:6
TActionController::sessionEnabled
virtual bool sessionEnabled() const
Definition: tactioncontroller.h:35
TActionController::setLayoutEnabled
void setLayoutEnabled(bool enable)
Definition: tactioncontroller.h:174
TActionController::flash
QString flash(const QString &name) const
Definition: tactioncontroller.h:199
TCookieJar
The TCookieJar class holds network cookies.
Definition: tcookiejar.h:12
Tf::Found
@ Found
Definition: tfnamespace.h:48
THttpRequest
The THttpRequest class contains request information for HTTP.
Definition: thttprequest.h:33
TAbstractController::authenticityToken
virtual QByteArray authenticityToken() const =0
TActionController::layoutEnabled
bool layoutEnabled() const
Definition: tactioncontroller.h:184
TActionController::httpResponse
const THttpResponse & httpResponse() const
Definition: tactioncontroller.h:32
TAbstractController::setFlash
virtual void setFlash(const QString &name, const QVariant &value)=0
TActionController::rollbackTransaction
void rollbackTransaction()
Definition: tactioncontroller.h:97
TActionHelper
The TActionHelper class is the base class of all helpers.
Definition: tactionhelper.h:12
TActionController::setContentType
void setContentType(const QByteArray &type)
Definition: tactioncontroller.h:209
TActionView
The TActionView class is the abstract base class of views, providing functionality common to view.
Definition: tactionview.h:15
THttpResponse::header
THttpResponseHeader & header()
Definition: thttpresponse.h:16
TCookie
The TCookie class holds one network cookie.
Definition: tcookie.h:7
TActionController::statusCode
int statusCode() const
Definition: tactioncontroller.h:60
TActionController::httpRequest
THttpRequest & httpRequest()
Definition: tactioncontroller.h:113
T_CORE_EXPORT
#define T_CORE_EXPORT
Definition: tglobal.h:67
TCache
The TCache class stores items so that can be served faster.
Definition: tcache.h:7
TActionController::setStatusCode
void setStatusCode(int code)
Definition: tactioncontroller.h:194
TSession
The TSession class holds information associated with individual visitors. This class inherits QVarian...
Definition: tsession.h:7