TreeFrogFramework  1.18
tactioncontroller.h
Go to the documentation of this file.
1 #ifndef TACTIONCONTROLLER_H
2 #define TACTIONCONTROLLER_H
3 
4 #include <QObject>
5 #include <QString>
6 #include <QHostAddress>
7 #include <QDomDocument>
8 #include <TGlobal>
9 #include <TAbstractController>
10 #include <TActionHelper>
11 #include <THttpRequest>
12 #include <THttpResponse>
13 #include <TSession>
14 #include <TCookieJar>
15 #include <TAccessValidator>
16 #if QT_VERSION >= 0x050000
17 #include <QJsonDocument>
18 #include <QJsonObject>
19 #include <QJsonArray>
20 #include <QJsonValue>
21 #endif
22 
23 class TActionView;
24 class TAbstractUser;
25 class TFormValidator;
26 
27 
28 class T_CORE_EXPORT TActionController : public QObject, public TAbstractController, public TActionHelper, protected TAccessValidator
29 {
30 public:
32  virtual ~TActionController() { }
33 
34  QString className() const;
35  QString name() const;
36  QString activeAction() const;
37  const THttpRequest &httpRequest() const;
38  const THttpResponse &httpResponse() const { return response; }
39  QString getRenderingData(const QString &templateName, const QVariantMap &vars = QVariantMap());
40  const TSession &session() const { return sessionStore; }
41  virtual bool sessionEnabled() const { return true; }
42  virtual bool csrfProtectionEnabled() const { return true; }
43  virtual QStringList exceptionActionsOfCsrfProtection() const { return QStringList(); }
44  virtual bool transactionEnabled() const { return true; }
45  QByteArray authenticityToken() const;
46  QString flash(const QString &name) const;
47  QHostAddress clientAddress() const;
48  virtual bool isUserLoggedIn() const;
49  virtual QString identityKeyOfLoginUser() const;
50 
51  static void setCsrfProtectionInto(TSession &session);
52  static const QStringList &availableControllers();
53  static const QStringList &disabledControllers();
54  static QString loginUserNameKey();
55 
56 protected:
57  virtual bool preFilter() { return true; }
58  virtual void postFilter() { }
59  void setLayoutEnabled(bool enable);
60  void setLayoutDisabled(bool disable);
61  bool layoutEnabled() const;
62  void setLayout(const QString &layout);
63  QString layout() const;
64  void setStatusCode(int code);
65  int statusCode() const { return statCode; }
66  void setFlash(const QString &name, const QVariant &value);
67  void setFlashValidationErrors(const TFormValidator &validator, const QString &prefix = QString("err_"));
68  TSession &session() { return sessionStore; }
69  void setSession(const TSession &session);
70  bool addCookie(const TCookie &cookie);
71  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);
72  QByteArray contentType() const;
73  void setContentType(const QByteArray &type);
74  bool render(const QString &action = QString(), const QString &layout = QString());
75  bool renderTemplate(const QString &templateName, const QString &layout = QString());
76  bool renderText(const QString &text, bool layoutEnable = false, const QString &layout = QString());
77  bool renderXml(const QDomDocument &document);
78  bool renderXml(const QVariantMap &map);
79  bool renderXml(const QVariantList &list);
80  bool renderXml(const QStringList &list);
81 #if QT_VERSION >= 0x050000
82  bool renderJson(const QJsonDocument &document);
83  bool renderJson(const QJsonObject &object);
84  bool renderJson(const QJsonArray &array);
85  bool renderJson(const QVariantMap &map);
86  bool renderJson(const QVariantList &list);
87  bool renderJson(const QStringList &list);
88 #endif
89  bool renderErrorResponse(int statusCode);
90  void redirect(const QUrl &url, int statusCode = Tf::Found);
91  bool sendFile(const QString &filePath, const QByteArray &contentType, const QString &name = QString(), bool autoRemove = false);
92  bool sendData(const QByteArray &data, const QByteArray &contentType, const QString &name = QString());
93  void rollbackTransaction() { rollback = true; }
94  void setAutoRemove(const QString &filePath);
95  bool validateAccess(const TAbstractUser *user);
96  int socketId() const { return sockId; }
97  // For WebSocket
98  void sendTextToWebSocket(int sid, const QString &text);
99  void sendBinaryToWebSocket(int sid, const QByteArray &binary);
100  void closeWebSokcet(int sid, int closeCode = Tf::NormalClosure);
101 
102  virtual bool userLogin(const TAbstractUser *user);
103  virtual void userLogout();
104  virtual void setAccessRules() { }
105 
106  THttpRequest &httpRequest();
107  THttpResponse &httpResponse() { return response; }
108 
109 private:
110  enum WebSocketTaskType {
111  SendTextTo,
112  SendBinaryTo,
113  SendCloseTo,
114  };
115 
116  void setActionName(const QString &name);
117  void setSocketId(int sid) { sockId = sid; }
118  bool verifyRequest(const THttpRequest &request) const;
119  QByteArray renderView(TActionView *view);
120  void exportAllFlashVariants();
121  const TActionController *controller() const { return this; }
122  bool rollbackRequested() const { return rollback; }
123  static QString layoutClassName(const QString &layout);
124  static QString partialViewClassName(const QString &partial);
125 
126  mutable QString ctrlName;
127  QString actName;
128  int statCode;
129  bool rendered;
130  bool layoutEnable;
131  QString layoutName;
132  THttpResponse response;
133  QVariantMap flashVars;
134  TSession sessionStore;
135  TCookieJar cookieJar;
136  bool rollback;
137  QStringList autoRemoveFiles;
138  QList<QPair<int, QVariant>> taskList;
139  int sockId {0};
140 
141  friend class TActionContext;
142  friend class TSessionCookieStore;
143  friend class TDirectView;
146 };
147 
148 
149 inline QString TActionController::className() const
150 {
151  return QString(metaObject()->className());
152 }
153 
154 inline QString TActionController::activeAction() const
155 {
156  return actName;
157 }
158 
159 inline void TActionController::setActionName(const QString &name)
160 {
161  actName = name;
162 }
163 
164 inline void TActionController::setLayoutEnabled(bool enable)
165 {
166  layoutEnable = enable;
167 }
168 
169 inline void TActionController::setLayoutDisabled(bool disable)
170 {
171  layoutEnable = !disable;
172 }
173 
175 {
176  return layoutEnable;
177 }
178 
179 inline QString TActionController::layout() const
180 {
181  return layoutName;
182 }
183 
184 inline void TActionController::setStatusCode(int code)
185 {
186  statCode = code;
187 }
188 
189 inline QString TActionController::flash(const QString &name) const
190 {
191  return flashVars.value(name).toString();
192 }
193 
194 inline QByteArray TActionController::contentType() const
195 {
196  return response.header().contentType();
197 }
198 
199 inline void TActionController::setContentType(const QByteArray &type)
200 {
201  response.header().setContentType(type);
202 }
203 
204 #endif // TACTIONCONTROLLER_H
bool layoutEnabled() const
Returns true if the layout mechanism is enabled; otherwise returns false.
Definition: tactioncontroller.h:174
#define T_CORE_EXPORT
Definition: tglobal.h:81
The THttpResponse class contains response information for HTTP.
Definition: thttpresponse.h:12
The TAbstractController class is the abstract base class of controllers, providing functionality comm...
Definition: tabstractcontroller.h:10
int statusCode() const
Returns the status code of the HTTP response to be sent.
Definition: tactioncontroller.h:65
THttpResponse & httpResponse()
Returns a HTTP response to be sent.
Definition: tactioncontroller.h:107
const TSession & session() const
Returns the current HTTP session, allows associating information with individual visitors.
Definition: tactioncontroller.h:40
THttpResponseHeader & header()
Return the HTTP header.
Definition: thttpresponse.h:19
virtual QStringList exceptionActionsOfCsrfProtection() const
Must be overridden by subclasses to return a string list of actions excluded from CSRF protection whe...
Definition: tactioncontroller.h:43
const THttpResponse & httpResponse() const
Returns a HTTP response to be sent.
Definition: tactioncontroller.h:38
#define T_DISABLE_COPY(Class)
Definition: tglobal.h:137
QNetworkCookie TCookie
Definition: tcookiejar.h:9
#define T_DISABLE_MOVE(Class)
Definition: tglobal.h:141
void setLayoutDisabled(bool disable)
Disables the layout mechanism if disable is true, otherwise enables it.
Definition: tactioncontroller.h:169
QString flash(const QString &name) const
Returns the flash message for name.
Definition: tactioncontroller.h:189
virtual const TActionController * controller() const =0
virtual ~TActionController()
Destructor.
Definition: tactioncontroller.h:32
TSession & session()
Returns the current HTTP session, allows associating information with individual visitors.
Definition: tactioncontroller.h:68
virtual bool preFilter()
This function is called before actions on the controller are performed, therefore can be overridden b...
Definition: tactioncontroller.h:57
QByteArray contentType() const
Returns the content type for a response message.
Definition: tactioncontroller.h:194
virtual void setAccessRules()
Sets rules of access to this controller.
Definition: tactioncontroller.h:104
The TSession class holds information associated with individual visitors.
Definition: tsession.h:9
virtual bool csrfProtectionEnabled() const
Must be overridden by subclasses to disable CSRF protection.
Definition: tactioncontroller.h:42
virtual void postFilter()
This function is called after actions on the controller are performed.
Definition: tactioncontroller.h:58
The TActionView class is the abstract base class of views, providing functionality common to view...
Definition: tactionview.h:17
The THttpRequest class contains request information for HTTP.
Definition: thttprequest.h:38
QString activeAction() const
Returns the active action name.
Definition: tactioncontroller.h:154
void setContentType(const QByteArray &type)
Sets the value of the header field content-type to type.
Definition: tinternetmessageheader.cpp:126
QByteArray contentType() const
Returns the value of the header field content-type.
Definition: tinternetmessageheader.cpp:118
virtual bool sessionEnabled() const
Must be overridden by subclasses to enable a HTTP session.
Definition: tactioncontroller.h:41
The TFormValidator class provides form validation for a map-table-based dictionary.
Definition: tformvalidator.h:11
The TAccessValidator class provides validation of user access.
Definition: taccessvalidator.h:12
void setLayoutEnabled(bool enable)
Enables the layout mechanism if enable is true, otherwise disables it.
Definition: tactioncontroller.h:164
The TActionHelper class is the base class of all helpers.
Definition: tactionhelper.h:14
void rollbackTransaction()
This function is called to rollback a transaction on the database.
Definition: tactioncontroller.h:93
void setContentType(const QByteArray &type)
Sets the content type specified by type for a response message.
Definition: tactioncontroller.h:199
QString layout() const
Returns the name of the layout template.
Definition: tactioncontroller.h:179
virtual QString activeAction() const =0
This function is reimplemented in subclasses to return a active action name.
Definition: tfnamespace.h:203
int socketId() const
Definition: tactioncontroller.h:96
virtual bool transactionEnabled() const
Must be overridden by subclasses to disable transaction mechanism.
Definition: tactioncontroller.h:44
The TCookieJar class holds network cookies.
Definition: tcookiejar.h:16
The TActionController class is the base class of all action controllers.
Definition: tactioncontroller.h:28
The TAbstractUser class is the abstract base class of users, providing functionality common to users...
Definition: tabstractuser.h:8
Definition: tfnamespace.h:51
void setStatusCode(int code)
Sets the status code to code.
Definition: tactioncontroller.h:184
virtual QString name() const =0
This function is reimplemented in subclasses to return a controller name.