-
Notifications
You must be signed in to change notification settings - Fork 1
/
NgxUser.hpp
305 lines (265 loc) · 7.6 KB
/
NgxUser.hpp
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
#ifndef NGXUSER_HPP
#define NGXUSER_HPP
#include "HeaderPrecompilation.hpp"
#include "vmime/vmime.hpp"
using DbInterface=std::function<rapidjson::Document(const char*,const char*,bool&)>;
namespace TestValidation
{
//提供基础功能
class TObject{
protected:
void setStatusAndMessage(rapidjson::Document&,bool,const char*);
rapidjson::Document _response;
//状态和信息
void setSAMFormatError(){
setStatusAndMessage(_response,false,"Client data format error!");
}
void setSAMSessionError(){
setStatusAndMessage(_response,false,"Session Expired!");
}
void setSAMDBError(){
setStatusAndMessage(_response,false,"Database execution error!");
}
void setSAMFileError(){
setStatusAndMessage(_response,false,"File operation failed!");
}
};
//请求基类
class BaseOp:public TObject
{
public:
//删除构造和拷贝构造函数
BaseOp() = delete;
BaseOp(const BaseOp &) = delete;
//将JSON字符串转换成JSON对象
BaseOp(const char *);
//注册和登录时需要判断用户是否存在
bool Existence();
//返回json对象
rapidjson::Document Ret() { return std::move(_response); };
//静态函数对象functor作为接口,处理数据库读取相关
static DbInterface BaseDao;
//上次更新时间
time_t GetLastSessionTime(const char*,const char*dbName="userinfo");
//执行状态
bool Status(){
if(_response.HasMember("status")){
return _response["status"].GetBool();
}
return false;
}
protected:
//只要登录就会更新sessionid和lastlogintime
bool UpdateSessionId(const char*);
bool UpdateLastLoginTime(const char *);
bool SessionExpired();
//工号和邮箱,常用值
std::string GetID();
std::string GetEMail();
time_t lastSessionTime;
//格式是否正确
bool validated = false;
rapidjson::Document _request;
//数据库执行状态
bool OpStatus=false;
};
//处理注册请求
class RegisterOp : public BaseOp
{
public:
RegisterOp() = delete;
RegisterOp(const RegisterOp &) = delete;
RegisterOp(const char *json) : BaseOp(json)
{
if (_request.HasMember("id") && //工号
_request.HasMember("password")/* && //密码
_request.HasMember("name") && //姓名
_request.HasMember("role") && //功能组
_request.HasMember("email") && //邮件
_request.HasMember("group")*/
) //联系方式
{
validated = true;
}
else
{
validated = false;
}
}
RegisterOp &Register();
};
//处理登录请求
class LoginOp : public BaseOp
{
public:
LoginOp()=delete;
LoginOp(const LoginOp &) = delete;
LoginOp(const char *json) : BaseOp(json)
{
{
if (_request.HasMember("id") &&
_request.HasMember("password"))
{
validated = true;
//获得数据库中上次登录时间
lastSessionTime = GetLastSessionTime("id");
}
else
{
validated = false;
}
}
}
LoginOp &Login();
};
//处理数据请求
class DataOp : public BaseOp
{
public:
DataOp() = delete;
DataOp(const DataOp &) = delete;
DataOp(const char *json) : BaseOp(json)
{
if( _request.HasMember("database") &&
_request.HasMember("sessionid")&&
_request.HasMember("statement"))
{
validated = true;
if (_request["sessionid"].GetString()!=std::string("registeronly")){
//获得数据库中上次登录时间
lastSessionTime = GetLastSessionTime("sessionid");
}
}else{
validated=false;
}
}
//读取数据
DataOp &Data();
};
//处理邮件请求
class MailOp:public BaseOp{
public:
MailOp()=delete;
MailOp(const MailOp&)=delete;
MailOp(const char* json):BaseOp(json){
sender.setName(vmime::text("iTEST工作室",vmime::charset("utf-8")));
sender.setEmail("[email protected]");
mb.setExpeditor(sender);
mb.setSubject(vmime::text("一封来自iTEST的邮件",vmime::charset("utf-8")));
}
//发送邮件
void SendMail();
protected:
//数据成员
vmime::messageBuilder mb;
vmime::mailbox sender;
vmime::mailboxList recver;
rapidjson::Document::AllocatorType &_dallocator = _response.GetAllocator();
};
//正文和附件都来自文件
class MailFileOp: public MailOp{
public:
MailFileOp()=delete;
MailFileOp(const MailFileOp&)=delete;
MailFileOp(const char* json):MailOp(json){
//需要重新更新lastsessiontime
if( _request.HasMember("recv_users") //邮件接收者,数组
&& _request.HasMember("email_content") //邮件正文
&& (_request["email_content"].HasMember("body")||_request["email_content"].HasMember("path"))
)
{
//获得数据库中上次登录时间
if(_request.HasMember("sessionid")){
lastSessionTime = GetLastSessionTime("sessionid");
}
validated=true;
//判断数组
if( _request["recv_users"].IsArray()==false)
{
//键值对中的值都应该是数组
validated=false;
_response.AddMember("recv_alert","recv_users or attachments, key-value should be an array.",_dallocator);
}else{
//确保每个邮件接收者都是以saicmotor.com结尾
auto& recv_array=_request["recv_users"];
for(auto& it:recv_array.GetArray()){
if(std::string(it.GetString()).find("@saicmotor.com")==std::string::npos){
validated=false;
_response.AddMember("email_alert","email should be ended with @saicmotor.com.",_dallocator);
break;
}
}
}
//判断附件的格式
if(_request.HasMember("attachments")&&_request["attachments"].IsArray()==true){
auto& attachments=_request["attachments"];
for(auto& at:attachments.GetArray()){
if(!at.HasMember("md5")||!at.HasMember("mname")||!at.HasMember("mtype")){
validated=false;
_response.AddMember("attachments","attachments metadata incorrect.",_dallocator);
break;
}
}
}
}else{
//键值对格式错误
validated=false;
}
}
//邮件发送
MailFileOp& Construct();
//设置根目录
void SetRootDir(const char* _dir){
upload_root_dir=_dir;
}
private:
std::string upload_root_dir;
};
//验证码发送
class VerificatonCodeOp:public MailOp{
public:
VerificatonCodeOp()=delete;
VerificatonCodeOp(const VerificatonCodeOp&)=delete;
VerificatonCodeOp(const char* json):MailOp(json){
if (_request.HasMember("id"))
{
validated = true;
}
else
{
validated = false;
}
}
//验证码构造
VerificatonCodeOp& Construct();
//验证码验证
VerificatonCodeOp& Verify();
};
//文件上传
class UploadOp:public TObject{
public:
UploadOp()=delete;
UploadOp(const UploadOp&)=delete;
//MultiPart-form字段解析
UploadOp(const char* multiform);
/*
//静态函数对象functor作为接口,处理数据库读取相关
static std::function<rapidjson::Document(const char*,const char*,bool&)> BaseDao;
private:
bool UploadOpStatus=false;
bool validated=false;
*/
//返回json对象
rapidjson::Document Ret() { return std::move(doc); };
UploadOp& ToDataBase();
//数据库调用接口
static DbInterface UploadDao;
//存储sessionid
static std::string sessionid;
private:
rapidjson::Document doc;
bool validated=false;
bool UploadStatus=false;
};
} // namespace TestValidation
#endif