-
-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathtactioncontroller.cpp
More file actions
1137 lines (963 loc) · 29.5 KB
/
tactioncontroller.cpp
File metadata and controls
1137 lines (963 loc) · 29.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2010-2019, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/
#include "tsessionmanager.h"
#include "ttextview.h"
#include <QCryptographicHash>
#include <QDir>
#include <QDomDocument>
#include <QFile>
#include <QMetaMethod>
#include <QMetaType>
#include <QMutexLocker>
#include <QTextStream>
#include <TAbstractUser>
#include <TActionContext>
#include <TActionController>
#include <TActionView>
#include <TAppSettings>
#include <TCache>
#include <TDispatcher>
#include <TFormValidator>
#include <TSession>
#include <QMessageAuthenticationCode>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <TWebApplication>
#include <QStringEncoder>
const QString QUEUED_FLASH_SESSION_KEY("_flashVariants");
const QString FLASH_VARS_SESSION_KEY("_activeFlash");
const QString LOGIN_USER_NAME_KEY("_loginUserName");
const QByteArray DEFAULT_CONTENT_TYPE("text/html");
/*!
\class TActionController
\brief The TActionController class is the base class of all action
controllers.
*/
/*!
\brief Constructor.
*/
TActionController::TActionController() :
TAbstractController()
{
// Default content type
setContentType(DEFAULT_CONTENT_TYPE);
}
/*!
\brief Destructor.
*/
TActionController::~TActionController()
{
}
/*!
Returns the controller name.
*/
QString TActionController::name() const
{
static const QString Postfix = "Controller";
if (_ctrlName.isEmpty()) {
_ctrlName = className();
if (_ctrlName.endsWith(Postfix)) {
_ctrlName.resize(_ctrlName.length() - Postfix.length());
}
}
return _ctrlName;
}
/*!
\fn QString TActionController::activeAction() const
Returns the active action name.
*/
/*!
Returns the HTTP request being executed.
*/
const THttpRequest &TActionController::request() const
{
return context()->httpRequest();
}
/*!
Returns the HTTP request being executed.
*/
THttpRequest &TActionController::request()
{
return context()->httpRequest();
}
/*!
\fn const THttpResponse &TActionController::httpResponse() const;
Returns a HTTP response to be sent.
*/
/*!
\fn THttpResponse &TActionController::httpResponse();
Returns a HTTP response to be sent.
*/
/*!
Sets the layout template to \a layout.
\sa layout()
*/
void TActionController::setLayout(const QString &layout)
{
if (!layout.isNull()) {
_layoutName = layout;
}
}
/*!
Adds the cookie to the internal list of cookies.
*/
bool TActionController::addCookie(const TCookie &cookie)
{
QByteArray name = cookie.name();
if (name.isEmpty() || name.contains(';') || name.contains(',') || name.contains(' ') || name.contains('\"')) {
Tf::error("Invalid cookie name: {}", (const char *)name.data());
return false;
}
_cookieJar.addCookie(cookie);
_response.header().removeAllRawHeaders("Set-Cookie");
for (auto &ck : (const QList<TCookie> &)_cookieJar.allCookies()) {
_response.header().addRawHeader("Set-Cookie", ck.toRawForm(QNetworkCookie::Full));
}
return true;
}
/*!
Adds the cookie to the internal list of cookies.
*/
bool TActionController::addCookie(const QByteArray &name, const QByteArray &value, const QDateTime &expire,
const QString &path, const QString &domain, bool secure, bool httpOnly,
const QByteArray &sameSite)
{
TCookie cookie(name, value);
cookie.setExpirationDate(expire);
cookie.setPath(path);
cookie.setDomain(domain);
cookie.setSecure(secure);
cookie.setHttpOnly(httpOnly);
cookie.setSameSite(sameSite);
return addCookie(cookie);
}
bool TActionController::addCookie(const QByteArray &name, const QByteArray &value, int64_t maxAge, const QString &path,
const QString &domain, bool secure, bool httpOnly, const QByteArray &sameSite)
{
TCookie cookie(name, value);
cookie.setMaxAge(maxAge);
if (maxAge > 0) {
cookie.setExpirationDate(QDateTime::currentDateTime().addSecs(maxAge)); // For IE11
}
cookie.setPath(path);
cookie.setDomain(domain);
cookie.setSecure(secure);
cookie.setHttpOnly(httpOnly);
cookie.setSameSite(sameSite);
return addCookie(cookie);
}
/*!
Returns the authenticity token.
*/
QByteArray TActionController::authenticityToken() const
{
if (TSessionManager::instance().storeType() == QLatin1String("cookie")) {
QString key = TSessionManager::instance().csrfProtectionKey();
QByteArray csrfId = session().value(key).toByteArray();
if (csrfId.isEmpty()) {
if (Tf::appSettings()->value(Tf::EnableCsrfProtectionModule).toBool() && csrfProtectionEnabled()) {
// Throw an exceptioon if CSRF is enabled
throw RuntimeException("CSRF protectionsession value is empty", __FILE__, __LINE__);
}
}
return csrfId;
} else {
QByteArray key = Tf::appSettings()->value(Tf::SessionSecret).toByteArray();
return QMessageAuthenticationCode::hash(session().id(), key, QCryptographicHash::Sha3_256).toHex();
}
}
/*!
Sets the HTTP session to \a session.
*/
void TActionController::setSession(const TSession &session)
{
_sessionStore = session;
}
/*!
Sets CSRF protection informaion into \a session. Internal use.
*/
void TActionController::setCsrfProtectionInto(TSession &session)
{
if (TSessionManager::instance().storeType() == QLatin1String("cookie")) {
QString key = TSessionManager::instance().csrfProtectionKey();
QByteArray val = session.value(key).toByteArray();
if (val.isEmpty()) {
session.insert(key, TSessionManager::instance().generateId()); // it's just a random value
}
}
}
/*!
Returns the list of all available controllers.
*/
const QStringList &TActionController::availableControllers()
{
static QStringList controllers = []() {
QStringList controllers;
for (auto it = Tf::objectFactories()->cbegin(); it != Tf::objectFactories()->cend(); ++it) {
if (it.key().endsWith("controller")) {
controllers << QString::fromLatin1(it.key());
}
}
std::sort(controllers.begin(), controllers.end());
return controllers;
}();
return controllers;
}
const QStringList &TActionController::disabledControllers()
{
static const QStringList disabledNames = {"application"};
return disabledNames;
}
QString TActionController::loginUserNameKey()
{
return LOGIN_USER_NAME_KEY;
}
/*!
Verifies the HTTP request \a request.
*/
bool TActionController::verifyRequest(const THttpRequest &request) const
{
if (!csrfProtectionEnabled()) {
tSystemWarn("Skipped verifying authenticity token : {}", request.header().path().data());
return true;
}
if (TSessionManager::instance().storeType() != QLatin1String("cookie")) {
if (session().id().isEmpty()) {
throw SecurityException("Request Forgery Protection requires a valid session", __FILE__, __LINE__);
}
}
QByteArray postAuthToken = request.parameter("authenticity_token").toLatin1();
if (postAuthToken.isEmpty()) {
throw SecurityException("Authenticity token is empty", __FILE__, __LINE__);
}
tSystemDebug("postAuthToken: {}", (const char*)postAuthToken.data());
bool res = Tf::strcmp(postAuthToken, authenticityToken());
if (res) {
tSystemDebug("Verified authenticity token : {}", request.header().path().data());
}
return res;
}
/*!
Renders the template of the action \a action with the layout \a layout.
*/
bool TActionController::render(const QString &action, const QString &layout)
{
if ((int)_rendered > 0) {
Tf::warn("Has rendered already: {}", (className() + '.' + activeAction()));
return false;
}
_rendered = RenderState::Rendered;
// Creates view-object and displays it
TDispatcher<TActionView> viewDispatcher(viewClassName(action));
setLayout(layout);
_response.setBody(renderView(viewDispatcher.object()));
return !response().isBodyNull();
}
/*!
Renders the template given by \a templateName with the layout \a layout.
*/
bool TActionController::renderTemplate(const QString &templateName, const QString &layout)
{
if ((int)_rendered > 0) {
Tf::warn("Has rendered already: {}", (className() + '#' + activeAction()));
return false;
}
_rendered = RenderState::Rendered;
// Creates view-object and displays it
QStringList names = templateName.split("/");
if (names.count() != 2) {
Tf::error("Invalid patameter: {}", templateName);
return false;
}
TDispatcher<TActionView> viewDispatcher(viewClassName(names[0], names[1]));
setLayout(layout);
_response.setBody(renderView(viewDispatcher.object()));
return (!response().isBodyNull());
}
/*!
Renders the text \a text with the layout \a layout.
*/
bool TActionController::renderText(const QString &text, bool layoutEnable, const QString &layout)
{
if ((int)_rendered > 0) {
Tf::warn("Has rendered already: {}", (className() + '#' + activeAction()));
return false;
}
_rendered = RenderState::Rendered;
if (contentType() == DEFAULT_CONTENT_TYPE) {
setContentType(QByteArrayLiteral("text/plain"));
}
// Creates TTextView object and displays it
setLayout(layout);
setLayoutEnabled(layoutEnable);
TTextView *view = new TTextView(text);
_response.setBody(renderView(view));
delete view;
return (!_response.isBodyNull());
}
static QDomElement createDomElement(const QString &name, const QVariantMap &map, QDomDocument &document)
{
QDomElement element = document.createElement(name);
for (auto it = map.begin(); it != map.end(); ++it) {
QDomElement tag = document.createElement(it.key());
element.appendChild(tag);
QDomText text = document.createTextNode(it.value().toString());
tag.appendChild(text);
}
return element;
}
/*!
Renders the XML document \a document.
*/
bool TActionController::renderXml(const QDomDocument &document)
{
QByteArray xml;
QTextStream ts(&xml);
ts.setEncoding(QStringConverter::Utf8);
document.save(ts, 1, QDomNode::EncodingFromTextStream);
return sendData(xml, "text/xml");
}
/*!
Renders the \a map as XML document.
*/
bool TActionController::renderXml(const QVariantMap &map)
{
QDomDocument doc;
QDomElement root = doc.createElement("map");
doc.appendChild(root);
root.appendChild(createDomElement("map", map, doc));
return renderXml(doc);
}
/*!
Renders the list of variants \a list as XML document.
*/
bool TActionController::renderXml(const QVariantList &list)
{
QDomDocument doc;
QDomElement root = doc.createElement("list");
doc.appendChild(root);
for (auto &var : list) {
QVariantMap map = var.toMap();
root.appendChild(createDomElement("map", map, doc));
}
return renderXml(doc);
}
/*!
Renders the list of strings \a list as XML document.
*/
bool TActionController::renderXml(const QStringList &list)
{
QDomDocument doc;
QDomElement root = doc.createElement("list");
doc.appendChild(root);
for (auto &str : list) {
QDomElement tag = doc.createElement("string");
root.appendChild(tag);
QDomText text = doc.createTextNode(str);
tag.appendChild(text);
}
return renderXml(doc);
}
/*!
Renders the template of the \a action with the \a layout and caches it with
the \a key for \a seconds.
To use this function, enable cache module in application.ini.
\sa render()
\sa renderOnCache()
\sa removeCache()
*/
bool TActionController::renderAndCache(const QByteArray &key, int seconds, const QString &action, const QString &layout)
{
if ((int)_rendered > 0) {
Tf::warn("Has rendered already: {}", (className() + '.' + activeAction()));
return false;
}
render(action, layout);
if ((int)_rendered > 0) {
QByteArray responseMsg = response().body();
Tf::cache()->set(key, responseMsg, seconds);
}
return (bool)_rendered;
}
/*!
Renders the template cached with the \a key. If no item with the \a key
found, returns false.
To use this function, enable cache module in application.ini.
\sa renderAndCache()
*/
bool TActionController::renderOnCache(const QByteArray &key)
{
if ((int)_rendered > 0) {
Tf::warn("Has rendered already: {}", (className() + '.' + activeAction()));
return false;
}
auto responseMsg = Tf::cache()->get(key);
if (responseMsg.isEmpty()) {
return false;
}
_response.setBody(responseMsg);
_rendered = RenderState::Rendered;
return (bool)_rendered;
}
/*!
Removes the template with the \a key from the cache.
\sa renderAndCache()
\sa renderOnCache()
*/
void TActionController::removeCache(const QByteArray &key)
{
Tf::cache()->remove(key);
}
/*!
Returns the rendering data of the partial template given by \a templateName.
*/
QString TActionController::getRenderingData(const QString &templateName, const QVariantMap &vars)
{
// Creates view-object
QStringList names = templateName.split("/");
if (names.count() != 2) {
Tf::error("Invalid patameter: {}", templateName);
return QString();
}
TDispatcher<TActionView> viewDispatcher(viewClassName(names[0], names[1]));
TActionView *view = viewDispatcher.object();
if (!view) {
return QString();
}
QVariantMap map = allVariants();
for (auto it = vars.begin(); it != vars.end(); ++it) {
map.insert(it.key(), it.value()); // item's value of same key is replaced
}
view->setController(this);
view->setVariantMap(map);
return view->toString();
}
/*!
Renders the \a view view.
*/
QByteArray TActionController::renderView(TActionView *view)
{
if (!view) {
tSystemError("view null pointer. action:{}", activeAction());
return QByteArray();
}
view->setController(this);
view->setVariantMap(allVariants());
if (!layoutEnabled()) {
// Renders without layout
tSystemDebug("Renders without layout");
return QStringEncoder(Tf::app()->encodingForHttpOutput()).encode(view->toString());
}
// Displays with layout
QString lay = (layout().isNull()) ? name().toLower() : layout().toLower();
TDispatcher<TActionView> layoutDispatcher(layoutClassName(lay));
TActionView *layoutView = layoutDispatcher.object();
TDispatcher<TActionView> defLayoutDispatcher(layoutClassName(QLatin1String("application")));
if (!layoutView) {
if (!layout().isNull()) {
tSystemDebug("Not found layout: {}", layout());
return QByteArray();
} else {
// Use default layout
layoutView = defLayoutDispatcher.object();
if (!layoutView) {
tSystemDebug("Not found default layout. Renders without layout.");
return QStringEncoder(Tf::app()->encodingForHttpOutput()).encode(view->toString());
}
}
}
// Renders layout
layoutView->setVariantMap(allVariants());
layoutView->setController(this);
layoutView->setSubActionView(view);
return QStringEncoder(Tf::app()->encodingForHttpOutput()).encode(layoutView->toString());
}
/*!
Renders a static error page with the status code, which page is [statusCode].html
in the \a public directory.
*/
bool TActionController::renderErrorResponse(int statusCode)
{
bool ret = false;
if ((int)_rendered > 0) {
Tf::warn("Has rendered already: {}", (className() + '#' + activeAction()));
return ret;
}
QString file = Tf::app()->publicPath() + QString::number(statusCode) + QLatin1String(".html");
if (QFileInfo(file).exists()) {
ret = sendFile(file, "text/html", "", false);
} else {
_response.setBody("");
}
setStatusCode(statusCode);
_rendered = RenderState::Rendered;
return ret;
}
/*!
Returns the layout class name. Internal use.
*/
QString TActionController::layoutClassName(const QString &layout)
{
return QLatin1String("layouts_") + layout + QLatin1String("View");
}
/*!
Returns the class name of the partial view. Internal use.
*/
QString TActionController::partialViewClassName(const QString &partial)
{
return QLatin1String("partial_") + partial + QLatin1String("View");
}
/*!
Redirects to the URL \a url.
*/
void TActionController::redirect(const QUrl &url, int statusCode)
{
if ((int)_rendered > 0) {
Tf::error("Unable to redirect. Has rendered already: {}", (className() + '#' + activeAction()));
return;
}
_rendered = RenderState::Rendered;
setStatusCode(statusCode);
_response.header().setRawHeader("Location", url.toEncoded());
_response.setBody(QByteArray("<html><body>redirected.</body></html>"));
setContentType("text/html");
// Enable flash-variants
QVariant var;
var.setValue(_flashVars);
_sessionStore.insert(QUEUED_FLASH_SESSION_KEY, var);
}
/*!
Sends the file \a filePath as HTTP response.
*/
bool TActionController::sendFile(const QString &filePath, const QByteArray &contentType, const QString &name, bool autoRemove)
{
if ((int)_rendered > 0) {
Tf::warn("Has rendered already: {}", (className() + '#' + activeAction()));
return false;
}
_rendered = RenderState::Rendered;
if (!name.isEmpty()) {
QByteArray filename;
filename += "attachment; filename=\"";
filename += name.toUtf8();
filename += '"';
_response.header().setRawHeader("Content-Disposition", filename);
}
_response.setBodyFile(filePath);
setContentType(contentType);
if (autoRemove) {
setAutoRemove(filePath);
}
return true;
}
/*!
Sends the data \a data as HTTP response.
*/
bool TActionController::sendData(const QByteArray &data, const QByteArray &contentType, const QString &name)
{
if ((int)_rendered > 0) {
Tf::warn("Has rendered already: {}", (className() + '#' + activeAction()));
return false;
}
_rendered = RenderState::Rendered;
if (!name.isEmpty()) {
QByteArray filename;
filename += "attachment; filename=\"";
filename += name.toUtf8();
filename += '"';
_response.header().setRawHeader("Content-Disposition", filename);
}
_response.setBody(data);
setContentType(contentType);
return true;
}
/*!
Exports the all flash variants.
*/
void TActionController::exportAllFlashVariants()
{
_sessionStore.remove(FLASH_VARS_SESSION_KEY);
QVariant var = _sessionStore.take(QUEUED_FLASH_SESSION_KEY);
if (!var.isNull()) {
exportVariants(var.toMap());
_sessionStore.insert(FLASH_VARS_SESSION_KEY, var);
}
}
QVariantMap TActionController::flashVariants() const
{
return _sessionStore.value(FLASH_VARS_SESSION_KEY).toMap();
}
QVariant TActionController::flashVariant(const QString &key) const
{
return _sessionStore.value(FLASH_VARS_SESSION_KEY).toMap().value(key);
}
QJsonObject TActionController::flashVariantsJson() const
{
return QJsonObject::fromVariantMap(flashVariants());
}
QJsonObject TActionController::flashVariantJson(const QString &key) const
{
return QJsonObject::fromVariantMap(flashVariant(key).toMap());
}
/*!
Validates the access of the user \a user. Returns true if the user
access is allowed by rule; otherwise returns false.
@sa setAccessRules(), TAccessValidator::validate()
*/
bool TActionController::validateAccess(const TAbstractUser *user)
{
if (TAccessValidator::accessRules.isEmpty()) {
setAccessRules();
}
return TAccessValidator::validate(user, this);
}
/*!
Logs the user \a user in to the system.
This is a virtual function.
\sa userLogout()
*/
bool TActionController::userLogin(const TAbstractUser *user)
{
if (!user) {
tSystemError("userLogin: null specified");
return false;
}
if (user->identityKey().isEmpty()) {
tSystemError("userLogin: identityKey empty");
return false;
}
if (isUserLoggedIn()) {
tSystemWarn("userLogin: Duplicate login detected. Force logout [user:{}]", identityKeyOfLoginUser());
}
session().insert(LOGIN_USER_NAME_KEY, user->identityKey());
return true;
}
/*!
Logs out of the system.
This is a virtual function.
\sa userLogin()
*/
void TActionController::userLogout()
{
session().take(LOGIN_USER_NAME_KEY);
}
/*!
Returns true if a user is logged in to the system; otherwise returns false.
This is a virtual function.
\sa userLogin()
*/
bool TActionController::isUserLoggedIn() const
{
return session().contains(LOGIN_USER_NAME_KEY);
}
/*!
Returns the identity key of the user, i.e., TAbstractUser object,
logged in.
This is a virtual function.
\sa userLogin()
*/
QString TActionController::identityKeyOfLoginUser() const
{
return session().value(LOGIN_USER_NAME_KEY).toString();
}
/*!
Sets the automatically removing file.
The file \a filePath is removed when the context is extinguished,
after replied the HTTP response.
*/
void TActionController::setAutoRemove(const QString &filePath)
{
if (!filePath.isEmpty() && !_autoRemoveFiles.contains(filePath)) {
_autoRemoveFiles << filePath;
}
}
/*!
Returns the client address of the current session.
*/
QHostAddress TActionController::clientAddress() const
{
return context()->clientAddress();
}
/*!
Sets the flash message of \a name to \a value.
\sa flash()
*/
void TActionController::setFlash(const QString &name, const QVariant &value)
{
if (value.isValid()) {
_flashVars.insert(name, value);
} else {
tSystemWarn("An invalid QVariant object for setFlash(), name:{}", name);
}
}
/*!
Sets the validation errors to flash variant.
*/
void TActionController::setFlashValidationErrors(const TFormValidator &v, const QString &prefix)
{
for (auto &key : (const QStringList &)v.validationErrorKeys()) {
QString msg = v.errorMessage(key);
setFlash(prefix + key, msg);
}
}
void TActionController::sendTextToWebSocket(int socket, const QString &text)
{
QVariantList info;
info << socket << text;
_taskList << qMakePair((int)SendTextTo, QVariant(info));
}
void TActionController::sendBinaryToWebSocket(int socket, const QByteArray &binary)
{
QVariantList info;
info << socket << binary;
_taskList << qMakePair((int)SendBinaryTo, QVariant(info));
}
void TActionController::closeWebSokcet(int socket, int closeCode)
{
QVariantList info;
info << socket << closeCode;
_taskList << qMakePair((int)SendCloseTo, QVariant(info));
}
void TActionController::publish(const QString &topic, const QString &text)
{
QVariantList info;
info << topic << text;
_taskList << qMakePair((int)PublishText, QVariant(info));
}
void TActionController::publish(const QString &topic, const QByteArray &binary)
{
QVariantList info;
info << topic << binary;
_taskList << qMakePair((int)PublishBinary, QVariant(info));
}
/*!
Sends a response immediately, and then allows time-consuming processing to
continue in the controller.
*/
void TActionController::flushResponse()
{
if (_rendered == RenderState::Rendered) {
context()->flushResponse(this, true);
_rendered = RenderState::DataSent;
}
}
void TActionController::reset()
{
TAccessValidator::clear();
_ctrlName.clear();
_actionName.clear();
_args.clear();
_statCode = Tf::OK; // 200 OK
_rendered = RenderState::NotRendered;
_layoutEnable = true;
_layoutName.clear();
_response.clear();
_flashVars.clear();
_sessionStore.reset();
_cookieJar.clear();
_rollback = false;
_autoRemoveFiles.clear();
_taskList.clear();
}
/*!
Renders the JSON document \a document as HTTP response.
*/
bool TActionController::renderJson(const QJsonDocument &document)
{
return sendData(document.toJson(QJsonDocument::Compact), "application/json; charset=utf-8");
}
/*!
Renders the JSON object \a object as HTTP response.
*/
bool TActionController::renderJson(const QJsonObject &object)
{
return renderJson(QJsonDocument(object));
}
/*!
Renders the JSON array \a array as HTTP response.
*/
bool TActionController::renderJson(const QJsonArray &array)
{
return renderJson(QJsonDocument(array));
}
/*!
Renders the \a map as a JSON object.
*/
bool TActionController::renderJson(const QVariantMap &map)
{
return renderJson(QJsonObject::fromVariantMap(map));
}
/*!
Renders the \a list as a JSON array.
*/
bool TActionController::renderJson(const QVariantList &list)
{
return renderJson(QJsonArray::fromVariantList(list));
}
/*!
Renders the \a list as a JSON array.
*/
bool TActionController::renderJson(const QStringList &list)
{
return renderJson(QJsonArray::fromStringList(list));
}
/*!
Renders a CBOR object \a variant as HTTP response.
*/
bool TActionController::renderCbor(const QVariant &variant, QCborValue::EncodingOptions opt)
{
return renderCbor(QCborValue::fromVariant(variant), opt);
}
/*!
Renders a CBOR object \a map as HTTP response.
*/
bool TActionController::renderCbor(const QVariantMap &map, QCborValue::EncodingOptions opt)
{
return renderCbor(QCborMap::fromVariantMap(map), opt);
}
/*!
Renders a CBOR object \a hash as HTTP response.
*/
bool TActionController::renderCbor(const QVariantHash &hash, QCborValue::EncodingOptions opt)
{
return renderCbor(QCborMap::fromVariantHash(hash), opt);
}
/*!
Renders a CBOR \a value as HTTP response.
*/
bool TActionController::renderCbor(const QCborValue &value, QCborValue::EncodingOptions opt)
{
QCborValue val = value;
return sendData(val.toCbor(opt), "application/cbor");
}
/*!
Renders a CBOR \a map as HTTP response.
*/
bool TActionController::renderCbor(const QCborMap &map, QCborValue::EncodingOptions opt)
{
return renderCbor(map.toCborValue(), opt);
}
/*!
Renders a CBOR \a array as HTTP response.
*/
bool TActionController::renderCbor(const QCborArray &array, QCborValue::EncodingOptions opt)
{
return renderCbor(array.toCborValue(), opt);
}
/*!
\fn const TSession &TActionController::session() const;
Returns the current HTTP session, allows associating information
with individual visitors.
\sa setSession(), sessionEnabled()
*/
/*!
\fn TSession &TActionController::session();
Returns the current HTTP session, allows associating information
with individual visitors.
\sa setSession(), sessionEnabled()