TreeFrogFramework 2.10
Loading...
Searching...
No Matches
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
14class TActionView;
15class TAbstractUser;
16class TFormValidator;
17class TCache;
18class QDomDocument;
19
20
22public:
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
52protected:
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 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 bool renderErrorResponse(int statusCode);
93 void redirect(const QUrl &url, int statusCode = Tf::Found);
94 bool sendFile(const QString &filePath, const QByteArray &contentType, const QString &name = QString(), bool autoRemove = false);
95 bool sendData(const QByteArray &data, const QByteArray &contentType, const QString &name = QString());
96 void rollbackTransaction() { _rollback = true; }
97 void setAutoRemove(const QString &filePath);
98 bool validateAccess(const TAbstractUser *user);
99 // For WebSocket
100 void sendTextToWebSocket(int socket, const QString &text);
101 void sendBinaryToWebSocket(int socket, const QByteArray &binary);
102 void closeWebSokcet(int socket, int closeCode = Tf::NormalClosure);
103 void publish(const QString &topic, const QString &text);
104 void publish(const QString &topic, const QByteArray &binary);
105 void flushResponse();
106
107 virtual bool userLogin(const TAbstractUser *user);
108 virtual void userLogout();
109 virtual void setAccessRules() {}
110
113 THttpResponse &httpResponse() { return _response; }
114
115private:
116 enum WebSocketTaskType {
117 SendTextTo = 0,
118 SendBinaryTo,
119 SendCloseTo,
120 PublishText,
121 PublishBinary,
122 };
123
124 enum class RenderState {
125 NotRendered = 0,
126 Rendered,
127 DataSent,
128 };
129
130 void setActionName(const QString &name);
131 void setArguments(const QStringList &arguments) { _args = arguments; }
132 bool verifyRequest(const THttpRequest &request) const;
133 QByteArray renderView(TActionView *view);
134 void exportAllFlashVariants();
135 const TActionController *controller() const override { return this; }
136 bool rollbackRequested() const { return _rollback; }
137 static QString layoutClassName(const QString &layout);
138 static QString partialViewClassName(const QString &partial);
139
140 mutable QString _ctrlName;
141 QString _actionName;
142 QStringList _args;
143 int _statCode {Tf::OK}; // 200 OK
144 RenderState _rendered {RenderState::NotRendered};
145 bool _layoutEnable {true};
146 QString _layoutName;
147 THttpResponse _response;
148 QVariantMap _flashVars;
149 TSession _sessionStore;
150 TCookieJar _cookieJar;
151 bool _rollback {false};
152 QStringList _autoRemoveFiles;
153 QList<QPair<int, QVariant>> _taskList;
154
155 friend class TActionContext;
156 friend class TSessionCookieStore;
159};
160
161
163{
164 return _actionName;
165}
166
167inline QStringList TActionController::arguments() const
168{
169 return _args;
170}
171
172inline void TActionController::setActionName(const QString &name)
173{
174 _actionName = name;
175}
176
178{
179 _layoutEnable = enable;
180}
181
183{
184 _layoutEnable = !disable;
185}
186
188{
189 return _layoutEnable;
190}
191
192inline QString TActionController::layout() const
193{
194 return _layoutName;
195}
196
198{
199 _statCode = code;
200}
201
202inline QString TActionController::flash(const QString &name) const
203{
204 return _flashVars.value(name).toString();
205}
206
207inline QByteArray TActionController::contentType() const
208{
209 return _response.header().contentType();
210}
211
212inline void TActionController::setContentType(const QByteArray &type)
213{
214 _response.header().setContentType(type);
215}
216
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:95
virtual void setFlash(const QString &name, const QVariant &value)
Definition tabstractcontroller.cpp:89
virtual bool addCookie(const TCookie &cookie)
Definition tabstractcontroller.cpp:125
virtual const THttpRequest & request() const
Definition tabstractcontroller.cpp:107
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:148
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
const THttpResponse & response() const
Definition tactioncontroller.h:31
void rollbackTransaction()
This function is called to rollback a transaction on the database.
Definition tactioncontroller.h:96
const THttpRequest & httpRequest() const override
Definition tactioncontroller.h:30
virtual void setAccessRules()
Sets rules of access to this controller.
Definition tactioncontroller.h:109
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:113
void setStatusCode(int code)
Sets the status code to code.
Definition tactioncontroller.h:197
QString activeAction() const override
Returns the active action name.
Definition tactioncontroller.h:162
bool layoutEnabled() const
Returns true if the layout mechanism is enabled; otherwise returns false.
Definition tactioncontroller.h:187
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
QString name() const override
Returns the controller name.
Definition tactioncontroller.cpp:64
QStringList arguments() const override
Definition tactioncontroller.h:167
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
QString flash(const QString &name) const
Returns the flash message for name.
Definition tactioncontroller.h:202
void setContentType(const QByteArray &type)
Sets the content type specified by type for a response message.
Definition tactioncontroller.h:212
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:182
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
void setLayoutEnabled(bool enable)
Enables the layout mechanism if enable is true, otherwise disables it.
Definition tactioncontroller.h:177
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:192
QByteArray contentType() const
Returns the content type for a response message.
Definition tactioncontroller.h:207
virtual bool csrfProtectionEnabled() const
Must be overridden by subclasses to disable CSRF protection.
Definition tactioncontroller.h:36
THttpRequest & httpRequest()
Definition tactioncontroller.h:112
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