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