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_error_no.h"
19 #include "account_log_wrapper.h"
20 #include "os_account_constants.h"
21
22 namespace OHOS {
23 namespace AccountSA {
24 namespace {
25 const std::string LOCAL_ID = "localId";
26 const std::string LOCAL_NAME = "localName";
27 const std::string SHORT_NAME = "shortName";
28 const std::string TYPE = "type";
29 const std::string CONSTRAINTS = "constraints";
30 const std::string IS_OS_ACCOUNT_VERIFIED = "isVerified";
31 const std::string PHOTO = "photo";
32 const std::string CREATE_TIME = "createTime";
33 const std::string LAST_LOGGED_IN_TIME = "lastLoginTime";
34 const std::string SERIAL_NUMBER = "serialNumber";
35 const std::string IS_ACTIVATED = "isActived";
36 const std::string IS_ACCOUNT_COMPLETED = "isCreateCompleted";
37 const std::string DOMAIN_INFO = "domainInfo";
38 const std::string DOMAIN_NAME = "domain";
39 const std::string DOMAIN_ACCOUNT_NAME = "accountName";
40 const std::string DOMAIN_ACCOUNT_ID = "accountId";
41 const std::string TO_BE_REMOVED = "toBeRemoved";
42 const std::string CREDENTIAL_ID = "credentialId";
43 const std::string DISPLAY_ID = "displayId";
44 const std::string IS_FOREGROUND = "isForeground";
45 const std::string IS_LOGGED_IN = "isLoggedIn";
46 const std::string IS_DATA_REMOVABLE = "isDataRemovable";
47 const std::string DOMAIN_ACCOUNT_STATUS = "domainAccountStatus";
48 const std::string DOMAIN_ACCOUNT_CONFIG = "domainServerConfigId";
49 } // namespace
50
OsAccountInfo()51 OsAccountInfo::OsAccountInfo()
52 {}
53
OsAccountInfo(int localId,const std::string localName,OsAccountType type,int64_t serialNumber)54 OsAccountInfo::OsAccountInfo(int localId, const std::string localName, OsAccountType type, int64_t serialNumber)
55 : localId_(localId), localName_(localName), type_(type), serialNumber_(serialNumber)
56 {}
57
OsAccountInfo(int localId,const std::string localName,const std::string shortName,OsAccountType type,int64_t serialNumber)58 OsAccountInfo::OsAccountInfo(int localId, const std::string localName, const std::string shortName, OsAccountType type,
59 int64_t serialNumber)
60 : localId_(localId), localName_(localName), shortName_(shortName), type_(type), serialNumber_(serialNumber)
61 {}
62
GetLocalId() const63 int OsAccountInfo::GetLocalId() const
64 {
65 return localId_;
66 }
67
SetLocalId(int localId)68 void OsAccountInfo::SetLocalId(int localId)
69 {
70 localId_ = localId;
71 }
72
GetLocalName() const73 std::string OsAccountInfo::GetLocalName() const
74 {
75 return localName_;
76 }
77
SetLocalName(const std::string localName)78 void OsAccountInfo::SetLocalName(const std::string localName)
79 {
80 localName_ = localName;
81 }
82
GetShortName() const83 std::string OsAccountInfo::GetShortName() const
84 {
85 return shortName_;
86 }
87
SetShortName(const std::string & shortName)88 void OsAccountInfo::SetShortName(const std::string &shortName)
89 {
90 shortName_ = shortName;
91 }
92
GetType() const93 OsAccountType OsAccountInfo::GetType() const
94 {
95 return type_;
96 }
97
SetType(OsAccountType type)98 void OsAccountInfo::SetType(OsAccountType type)
99 {
100 type_ = type;
101 }
102
GetConstraints() const103 std::vector<std::string> OsAccountInfo::GetConstraints() const
104 {
105 return constraints_;
106 }
107
SetConstraints(const std::vector<std::string> constraints)108 void OsAccountInfo::SetConstraints(const std::vector<std::string> constraints)
109 {
110 constraints_ = constraints;
111 }
112
GetIsVerified() const113 bool OsAccountInfo::GetIsVerified() const
114 {
115 return isVerified_;
116 }
117
SetIsVerified(bool isVerified)118 void OsAccountInfo::SetIsVerified(bool isVerified)
119 {
120 isVerified_ = isVerified;
121 }
122
GetIsCreateCompleted() const123 bool OsAccountInfo::GetIsCreateCompleted() const
124 {
125 return isCreateCompleted_;
126 }
127
SetIsCreateCompleted(const bool isCreateCompleted)128 void OsAccountInfo::SetIsCreateCompleted(const bool isCreateCompleted)
129 {
130 isCreateCompleted_ = isCreateCompleted;
131 }
132
GetCredentialId() const133 uint64_t OsAccountInfo::GetCredentialId() const
134 {
135 return credentialId_;
136 }
137
SetCredentialId(uint64_t credentialId)138 void OsAccountInfo::SetCredentialId(uint64_t credentialId)
139 {
140 credentialId_ = credentialId;
141 }
142
GetDisplayId() const143 uint64_t OsAccountInfo::GetDisplayId() const
144 {
145 return displayId_;
146 }
147
SetDisplayId(const uint64_t displayId)148 void OsAccountInfo::SetDisplayId(const uint64_t displayId)
149 {
150 displayId_ = displayId;
151 }
152
GetIsForeground() const153 bool OsAccountInfo::GetIsForeground() const
154 {
155 return isForeground_;
156 }
157
SetIsForeground(bool isForeground)158 void OsAccountInfo::SetIsForeground(bool isForeground)
159 {
160 isForeground_ = isForeground;
161 }
162
GetIsLoggedIn() const163 bool OsAccountInfo::GetIsLoggedIn() const
164 {
165 return isLoggedIn_;
166 }
167
SetIsLoggedIn(bool isLoggedIn)168 void OsAccountInfo::SetIsLoggedIn(bool isLoggedIn)
169 {
170 isLoggedIn_ = isLoggedIn;
171 }
172
GetIsDataRemovable() const173 bool OsAccountInfo::GetIsDataRemovable() const
174 {
175 return isDataRemovable_;
176 }
177
SetIsDataRemovable(bool isDataRemovable)178 void OsAccountInfo::SetIsDataRemovable(bool isDataRemovable)
179 {
180 isDataRemovable_ = isDataRemovable;
181 }
182
SetDomainInfo(const DomainAccountInfo & domainInfo)183 bool OsAccountInfo::SetDomainInfo(const DomainAccountInfo &domainInfo)
184 {
185 if (domainInfo.accountName_.size() > Constants::LOCAL_NAME_MAX_SIZE) {
186 ACCOUNT_LOGE("domain account name too long! %{public}zu.", domainInfo.accountName_.size());
187 return false;
188 }
189 if (domainInfo.domain_.size() > Constants::DOMAIN_NAME_MAX_SIZE) {
190 ACCOUNT_LOGE("domain name too long! %{public}zu.", domainInfo.domain_.size());
191 return false;
192 }
193 domainInfo_ = domainInfo;
194 return true;
195 }
196
GetDomainInfo(DomainAccountInfo & domainInfo) const197 void OsAccountInfo::GetDomainInfo(DomainAccountInfo &domainInfo) const
198 {
199 domainInfo = domainInfo_;
200 }
201
GetIsActived() const202 bool OsAccountInfo::GetIsActived() const
203 {
204 return isActivated_;
205 }
206
SetIsActived(const bool isActivated)207 void OsAccountInfo::SetIsActived(const bool isActivated)
208 {
209 isActivated_ = isActivated;
210 }
211
GetPhoto() const212 std::string OsAccountInfo::GetPhoto() const
213 {
214 return photo_;
215 }
216
SetPhoto(const std::string photo)217 void OsAccountInfo::SetPhoto(const std::string photo)
218 {
219 photo_ = photo;
220 }
221
GetCreateTime() const222 int64_t OsAccountInfo::GetCreateTime() const
223 {
224 return createTime_;
225 }
226
SetCreateTime(const int64_t createTime)227 void OsAccountInfo::SetCreateTime(const int64_t createTime)
228 {
229 createTime_ = createTime;
230 }
231
GetLastLoginTime() const232 int64_t OsAccountInfo::GetLastLoginTime() const
233 {
234 return lastLoginTime_;
235 }
236
SetLastLoginTime(const int64_t lastLoginTime)237 void OsAccountInfo::SetLastLoginTime(const int64_t lastLoginTime)
238 {
239 lastLoginTime_ = lastLoginTime;
240 }
241
ToJson() const242 Json OsAccountInfo::ToJson() const
243 {
244 Json jsonObject = Json {
245 {LOCAL_ID, localId_},
246 {LOCAL_NAME, localName_},
247 {SHORT_NAME, shortName_},
248 {TYPE, type_},
249 {CONSTRAINTS, constraints_},
250 {IS_OS_ACCOUNT_VERIFIED, isVerified_},
251 {PHOTO, photo_},
252 {CREATE_TIME, createTime_},
253 {LAST_LOGGED_IN_TIME, lastLoginTime_},
254 {SERIAL_NUMBER, serialNumber_},
255 {IS_ACTIVATED, isActivated_},
256 {IS_ACCOUNT_COMPLETED, isCreateCompleted_},
257 {TO_BE_REMOVED, toBeRemoved_},
258 {CREDENTIAL_ID, credentialId_},
259 {DISPLAY_ID, displayId_},
260 {IS_FOREGROUND, isForeground_},
261 {IS_LOGGED_IN, isLoggedIn_},
262 {IS_DATA_REMOVABLE, isDataRemovable_},
263 {DOMAIN_INFO, {
264 {DOMAIN_NAME, domainInfo_.domain_},
265 {DOMAIN_ACCOUNT_NAME, domainInfo_.accountName_},
266 {DOMAIN_ACCOUNT_ID, domainInfo_.accountId_},
267 {DOMAIN_ACCOUNT_STATUS, domainInfo_.status_},
268 {DOMAIN_ACCOUNT_CONFIG, domainInfo_.serverConfigId_},
269 },
270 }
271 };
272 return jsonObject;
273 }
274
Unmarshalling(Parcel & parcel)275 OsAccountInfo *OsAccountInfo::Unmarshalling(Parcel &parcel)
276 {
277 OsAccountInfo *osAccountInfo = new (std::nothrow) OsAccountInfo();
278
279 if (osAccountInfo && !osAccountInfo->ReadFromParcel(parcel)) {
280 ACCOUNT_LOGE("failed to read from parcel");
281 delete osAccountInfo;
282 osAccountInfo = nullptr;
283 }
284
285 return osAccountInfo;
286 }
287
GetDomainInfoFromJson(const Json & jsonObject)288 void OsAccountInfo::GetDomainInfoFromJson(const Json &jsonObject)
289 {
290 const auto &jsonObjectEnd = jsonObject.end();
291 Json typeJson;
292 OHOS::AccountSA::GetDataByType<Json>(
293 jsonObject, jsonObjectEnd, DOMAIN_INFO, typeJson, OHOS::AccountSA::JsonType::OBJECT);
294 OHOS::AccountSA::GetDataByType<std::string>(
295 typeJson, typeJson.end(), DOMAIN_NAME, domainInfo_.domain_, OHOS::AccountSA::JsonType::STRING);
296 OHOS::AccountSA::GetDataByType<std::string>(
297 typeJson, typeJson.end(), DOMAIN_ACCOUNT_NAME, domainInfo_.accountName_, OHOS::AccountSA::JsonType::STRING);
298 OHOS::AccountSA::GetDataByType<std::string>(
299 typeJson, typeJson.end(), DOMAIN_ACCOUNT_ID, domainInfo_.accountId_, OHOS::AccountSA::JsonType::STRING);
300 OHOS::AccountSA::GetDataByType<DomainAccountStatus>(
301 typeJson, typeJson.end(), DOMAIN_ACCOUNT_STATUS, domainInfo_.status_, OHOS::AccountSA::JsonType::NUMBER);
302 OHOS::AccountSA::GetDataByType<std::string>(
303 typeJson, typeJson.end(), DOMAIN_ACCOUNT_CONFIG, domainInfo_.serverConfigId_,
304 OHOS::AccountSA::JsonType::STRING);
305 }
306
FromJson(const Json & jsonObject)307 bool OsAccountInfo::FromJson(const Json &jsonObject)
308 {
309 const auto &jsonObjectEnd = jsonObject.end();
310 bool parseSuccess = OHOS::AccountSA::GetDataByType<int>(
311 jsonObject, jsonObjectEnd, LOCAL_ID, localId_, OHOS::AccountSA::JsonType::NUMBER);
312 OHOS::AccountSA::GetDataByType<std::string>(
313 jsonObject, jsonObjectEnd, LOCAL_NAME, localName_, OHOS::AccountSA::JsonType::STRING);
314 OHOS::AccountSA::GetDataByType<std::string>(
315 jsonObject, jsonObjectEnd, SHORT_NAME, shortName_, OHOS::AccountSA::JsonType::STRING);
316 OHOS::AccountSA::GetDataByType<OsAccountType>(
317 jsonObject, jsonObjectEnd, TYPE, type_, OHOS::AccountSA::JsonType::NUMBER);
318 OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
319 jsonObject, jsonObjectEnd, CONSTRAINTS, constraints_, OHOS::AccountSA::JsonType::ARRAY);
320 OHOS::AccountSA::GetDataByType<bool>(
321 jsonObject, jsonObjectEnd, IS_OS_ACCOUNT_VERIFIED, isVerified_, OHOS::AccountSA::JsonType::BOOLEAN);
322 OHOS::AccountSA::GetDataByType<std::string>(
323 jsonObject, jsonObjectEnd, PHOTO, photo_, OHOS::AccountSA::JsonType::STRING);
324 OHOS::AccountSA::GetDataByType<int64_t>(
325 jsonObject, jsonObjectEnd, CREATE_TIME, createTime_, OHOS::AccountSA::JsonType::NUMBER);
326 OHOS::AccountSA::GetDataByType<int64_t>(
327 jsonObject, jsonObjectEnd, LAST_LOGGED_IN_TIME, lastLoginTime_, OHOS::AccountSA::JsonType::NUMBER);
328 OHOS::AccountSA::GetDataByType<int64_t>(
329 jsonObject, jsonObjectEnd, SERIAL_NUMBER, serialNumber_, OHOS::AccountSA::JsonType::NUMBER);
330 OHOS::AccountSA::GetDataByType<bool>(
331 jsonObject, jsonObjectEnd, IS_ACTIVATED, isActivated_, OHOS::AccountSA::JsonType::BOOLEAN);
332 parseSuccess = parseSuccess && OHOS::AccountSA::GetDataByType<bool>(
333 jsonObject, jsonObjectEnd, IS_ACCOUNT_COMPLETED, isCreateCompleted_, OHOS::AccountSA::JsonType::BOOLEAN);
334 OHOS::AccountSA::GetDataByType<bool>(
335 jsonObject, jsonObjectEnd, TO_BE_REMOVED, toBeRemoved_, OHOS::AccountSA::JsonType::BOOLEAN);
336 OHOS::AccountSA::GetDataByType<uint64_t>(
337 jsonObject, jsonObjectEnd, CREDENTIAL_ID, credentialId_, OHOS::AccountSA::JsonType::NUMBER);
338 OHOS::AccountSA::GetDataByType<uint64_t>(
339 jsonObject, jsonObjectEnd, DISPLAY_ID, displayId_, OHOS::AccountSA::JsonType::NUMBER);
340 OHOS::AccountSA::GetDataByType<bool>(
341 jsonObject, jsonObjectEnd, IS_FOREGROUND, isForeground_, OHOS::AccountSA::JsonType::BOOLEAN);
342 OHOS::AccountSA::GetDataByType<bool>(
343 jsonObject, jsonObjectEnd, IS_LOGGED_IN, isLoggedIn_, OHOS::AccountSA::JsonType::BOOLEAN);
344 OHOS::AccountSA::GetDataByType<bool>(
345 jsonObject, jsonObjectEnd, IS_DATA_REMOVABLE, isDataRemovable_, OHOS::AccountSA::JsonType::BOOLEAN);
346
347 GetDomainInfoFromJson(jsonObject);
348 if (!parseSuccess) {
349 ACCOUNT_LOGE("parse from json failed");
350 }
351 return parseSuccess;
352 }
353
Marshalling(Parcel & parcel) const354 bool OsAccountInfo::Marshalling(Parcel &parcel) const
355 {
356 return parcel.WriteString(ToString());
357 }
358
ReadFromParcel(Parcel & parcel)359 bool OsAccountInfo::ReadFromParcel(Parcel &parcel)
360 {
361 std::string jsonString = parcel.ReadString();
362 nlohmann::json jsonObject = nlohmann::json::parse(jsonString, nullptr, false);
363 if (jsonObject.is_discarded()) {
364 ACCOUNT_LOGE("jsonObject is discarded");
365 }
366 FromJson(jsonObject);
367 return true;
368 }
369
ToString() const370 std::string OsAccountInfo::ToString() const
371 {
372 auto jsonObject = ToJson();
373 std::string jsonString;
374 try {
375 jsonString = jsonObject.dump();
376 } catch (Json::type_error& err) {
377 ACCOUNT_LOGE("failed to dump json object, reason: %{public}s", err.what());
378 }
379 return jsonString;
380 }
381
GetPrimeKey() const382 std::string OsAccountInfo::GetPrimeKey() const
383 {
384 return std::to_string(localId_);
385 }
386
GetSerialNumber() const387 int64_t OsAccountInfo::GetSerialNumber() const
388 {
389 return serialNumber_;
390 }
391
SetSerialNumber(const int64_t serialNumber)392 void OsAccountInfo::SetSerialNumber(const int64_t serialNumber)
393 {
394 serialNumber_ = serialNumber;
395 }
396
GetToBeRemoved() const397 bool OsAccountInfo::GetToBeRemoved() const
398 {
399 return toBeRemoved_;
400 }
401
SetToBeRemoved(bool toBeRemoved)402 void OsAccountInfo::SetToBeRemoved(bool toBeRemoved)
403 {
404 toBeRemoved_ = toBeRemoved;
405 }
406
IsTypeOutOfRange() const407 bool OsAccountInfo::IsTypeOutOfRange() const
408 {
409 return (type_ < OsAccountType::ADMIN) || ((type_ > OsAccountType::GUEST) && (type_ < OsAccountType::PRIVATE)) ||
410 (type_ >= OsAccountType::END);
411 }
412
ParamCheck()413 ErrCode OsAccountInfo::ParamCheck()
414 {
415 if (localId_ < Constants::START_USER_ID) {
416 ACCOUNT_LOGE("os localId is invalid");
417 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
418 }
419
420 if (localName_.size() > Constants::LOCAL_NAME_MAX_SIZE) {
421 ACCOUNT_LOGE("local name length %{public}zu is too long!", localName_.size());
422 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
423 }
424 if (localName_.empty() && localId_ != Constants::START_USER_ID) {
425 ACCOUNT_LOGE("local name is empty!");
426 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
427 }
428
429 if (IsTypeOutOfRange() || (localId_ == Constants::START_USER_ID && type_ != OsAccountType::ADMIN)) {
430 ACCOUNT_LOGE("os account type is invalid");
431 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
432 }
433
434 if ((createTime_ <= 0) && (localId_ != Constants::START_USER_ID)) {
435 ACCOUNT_LOGE("os create time is invalid");
436 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
437 }
438
439 return ERR_OK;
440 }
441
Marshalling(Parcel & parcel) const442 bool CreateOsAccountOptions::Marshalling(Parcel &parcel) const
443 {
444 return parcel.WriteStringVector(disallowedHapList) && parcel.WriteBool(hasShortName);
445 }
446
Unmarshalling(Parcel & parcel)447 CreateOsAccountOptions *CreateOsAccountOptions::Unmarshalling(Parcel &parcel)
448 {
449 CreateOsAccountOptions *options = new (std::nothrow) CreateOsAccountOptions();
450 if ((options != nullptr) && (!options->ReadFromParcel(parcel))) {
451 ACCOUNT_LOGW("read from parcel failed");
452 delete options;
453 options = nullptr;
454 }
455 return options;
456 }
457
ReadFromParcel(Parcel & parcel)458 bool CreateOsAccountOptions::ReadFromParcel(Parcel &parcel)
459 {
460 return parcel.ReadStringVector(&disallowedHapList) && parcel.ReadBool(hasShortName);
461 }
462 } // namespace AccountSA
463 } // namespace OHOS