1 /*
2 * Copyright (c) 2021-2023 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
17 #include <ctime>
18 #include "account_log_wrapper.h"
19 #include "os_account_constants.h"
20
21 namespace OHOS {
22 namespace AccountSA {
23 namespace {
24 const std::string LOCAL_ID = "localId";
25 const std::string LOCAL_NAME = "localName";
26 const std::string TYPE = "type";
27 const std::string CONSTRAINTS = "constraints";
28 const std::string IS_OS_ACCOUNT_VERIFIED = "isVerified";
29 const std::string PHOTO = "photo";
30 const std::string CREATE_TIME = "createTime";
31 const std::string LAST_LOGGED_IN_TIME = "lastLoginTime";
32 const std::string SERIAL_NUMBER = "serialNumber";
33 const std::string IS_ACTIVATED = "isActived";
34 const std::string IS_ACCOUNT_COMPLETED = "isCreateCompleted";
35 const std::string DOMAIN_INFO = "domainInfo";
36 const std::string DOMAIN_NAME = "domain";
37 const std::string DOMAIN_ACCOUNT_NAME = "accountName";
38 const std::string DOMAIN_ACCOUNT_ID = "accountId";
39 const std::string TO_BE_REMOVED = "toBeRemoved";
40 const std::string IS_CREATE_SECRET = "isCreateSecret";
41 const std::string DOMAIN_ACCOUNT_STATUS = "domainAccountStatus";
42 } // namespace
43
OsAccountInfo()44 OsAccountInfo::OsAccountInfo()
45 {
46 localId_ = -1;
47 localName_.clear();
48 type_ = OsAccountType::ADMIN;
49 constraints_.clear();
50 isVerified_ = false;
51 photo_.clear();
52 createTime_ = 0;
53 lastLoginTime_ = 0;
54 serialNumber_ = 0;
55 isActivated_ = false;
56 isCreateCompleted_ = false;
57 domainInfo_.Clear();
58 toBeRemoved_ = false;
59 isCreateSecret_ = false;
60 }
61
OsAccountInfo(int localId,const std::string localName,OsAccountType type,int64_t serialNumber)62 OsAccountInfo::OsAccountInfo(int localId, const std::string localName, OsAccountType type, int64_t serialNumber)
63 : localId_(localId), localName_(localName), type_(type), serialNumber_(serialNumber)
64 {
65 constraints_.clear();
66 isVerified_ = false;
67 photo_.clear();
68 createTime_ = 0;
69 lastLoginTime_ = 0;
70 isActivated_ = false;
71 isCreateCompleted_ = false;
72 domainInfo_.Clear();
73 toBeRemoved_ = false;
74 isCreateSecret_ = false;
75 }
76
GetLocalId() const77 int OsAccountInfo::GetLocalId() const
78 {
79 return localId_;
80 }
81
SetLocalId(int localId)82 void OsAccountInfo::SetLocalId(int localId)
83 {
84 localId_ = localId;
85 }
86
GetLocalName() const87 std::string OsAccountInfo::GetLocalName() const
88 {
89 return localName_;
90 }
91
SetLocalName(const std::string localName)92 void OsAccountInfo::SetLocalName(const std::string localName)
93 {
94 localName_ = localName;
95 }
96
GetType() const97 OsAccountType OsAccountInfo::GetType() const
98 {
99 return type_;
100 }
101
SetType(OsAccountType type)102 void OsAccountInfo::SetType(OsAccountType type)
103 {
104 type_ = type;
105 }
106
GetConstraints() const107 std::vector<std::string> OsAccountInfo::GetConstraints() const
108 {
109 return constraints_;
110 }
111
SetConstraints(const std::vector<std::string> constraints)112 void OsAccountInfo::SetConstraints(const std::vector<std::string> constraints)
113 {
114 constraints_ = constraints;
115 }
116
GetIsVerified() const117 bool OsAccountInfo::GetIsVerified() const
118 {
119 return isVerified_;
120 }
121
SetIsVerified(bool isVerified)122 void OsAccountInfo::SetIsVerified(bool isVerified)
123 {
124 isVerified_ = isVerified;
125 }
126
GetIsCreateCompleted() const127 bool OsAccountInfo::GetIsCreateCompleted() const
128 {
129 return isCreateCompleted_;
130 }
131
SetIsCreateCompleted(const bool isCreateCompleted)132 void OsAccountInfo::SetIsCreateCompleted(const bool isCreateCompleted)
133 {
134 isCreateCompleted_ = isCreateCompleted;
135 }
136
GetIsCreateSecret() const137 bool OsAccountInfo::GetIsCreateSecret() const
138 {
139 return isCreateSecret_;
140 }
141
SetIsCreateSecret(bool isCreateSecret)142 void OsAccountInfo::SetIsCreateSecret(bool isCreateSecret)
143 {
144 isCreateSecret_ = isCreateSecret;
145 }
146
SetDomainInfo(const DomainAccountInfo & domainInfo)147 bool OsAccountInfo::SetDomainInfo(const DomainAccountInfo &domainInfo)
148 {
149 if (domainInfo.accountName_.size() > Constants::DOMAIN_ACCOUNT_NAME_MAX_SIZE) {
150 ACCOUNT_LOGE("domain account name too long! %{public}zu.", domainInfo.accountName_.size());
151 return false;
152 }
153 if (domainInfo.domain_.size() > Constants::DOMAIN_NAME_MAX_SIZE) {
154 ACCOUNT_LOGE("domain name too long! %{public}zu.", domainInfo.domain_.size());
155 return false;
156 }
157 domainInfo_.accountName_ = domainInfo.accountName_;
158 domainInfo_.domain_ = domainInfo.domain_;
159 domainInfo_.accountId_ = domainInfo.accountId_;
160 return true;
161 }
162
GetDomainInfo(DomainAccountInfo & domainInfo) const163 void OsAccountInfo::GetDomainInfo(DomainAccountInfo &domainInfo) const
164 {
165 domainInfo.accountName_ = domainInfo_.accountName_;
166 domainInfo.domain_ = domainInfo_.domain_;
167 domainInfo.accountId_ = domainInfo_.accountId_;
168 }
169
GetIsActived() const170 bool OsAccountInfo::GetIsActived() const
171 {
172 return isActivated_;
173 }
174
SetIsActived(const bool isActivated)175 void OsAccountInfo::SetIsActived(const bool isActivated)
176 {
177 isActivated_ = isActivated;
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_ACTIVATED, isActivated_},
223 {IS_ACCOUNT_COMPLETED, isCreateCompleted_},
224 {TO_BE_REMOVED, toBeRemoved_},
225 {IS_CREATE_SECRET, isCreateSecret_},
226 {DOMAIN_INFO, {
227 {DOMAIN_NAME, domainInfo_.domain_},
228 {DOMAIN_ACCOUNT_NAME, domainInfo_.accountName_},
229 {DOMAIN_ACCOUNT_ID, domainInfo_.accountId_},
230 },
231 }
232 };
233 return jsonObject;
234 }
235
Unmarshalling(Parcel & parcel)236 OsAccountInfo *OsAccountInfo::Unmarshalling(Parcel &parcel)
237 {
238 OsAccountInfo *osAccountInfo = new (std::nothrow) OsAccountInfo();
239
240 if (osAccountInfo && !osAccountInfo->ReadFromParcel(parcel)) {
241 ACCOUNT_LOGE("failed to read from parcel");
242 delete osAccountInfo;
243 osAccountInfo = nullptr;
244 }
245
246 return osAccountInfo;
247 }
248
FromJson(const Json & jsonObject)249 void OsAccountInfo::FromJson(const Json &jsonObject)
250 {
251 const auto &jsonObjectEnd = jsonObject.end();
252 OHOS::AccountSA::GetDataByType<int>(
253 jsonObject, jsonObjectEnd, LOCAL_ID, localId_, OHOS::AccountSA::JsonType::NUMBER);
254 OHOS::AccountSA::GetDataByType<std::string>(
255 jsonObject, jsonObjectEnd, LOCAL_NAME, localName_, OHOS::AccountSA::JsonType::STRING);
256 OHOS::AccountSA::GetDataByType<OsAccountType>(
257 jsonObject, jsonObjectEnd, TYPE, type_, OHOS::AccountSA::JsonType::NUMBER);
258 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
259 jsonObject, jsonObjectEnd, CONSTRAINTS, constraints_, OHOS::AccountSA::JsonType::ARRAY);
260 OHOS::AccountSA::GetDataByType<bool>(
261 jsonObject, jsonObjectEnd, IS_OS_ACCOUNT_VERIFIED, isVerified_, OHOS::AccountSA::JsonType::BOOLEAN);
262 OHOS::AccountSA::GetDataByType<std::string>(
263 jsonObject, jsonObjectEnd, PHOTO, photo_, OHOS::AccountSA::JsonType::STRING);
264 OHOS::AccountSA::GetDataByType<int64_t>(
265 jsonObject, jsonObjectEnd, CREATE_TIME, createTime_, OHOS::AccountSA::JsonType::NUMBER);
266 OHOS::AccountSA::GetDataByType<int64_t>(
267 jsonObject, jsonObjectEnd, LAST_LOGGED_IN_TIME, lastLoginTime_, OHOS::AccountSA::JsonType::NUMBER);
268 OHOS::AccountSA::GetDataByType<int64_t>(
269 jsonObject, jsonObjectEnd, SERIAL_NUMBER, serialNumber_, OHOS::AccountSA::JsonType::NUMBER);
270 OHOS::AccountSA::GetDataByType<bool>(
271 jsonObject, jsonObjectEnd, IS_ACTIVATED, isActivated_, OHOS::AccountSA::JsonType::BOOLEAN);
272 OHOS::AccountSA::GetDataByType<bool>(
273 jsonObject, jsonObjectEnd, IS_ACCOUNT_COMPLETED, isCreateCompleted_, OHOS::AccountSA::JsonType::BOOLEAN);
274 OHOS::AccountSA::GetDataByType<bool>(
275 jsonObject, jsonObjectEnd, TO_BE_REMOVED, toBeRemoved_, OHOS::AccountSA::JsonType::BOOLEAN);
276 OHOS::AccountSA::GetDataByType<bool>(
277 jsonObject, jsonObjectEnd, IS_CREATE_SECRET, isCreateSecret_, OHOS::AccountSA::JsonType::BOOLEAN);
278
279 Json typeJson;
280 OHOS::AccountSA::GetDataByType<Json>(
281 jsonObject, jsonObjectEnd, DOMAIN_INFO, typeJson, OHOS::AccountSA::JsonType::OBJECT);
282 OHOS::AccountSA::GetDataByType<std::string>(
283 typeJson, typeJson.end(), DOMAIN_NAME, domainInfo_.domain_, OHOS::AccountSA::JsonType::STRING);
284 OHOS::AccountSA::GetDataByType<std::string>(
285 typeJson, typeJson.end(), DOMAIN_ACCOUNT_NAME, domainInfo_.accountName_, OHOS::AccountSA::JsonType::STRING);
286 OHOS::AccountSA::GetDataByType<std::string>(
287 typeJson, typeJson.end(), DOMAIN_ACCOUNT_ID, domainInfo_.accountId_, OHOS::AccountSA::JsonType::STRING);
288 }
289
Marshalling(Parcel & parcel) const290 bool OsAccountInfo::Marshalling(Parcel &parcel) const
291 {
292 parcel.WriteString(ToString());
293 return true;
294 }
295
ReadFromParcel(Parcel & parcel)296 bool OsAccountInfo::ReadFromParcel(Parcel &parcel)
297 {
298 std::string jsonString = parcel.ReadString();
299 nlohmann::json jsonObject = nlohmann::json::parse(jsonString, nullptr, false);
300 FromJson(jsonObject);
301 return true;
302 }
303
ToString() const304 std::string OsAccountInfo::ToString() const
305 {
306 auto jsonObject = ToJson();
307 std::string jsonString;
308 try {
309 jsonString = jsonObject.dump();
310 } catch (Json::type_error& err) {
311 ACCOUNT_LOGE("failed to dump json object, reason: %{public}s", err.what());
312 }
313 return jsonString;
314 }
315
GetPrimeKey() const316 std::string OsAccountInfo::GetPrimeKey() const
317 {
318 return std::to_string(localId_);
319 }
320
GetSerialNumber() const321 int64_t OsAccountInfo::GetSerialNumber() const
322 {
323 return serialNumber_;
324 }
325
SetSerialNumber(const int64_t serialNumber)326 void OsAccountInfo::SetSerialNumber(const int64_t serialNumber)
327 {
328 serialNumber_ = serialNumber;
329 }
330
GetToBeRemoved() const331 bool OsAccountInfo::GetToBeRemoved() const
332 {
333 return toBeRemoved_;
334 }
335
SetToBeRemoved(bool toBeRemoved)336 void OsAccountInfo::SetToBeRemoved(bool toBeRemoved)
337 {
338 toBeRemoved_ = toBeRemoved;
339 }
340 } // namespace AccountSA
341 } // namespace OHOS