• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "os_account_info.h"
16 #include "account_log_wrapper.h"
17 #include "os_account_constants.h"
18 
19 namespace OHOS {
20 namespace AccountSA {
21 namespace {
22 const std::string LOCAL_ID = "localId";
23 const std::string LOCAL_NAME = "localName";
24 const std::string TYPE = "type";
25 const std::string CONSTRAINTS = "constraints";
26 const std::string IS_OS_ACCOUNT_VERIFIED = "isVerified";
27 const std::string PHOTO = "photo";
28 const std::string CREATE_TIME = "createTime";
29 const std::string LAST_LOGGED_IN_TIME = "lastLoginTime";
30 const std::string SERIAL_NUMBER = "serialNumber";
31 const std::string IS_ACTIVED = "isActived";
32 const std::string IS_ACCOUNT_COMPLETED = "isCreateCompleted";
33 const std::string DOMAIN_INFO = "domainInfo";
34 const std::string DOMAIN_NAME = "domain";
35 const std::string DOMAIN_ACCOUNT_NAME = "accountName";
36 const std::string TO_BE_REMOVED = "toBeRemoved";
37 }  // namespace
38 
OsAccountInfo()39 OsAccountInfo::OsAccountInfo()
40 {
41     localId_ = -1;
42     localName_.clear();
43     type_ = OsAccountType::ADMIN;
44     constraints_.clear();
45     isVerified_ = false;
46     photo_.clear();
47     createTime_ = 0;
48     lastLoginTime_ = 0;
49     serialNumber_ = 0;
50     isActived_ = false;
51     isCreateCompleted_ = false;
52     domainInfo_.Clear();
53     toBeRemoved_ = false;
54 }
55 
OsAccountInfo(int localId,const std::string localName,OsAccountType type,int64_t serialNumber)56 OsAccountInfo::OsAccountInfo(int localId, const std::string localName, OsAccountType type, int64_t serialNumber)
57     : localId_(localId), localName_(localName), type_(type), serialNumber_(serialNumber)
58 {
59     constraints_.clear();
60     isVerified_ = false;
61     photo_.clear();
62     createTime_ = 0;
63     lastLoginTime_ = 0;
64     isActived_ = false;
65     isCreateCompleted_ = false;
66     domainInfo_.Clear();
67     toBeRemoved_ = false;
68 }
69 
OsAccountInfo(int localId,std::string localName,OsAccountType type,std::vector<std::string> constraints,bool isVerified,std::string photo,int64_t createTime,int64_t lastLoginTime,int64_t serialNumber,bool isCreateCompleted)70 OsAccountInfo::OsAccountInfo(int localId, std::string localName, OsAccountType type,
71     std::vector<std::string> constraints, bool isVerified, std::string photo, int64_t createTime, int64_t lastLoginTime,
72     int64_t serialNumber, bool isCreateCompleted)
73     : localId_(localId),
74       localName_(localName),
75       type_(type),
76       constraints_(constraints),
77       isVerified_(isVerified),
78       photo_(photo),
79       createTime_(createTime),
80       lastLoginTime_(lastLoginTime),
81       serialNumber_(serialNumber),
82       isCreateCompleted_(isCreateCompleted)
83 {
84     isActived_ = false;
85     domainInfo_.Clear();
86     toBeRemoved_ = false;
87 }
88 
GetLocalId() const89 int OsAccountInfo::GetLocalId() const
90 {
91     return localId_;
92 }
93 
SetLocalId(int localId)94 void OsAccountInfo::SetLocalId(int localId)
95 {
96     localId_ = localId;
97 }
98 
GetLocalName() const99 std::string OsAccountInfo::GetLocalName() const
100 {
101     return localName_;
102 }
103 
SetLocalName(const std::string localName)104 void OsAccountInfo::SetLocalName(const std::string localName)
105 {
106     localName_ = localName;
107 }
108 
GetType() const109 OsAccountType OsAccountInfo::GetType() const
110 {
111     return type_;
112 }
113 
SetType(OsAccountType type)114 void OsAccountInfo::SetType(OsAccountType type)
115 {
116     type_ = type;
117 }
118 
GetConstraints() const119 std::vector<std::string> OsAccountInfo::GetConstraints() const
120 {
121     return constraints_;
122 }
123 
SetConstraints(const std::vector<std::string> constraints)124 void OsAccountInfo::SetConstraints(const std::vector<std::string> constraints)
125 {
126     constraints_ = constraints;
127 }
128 
GetIsVerified() const129 bool OsAccountInfo::GetIsVerified() const
130 {
131     return isVerified_;
132 }
133 
SetIsVerified(bool isVerified)134 void OsAccountInfo::SetIsVerified(bool isVerified)
135 {
136     isVerified_ = isVerified;
137 }
138 
GetIsCreateCompleted() const139 bool OsAccountInfo::GetIsCreateCompleted() const
140 {
141     return isCreateCompleted_;
142 }
143 
SetIsCreateCompleted(const bool isCreateCompleted)144 void OsAccountInfo::SetIsCreateCompleted(const bool isCreateCompleted)
145 {
146     isCreateCompleted_ = isCreateCompleted;
147 }
148 
SetDomainInfo(const DomainAccountInfo & domainInfo)149 bool OsAccountInfo::SetDomainInfo(const DomainAccountInfo &domainInfo)
150 {
151     if (domainInfo.accountName_.size() > Constants::DOMAIN_ACCOUNT_NAME_MAX_SIZE) {
152         ACCOUNT_LOGE("domain account name too long! %{public}zu.", domainInfo.accountName_.size());
153         return false;
154     }
155     if (domainInfo.domain_.size() > Constants::DOMAIN_NAME_MAX_SIZE) {
156         ACCOUNT_LOGE("domain name too long! %{public}zu.", domainInfo.domain_.size());
157         return false;
158     }
159     domainInfo_.accountName_ = domainInfo.accountName_;
160     domainInfo_.domain_ = domainInfo.domain_;
161     return true;
162 }
163 
GetDomainInfo(DomainAccountInfo & domainInfo) const164 void OsAccountInfo::GetDomainInfo(DomainAccountInfo &domainInfo) const
165 {
166     domainInfo.accountName_ = domainInfo_.accountName_;
167     domainInfo.domain_ = domainInfo_.domain_;
168 }
169 
GetIsActived() const170 bool OsAccountInfo::GetIsActived() const
171 {
172     return isActived_;
173 }
174 
SetIsActived(const bool isActived)175 void OsAccountInfo::SetIsActived(const bool isActived)
176 {
177     isActived_ = isActived;
178 }
179 
GetPhoto() const180 std::string OsAccountInfo::GetPhoto() const
181 {
182     return photo_;
183 }
184 
SetPhoto(const std::string photo)185 void OsAccountInfo::SetPhoto(const std::string photo)
186 {
187     photo_ = photo;
188 }
189 
GetCreateTime() const190 int64_t OsAccountInfo::GetCreateTime() const
191 {
192     return createTime_;
193 }
194 
SetCreateTime(const int64_t createTime)195 void OsAccountInfo::SetCreateTime(const int64_t createTime)
196 {
197     createTime_ = createTime;
198 }
199 
GetLastLoginTime() const200 int64_t OsAccountInfo::GetLastLoginTime() const
201 {
202     return lastLoginTime_;
203 }
204 
SetLastLoginTime(const int64_t lastLoginTime)205 void OsAccountInfo::SetLastLoginTime(const int64_t lastLoginTime)
206 {
207     lastLoginTime_ = lastLoginTime;
208 }
209 
ToJson() const210 Json OsAccountInfo::ToJson() const
211 {
212     Json jsonObject = Json {
213         {LOCAL_ID, localId_},
214         {LOCAL_NAME, localName_},
215         {TYPE, type_},
216         {CONSTRAINTS, constraints_},
217         {IS_OS_ACCOUNT_VERIFIED, isVerified_},
218         {PHOTO, photo_},
219         {CREATE_TIME, createTime_},
220         {LAST_LOGGED_IN_TIME, lastLoginTime_},
221         {SERIAL_NUMBER, serialNumber_},
222         {IS_ACTIVED, isActived_},
223         {IS_ACCOUNT_COMPLETED, isCreateCompleted_},
224         {TO_BE_REMOVED, toBeRemoved_},
225         {DOMAIN_INFO, {
226             {DOMAIN_NAME, domainInfo_.domain_},
227             {DOMAIN_ACCOUNT_NAME, domainInfo_.accountName_},
228         },
229         }
230     };
231     return jsonObject;
232 }
Unmarshalling(Parcel & parcel)233 OsAccountInfo *OsAccountInfo::Unmarshalling(Parcel &parcel)
234 {
235     OsAccountInfo *osAccountInfo = new (std::nothrow) OsAccountInfo();
236 
237     if (osAccountInfo && !osAccountInfo->ReadFromParcel(parcel)) {
238         ACCOUNT_LOGE("failed to read from parcel");
239         delete osAccountInfo;
240         osAccountInfo = nullptr;
241     }
242 
243     return osAccountInfo;
244 }
FromJson(const Json & jsonObject)245 void OsAccountInfo::FromJson(const Json &jsonObject)
246 {
247     const auto &jsonObjectEnd = jsonObject.end();
248     OHOS::AccountSA::GetDataByType<int>(
249         jsonObject, jsonObjectEnd, LOCAL_ID, localId_, OHOS::AccountSA::JsonType::NUMBER);
250     OHOS::AccountSA::GetDataByType<std::string>(
251         jsonObject, jsonObjectEnd, LOCAL_NAME, localName_, OHOS::AccountSA::JsonType::STRING);
252     OHOS::AccountSA::GetDataByType<OsAccountType>(
253         jsonObject, jsonObjectEnd, TYPE, type_, OHOS::AccountSA::JsonType::NUMBER);
254     OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
255         jsonObject, jsonObjectEnd, CONSTRAINTS, constraints_, OHOS::AccountSA::JsonType::ARRAY);
256     OHOS::AccountSA::GetDataByType<bool>(
257         jsonObject, jsonObjectEnd, IS_OS_ACCOUNT_VERIFIED, isVerified_, OHOS::AccountSA::JsonType::BOOLEAN);
258     OHOS::AccountSA::GetDataByType<std::string>(
259         jsonObject, jsonObjectEnd, PHOTO, photo_, OHOS::AccountSA::JsonType::STRING);
260     OHOS::AccountSA::GetDataByType<int64_t>(
261         jsonObject, jsonObjectEnd, CREATE_TIME, createTime_, OHOS::AccountSA::JsonType::NUMBER);
262     OHOS::AccountSA::GetDataByType<int64_t>(
263         jsonObject, jsonObjectEnd, LAST_LOGGED_IN_TIME, lastLoginTime_, OHOS::AccountSA::JsonType::NUMBER);
264     OHOS::AccountSA::GetDataByType<int64_t>(
265         jsonObject, jsonObjectEnd, SERIAL_NUMBER, serialNumber_, OHOS::AccountSA::JsonType::NUMBER);
266     OHOS::AccountSA::GetDataByType<bool>(
267         jsonObject, jsonObjectEnd, IS_ACTIVED, isActived_, OHOS::AccountSA::JsonType::BOOLEAN);
268     OHOS::AccountSA::GetDataByType<bool>(
269         jsonObject, jsonObjectEnd, IS_ACCOUNT_COMPLETED, isCreateCompleted_, OHOS::AccountSA::JsonType::BOOLEAN);
270     OHOS::AccountSA::GetDataByType<bool>(
271         jsonObject, jsonObjectEnd, TO_BE_REMOVED, toBeRemoved_, OHOS::AccountSA::JsonType::BOOLEAN);
272 
273     Json typeJson;
274     OHOS::AccountSA::GetDataByType<Json>(
275         jsonObject, jsonObjectEnd, DOMAIN_INFO, typeJson, OHOS::AccountSA::JsonType::OBJECT);
276     OHOS::AccountSA::GetDataByType<std::string>(
277         typeJson, typeJson.end(), DOMAIN_NAME, domainInfo_.domain_, OHOS::AccountSA::JsonType::STRING);
278     OHOS::AccountSA::GetDataByType<std::string>(
279         typeJson, typeJson.end(), DOMAIN_ACCOUNT_NAME, domainInfo_.accountName_, OHOS::AccountSA::JsonType::STRING);
280 }
281 
Marshalling(Parcel & parcel) const282 bool OsAccountInfo::Marshalling(Parcel &parcel) const
283 {
284     parcel.WriteString(ToString());
285     return true;
286 }
287 
ReadFromParcel(Parcel & parcel)288 bool OsAccountInfo::ReadFromParcel(Parcel &parcel)
289 {
290     std::string jsonString = parcel.ReadString();
291     nlohmann::json jsonObject = nlohmann::json::parse(jsonString, nullptr, false);
292     FromJson(jsonObject);
293     return true;
294 }
295 
ToString() const296 std::string OsAccountInfo::ToString() const
297 {
298     auto jsonObject = ToJson();
299     std::string jsonString;
300     try {
301         jsonString = jsonObject.dump();
302     } catch (Json::type_error& err) {
303         ACCOUNT_LOGE("failed to dump json object, reason: %{public}s", err.what());
304     }
305     return jsonString;
306 }
307 
GetPrimeKey() const308 std::string OsAccountInfo::GetPrimeKey() const
309 {
310     return std::to_string(localId_);
311 }
312 
GetSerialNumber() const313 int64_t OsAccountInfo::GetSerialNumber() const
314 {
315     return serialNumber_;
316 }
317 
SetSerialNumber(const int64_t serialNumber)318 void OsAccountInfo::SetSerialNumber(const int64_t serialNumber)
319 {
320     serialNumber_ = serialNumber;
321 }
322 
GetToBeRemoved() const323 bool OsAccountInfo::GetToBeRemoved() const
324 {
325     return toBeRemoved_;
326 }
327 
SetToBeRemoved(bool toBeRemoved)328 void OsAccountInfo::SetToBeRemoved(bool toBeRemoved)
329 {
330     toBeRemoved_ = toBeRemoved;
331 }
332 }  // namespace AccountSA
333 }  // namespace OHOS