This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
accesscontrol.cpp
158 lines (148 loc) · 5.84 KB
/
accesscontrol.cpp
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
#include "accesscontrol.h"
//#include "octonetwork.h"//Global prama
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
#include <QJsonArray>
//#include <filedialog.h>
//#include <mainwindow.h>
AccessControl::AccessControl()
{
// GetACRequest.setUrl(QUrl(MainUrl));//http://IP/api/login
GetACRequest.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
}
//login
void AccessControl::acLogin()
{
if(userinfo.uid == NULL || userinfo.uid.trimmed() == "" || userinfo.pwd == NULL)
{
emit CommunicationStateChange(false);
return;
}
GetACRequest.setUrl(QUrl(MainUrl + "login"));//http://IP/api/login
QJsonObject TemperatureJsonObj;
// {
// "user": "uid",
// "pass": "pwd",
// "remember": "remember me"
// }
TemperatureJsonObj.insert("user",userinfo.uid);
TemperatureJsonObj.insert("pass",userinfo.pwd);
TemperatureJsonObj.insert("remember","remember me");
TemperatureJsonObj.insert("passive",true);//被动模式登录,如果免登录依旧能够获取session///需要允许被动登录才可以获得免登录方式
QJsonDocument _SenderJson;
_SenderJson.setObject(TemperatureJsonObj);
ACManager->disconnect(SIGNAL(finished(QNetworkReply*)));
connect(ACManager,&QNetworkAccessManager::finished,this,&AccessControl::LoginReply);
ACManager->post(GetACRequest,_SenderJson.toJson());
}
void AccessControl::LoginReply(QNetworkReply *reply)
{
bool _return=false;
// ACManager->disconnect(SIGNAL(finished(QNetworkReply*)));
if(reply->error() == QNetworkReply::NoError)
{
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(statusCode == 200)//Have receive
{
// userinfo.login = true;
//创建接收对象
QByteArray replyArray = reply->readAll();
QJsonDocument loadDoc(QJsonDocument::fromJson(replyArray));
QJsonObject QObj = loadDoc.object();
//获取登录反馈内容
QList<QJsonValue> result = SearchJsonValue(LoginMsg , QObj);//{{"active"},{"admin"},{"apikey"},{"name"},{"session"}};
userinfo.login = result[0].toBool();
userinfo.admin = result[1].toBool();
userinfo.API_Key = result[2].toString().toUtf8();
userinfo.onlineUid = result[3].toString();
userinfo.session = result[4].toString().toUtf8();
_return = true;
}
}
ACManager->disconnect(SIGNAL(finished(QNetworkReply*)));
emit CommunicationStateChange(_return);
}
//logout
void AccessControl::acLogout()
{
GetACRequest.setUrl(QUrl(MainUrl + "logout"));//http://IP/api/logout
ACManager->disconnect(SIGNAL(finished(QNetworkReply*)));
connect(ACManager,&QNetworkAccessManager::finished,this,&AccessControl::LogoutReply);
}
void AccessControl::LogoutReply(QNetworkReply *reply)
{
bool _return=false;
if(reply->error() == QNetworkReply::NoError)
{
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(statusCode == 200)//Have receive
{
//重置登录参数
userinfo = UserInfo(userinfo.uid,userinfo.pwd);
//创建接收对象
QByteArray replyArray = reply->readAll();
QJsonDocument loadDoc(QJsonDocument::fromJson(replyArray));
QJsonObject QObj = loadDoc.object();
// QList<QString> SearchLink ;
//获取登录反馈内容
QList<QJsonValue> result = SearchJsonValue(LoginMsg , QObj);//{{"active"},{"admin"},{"apikey"},{"name"},{"session"}};///被动账户执行退出登录才会返回账户信息
if(!result[3].isNull() && result[3].isString() && result[3].toString() == "_admin")
{
userinfo.login = result[0].toBool();
userinfo.admin = result[1].toBool();
userinfo.onlineUid = result[3].toString();//判定免登录账户:_admin
}
_return = true;
}
}
ACManager->disconnect(SIGNAL(finished(QNetworkReply*)));
emit CommunicationStateChange(_return);
}
//Function
//Find_List_Name : Retrieved List Sequence --> [Grandma,Mother,Me]
//_QObj:Retrieved Retrieved Json Objecet
QJsonValue AccessControl::SearchJsonValue(QList<QString> Find_List_Name , QJsonObject _QObj)
{
QJsonValue returnValue;
QList<QString> SearchLink ;
SearchLink << Find_List_Name;
if(SearchLink.count() > 1)
{
QString MotherKey = SearchLink[0];
SearchLink.removeFirst();
if(MotherKey.indexOf("*") >= 0)//wildcard
{
QJsonArray ArrayValue;
for(int i = 0 ; i <= 9 ; i++)
{
QString TempMotherKey = MotherKey;
if(_QObj.find(TempMotherKey.replace("*",QString::number(i))).value().isUndefined())continue;
// if(QSysInfo::productType() != "raspbian"){qDebug()<<_QObj.find(TempMotherKey.replace("*",QString::number(i))).value().isUndefined();};
QJsonValue data = SearchJsonValue(SearchLink , _QObj.constFind(TempMotherKey.replace("*",QString::number(i))).value().toObject());
ArrayValue.append(data);
}
returnValue = ArrayValue;
}
else
returnValue = SearchJsonValue(SearchLink , _QObj.constFind(MotherKey).value().toObject());
}
else
{
if(SearchLink[0].indexOf("*") >= 0)//wildcard
{
}
else
returnValue = _QObj.constFind(SearchLink[0]).value();
}
return returnValue;
}
QList<QJsonValue> AccessControl::SearchJsonValue(QList<QList<QString>> Find_List_Names , QJsonObject _QObj)
{
QList<QJsonValue> _returnValue ;
foreach(QList<QString> item , Find_List_Names)
{
_returnValue.append(SearchJsonValue(item,_QObj));
}
return _returnValue;
}