• 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 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 DOMAIN_ACCOUNT_STATUS = "domainAccountStatus";
47 const std::string DOMAIN_ACCOUNT_CONFIG = "domainServerConfigId";
48 }  // namespace
49 
OsAccountInfo()50 OsAccountInfo::OsAccountInfo()
51 {}
52 
OsAccountInfo(int localId,const std::string localName,OsAccountType type,int64_t serialNumber)53 OsAccountInfo::OsAccountInfo(int localId, const std::string localName, OsAccountType type, int64_t serialNumber)
54     : localId_(localId), localName_(localName), type_(type), serialNumber_(serialNumber)
55 {}
56 
OsAccountInfo(int localId,const std::string localName,const std::string shortName,OsAccountType type,int64_t serialNumber)57 OsAccountInfo::OsAccountInfo(int localId, const std::string localName, const std::string shortName, OsAccountType type,
58     int64_t serialNumber)
59     : localId_(localId), localName_(localName), shortName_(shortName), type_(type), serialNumber_(serialNumber)
60 {}
61 
GetLocalId() const62 int OsAccountInfo::GetLocalId() const
63 {
64     return localId_;
65 }
66 
SetLocalId(int localId)67 void OsAccountInfo::SetLocalId(int localId)
68 {
69     localId_ = localId;
70 }
71 
GetLocalName() const72 std::string OsAccountInfo::GetLocalName() const
73 {
74     return localName_;
75 }
76 
SetLocalName(const std::string localName)77 void OsAccountInfo::SetLocalName(const std::string localName)
78 {
79     localName_ = localName;
80 }
81 
GetShortName() const82 std::string OsAccountInfo::GetShortName() const
83 {
84     return shortName_;
85 }
86 
SetShortName(const std::string & shortName)87 void OsAccountInfo::SetShortName(const std::string &shortName)
88 {
89     shortName_ = shortName;
90 }
91 
GetType() const92 OsAccountType OsAccountInfo::GetType() const
93 {
94     return type_;
95 }
96 
SetType(OsAccountType type)97 void OsAccountInfo::SetType(OsAccountType type)
98 {
99     type_ = type;
100 }
101 
GetConstraints() const102 std::vector<std::string> OsAccountInfo::GetConstraints() const
103 {
104     return constraints_;
105 }
106 
SetConstraints(const std::vector<std::string> constraints)107 void OsAccountInfo::SetConstraints(const std::vector<std::string> constraints)
108 {
109     constraints_ = constraints;
110 }
111 
GetIsVerified() const112 bool OsAccountInfo::GetIsVerified() const
113 {
114     return isVerified_;
115 }
116 
SetIsVerified(bool isVerified)117 void OsAccountInfo::SetIsVerified(bool isVerified)
118 {
119     isVerified_ = isVerified;
120 }
121 
GetIsCreateCompleted() const122 bool OsAccountInfo::GetIsCreateCompleted() const
123 {
124     return isCreateCompleted_;
125 }
126 
SetIsCreateCompleted(const bool isCreateCompleted)127 void OsAccountInfo::SetIsCreateCompleted(const bool isCreateCompleted)
128 {
129     isCreateCompleted_ = isCreateCompleted;
130 }
131 
GetCredentialId() const132 uint64_t OsAccountInfo::GetCredentialId() const
133 {
134     return credentialId_;
135 }
136 
SetCredentialId(uint64_t credentialId)137 void OsAccountInfo::SetCredentialId(uint64_t credentialId)
138 {
139     credentialId_ = credentialId;
140 }
141 
GetDisplayId() const142 uint64_t OsAccountInfo::GetDisplayId() const
143 {
144     return displayId_;
145 }
146 
SetDisplayId(const uint64_t displayId)147 void OsAccountInfo::SetDisplayId(const uint64_t displayId)
148 {
149     displayId_ = displayId;
150 }
151 
GetIsForeground() const152 bool OsAccountInfo::GetIsForeground() const
153 {
154     return isForeground_;
155 }
156 
SetIsForeground(bool isForeground)157 void OsAccountInfo::SetIsForeground(bool isForeground)
158 {
159     isForeground_ = isForeground;
160 }
161 
GetIsLoggedIn() const162 bool OsAccountInfo::GetIsLoggedIn() const
163 {
164     return isLoggedIn_;
165 }
166 
SetIsLoggedIn(bool isLoggedIn)167 void OsAccountInfo::SetIsLoggedIn(bool isLoggedIn)
168 {
169     isLoggedIn_ = isLoggedIn;
170 }
171 
SetDomainInfo(const DomainAccountInfo & domainInfo)172 bool OsAccountInfo::SetDomainInfo(const DomainAccountInfo &domainInfo)
173 {
174     if (domainInfo.accountName_.size() > Constants::LOCAL_NAME_MAX_SIZE) {
175         ACCOUNT_LOGE("domain account name too long! %{public}zu.", domainInfo.accountName_.size());
176         return false;
177     }
178     if (domainInfo.domain_.size() > Constants::DOMAIN_NAME_MAX_SIZE) {
179         ACCOUNT_LOGE("domain name too long! %{public}zu.", domainInfo.domain_.size());
180         return false;
181     }
182     domainInfo_ = domainInfo;
183     return true;
184 }
185 
GetDomainInfo(DomainAccountInfo & domainInfo) const186 void OsAccountInfo::GetDomainInfo(DomainAccountInfo &domainInfo) const
187 {
188     domainInfo = domainInfo_;
189 }
190 
GetIsActived() const191 bool OsAccountInfo::GetIsActived() const
192 {
193     return isActivated_;
194 }
195 
SetIsActived(const bool isActivated)196 void OsAccountInfo::SetIsActived(const bool isActivated)
197 {
198     isActivated_ = isActivated;
199 }
200 
GetPhoto() const201 std::string OsAccountInfo::GetPhoto() const
202 {
203     return photo_;
204 }
205 
SetPhoto(const std::string photo)206 void OsAccountInfo::SetPhoto(const std::string photo)
207 {
208     photo_ = photo;
209 }
210 
GetCreateTime() const211 int64_t OsAccountInfo::GetCreateTime() const
212 {
213     return createTime_;
214 }
215 
SetCreateTime(const int64_t createTime)216 void OsAccountInfo::SetCreateTime(const int64_t createTime)
217 {
218     createTime_ = createTime;
219 }
220 
GetLastLoginTime() const221 int64_t OsAccountInfo::GetLastLoginTime() const
222 {
223     return lastLoginTime_;
224 }
225 
SetLastLoginTime(const int64_t lastLoginTime)226 void OsAccountInfo::SetLastLoginTime(const int64_t lastLoginTime)
227 {
228     lastLoginTime_ = lastLoginTime;
229 }
230 
ToJson() const231 Json OsAccountInfo::ToJson() const
232 {
233     Json jsonObject = Json {
234         {LOCAL_ID, localId_},
235         {LOCAL_NAME, localName_},
236         {SHORT_NAME, shortName_},
237         {TYPE, type_},
238         {CONSTRAINTS, constraints_},
239         {IS_OS_ACCOUNT_VERIFIED, isVerified_},
240         {PHOTO, photo_},
241         {CREATE_TIME, createTime_},
242         {LAST_LOGGED_IN_TIME, lastLoginTime_},
243         {SERIAL_NUMBER, serialNumber_},
244         {IS_ACTIVATED, isActivated_},
245         {IS_ACCOUNT_COMPLETED, isCreateCompleted_},
246         {TO_BE_REMOVED, toBeRemoved_},
247         {CREDENTIAL_ID, credentialId_},
248         {DISPLAY_ID, displayId_},
249         {IS_FOREGROUND, isForeground_},
250         {IS_LOGGED_IN, isLoggedIn_},
251         {DOMAIN_INFO, {
252             {DOMAIN_NAME, domainInfo_.domain_},
253             {DOMAIN_ACCOUNT_NAME, domainInfo_.accountName_},
254             {DOMAIN_ACCOUNT_ID, domainInfo_.accountId_},
255             {DOMAIN_ACCOUNT_STATUS, domainInfo_.status_},
256             {DOMAIN_ACCOUNT_CONFIG, domainInfo_.serverConfigId_},
257         },
258         }
259     };
260     return jsonObject;
261 }
262 
Unmarshalling(Parcel & parcel)263 OsAccountInfo *OsAccountInfo::Unmarshalling(Parcel &parcel)
264 {
265     OsAccountInfo *osAccountInfo = new (std::nothrow) OsAccountInfo();
266 
267     if (osAccountInfo && !osAccountInfo->ReadFromParcel(parcel)) {
268         ACCOUNT_LOGE("failed to read from parcel");
269         delete osAccountInfo;
270         osAccountInfo = nullptr;
271     }
272 
273     return osAccountInfo;
274 }
275 
GetDomainInfoFromJson(const Json & jsonObject)276 void OsAccountInfo::GetDomainInfoFromJson(const Json &jsonObject)
277 {
278     const auto &jsonObjectEnd = jsonObject.end();
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     OHOS::AccountSA::GetDataByType<DomainAccountStatus>(
289         typeJson, typeJson.end(), DOMAIN_ACCOUNT_STATUS, domainInfo_.status_, OHOS::AccountSA::JsonType::NUMBER);
290     OHOS::AccountSA::GetDataByType<std::string>(
291         typeJson, typeJson.end(), DOMAIN_ACCOUNT_CONFIG, domainInfo_.serverConfigId_,
292         OHOS::AccountSA::JsonType::STRING);
293 }
294 
FromJson(const Json & jsonObject)295 void OsAccountInfo::FromJson(const Json &jsonObject)
296 {
297     const auto &jsonObjectEnd = jsonObject.end();
298     OHOS::AccountSA::GetDataByType<int>(
299         jsonObject, jsonObjectEnd, LOCAL_ID, localId_, OHOS::AccountSA::JsonType::NUMBER);
300     OHOS::AccountSA::GetDataByType<std::string>(
301         jsonObject, jsonObjectEnd, LOCAL_NAME, localName_, OHOS::AccountSA::JsonType::STRING);
302     OHOS::AccountSA::GetDataByType<std::string>(
303         jsonObject, jsonObjectEnd, SHORT_NAME, shortName_, OHOS::AccountSA::JsonType::STRING);
304     OHOS::AccountSA::GetDataByType<OsAccountType>(
305         jsonObject, jsonObjectEnd, TYPE, type_, OHOS::AccountSA::JsonType::NUMBER);
306     OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
307         jsonObject, jsonObjectEnd, CONSTRAINTS, constraints_, OHOS::AccountSA::JsonType::ARRAY);
308     OHOS::AccountSA::GetDataByType<bool>(
309         jsonObject, jsonObjectEnd, IS_OS_ACCOUNT_VERIFIED, isVerified_, OHOS::AccountSA::JsonType::BOOLEAN);
310     OHOS::AccountSA::GetDataByType<std::string>(
311         jsonObject, jsonObjectEnd, PHOTO, photo_, OHOS::AccountSA::JsonType::STRING);
312     OHOS::AccountSA::GetDataByType<int64_t>(
313         jsonObject, jsonObjectEnd, CREATE_TIME, createTime_, OHOS::AccountSA::JsonType::NUMBER);
314     OHOS::AccountSA::GetDataByType<int64_t>(
315         jsonObject, jsonObjectEnd, LAST_LOGGED_IN_TIME, lastLoginTime_, OHOS::AccountSA::JsonType::NUMBER);
316     OHOS::AccountSA::GetDataByType<int64_t>(
317         jsonObject, jsonObjectEnd, SERIAL_NUMBER, serialNumber_, OHOS::AccountSA::JsonType::NUMBER);
318     OHOS::AccountSA::GetDataByType<bool>(
319         jsonObject, jsonObjectEnd, IS_ACTIVATED, isActivated_, OHOS::AccountSA::JsonType::BOOLEAN);
320     OHOS::AccountSA::GetDataByType<bool>(
321         jsonObject, jsonObjectEnd, IS_ACCOUNT_COMPLETED, isCreateCompleted_, OHOS::AccountSA::JsonType::BOOLEAN);
322     OHOS::AccountSA::GetDataByType<bool>(
323         jsonObject, jsonObjectEnd, TO_BE_REMOVED, toBeRemoved_, OHOS::AccountSA::JsonType::BOOLEAN);
324     OHOS::AccountSA::GetDataByType<uint64_t>(
325         jsonObject, jsonObjectEnd, CREDENTIAL_ID, credentialId_, OHOS::AccountSA::JsonType::NUMBER);
326     OHOS::AccountSA::GetDataByType<uint64_t>(
327         jsonObject, jsonObjectEnd, DISPLAY_ID, displayId_, OHOS::AccountSA::JsonType::NUMBER);
328     OHOS::AccountSA::GetDataByType<bool>(
329         jsonObject, jsonObjectEnd, IS_FOREGROUND, isForeground_, OHOS::AccountSA::JsonType::BOOLEAN);
330     OHOS::AccountSA::GetDataByType<bool>(
331         jsonObject, jsonObjectEnd, IS_LOGGED_IN, isLoggedIn_, OHOS::AccountSA::JsonType::BOOLEAN);
332 
333     GetDomainInfoFromJson(jsonObject);
334 }
335 
Marshalling(Parcel & parcel) const336 bool OsAccountInfo::Marshalling(Parcel &parcel) const
337 {
338     parcel.WriteString(ToString());
339     return true;
340 }
341 
ReadFromParcel(Parcel & parcel)342 bool OsAccountInfo::ReadFromParcel(Parcel &parcel)
343 {
344     std::string jsonString = parcel.ReadString();
345     nlohmann::json jsonObject = nlohmann::json::parse(jsonString, nullptr, false);
346     if (jsonObject.is_discarded()) {
347         ACCOUNT_LOGI("jsonObject is discarded");
348     }
349     FromJson(jsonObject);
350     return true;
351 }
352 
ToString() const353 std::string OsAccountInfo::ToString() const
354 {
355     auto jsonObject = ToJson();
356     std::string jsonString;
357     try {
358         jsonString = jsonObject.dump();
359     } catch (Json::type_error& err) {
360         ACCOUNT_LOGE("failed to dump json object, reason: %{public}s", err.what());
361     }
362     return jsonString;
363 }
364 
GetPrimeKey() const365 std::string OsAccountInfo::GetPrimeKey() const
366 {
367     return std::to_string(localId_);
368 }
369 
GetSerialNumber() const370 int64_t OsAccountInfo::GetSerialNumber() const
371 {
372     return serialNumber_;
373 }
374 
SetSerialNumber(const int64_t serialNumber)375 void OsAccountInfo::SetSerialNumber(const int64_t serialNumber)
376 {
377     serialNumber_ = serialNumber;
378 }
379 
GetToBeRemoved() const380 bool OsAccountInfo::GetToBeRemoved() const
381 {
382     return toBeRemoved_;
383 }
384 
SetToBeRemoved(bool toBeRemoved)385 void OsAccountInfo::SetToBeRemoved(bool toBeRemoved)
386 {
387     toBeRemoved_ = toBeRemoved;
388 }
389 
IsTypeOutOfRange() const390 bool OsAccountInfo::IsTypeOutOfRange() const
391 {
392     return (type_ < OsAccountType::ADMIN) || ((type_ > OsAccountType::GUEST) && (type_ < OsAccountType::PRIVATE)) ||
393         (type_ >= OsAccountType::END);
394 }
395 
ParamCheck()396 ErrCode OsAccountInfo::ParamCheck()
397 {
398     if (localId_ < Constants::START_USER_ID) {
399         ACCOUNT_LOGE("os localId is invalid");
400         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
401     }
402 
403     if (localName_.size() > Constants::LOCAL_NAME_MAX_SIZE) {
404         ACCOUNT_LOGE("local name length %{public}zu is too long!", localName_.size());
405         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
406     }
407     if (localName_.empty() && localId_ != Constants::START_USER_ID) {
408         ACCOUNT_LOGE("local name is empty!");
409         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
410     }
411 
412     if (IsTypeOutOfRange() || (localId_ == Constants::START_USER_ID && type_ != OsAccountType::ADMIN)) {
413         ACCOUNT_LOGE("os account type is invalid");
414         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
415     }
416 
417     if ((createTime_ <= 0) && (localId_ != Constants::START_USER_ID)) {
418         ACCOUNT_LOGE("os create time is invalid");
419         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
420     }
421 
422     return ERR_OK;
423 }
424 
Marshalling(Parcel & parcel) const425 bool CreateOsAccountOptions::Marshalling(Parcel &parcel) const
426 {
427     return parcel.WriteStringVector(disallowedHapList);
428 }
429 
Unmarshalling(Parcel & parcel)430 CreateOsAccountOptions *CreateOsAccountOptions::Unmarshalling(Parcel &parcel)
431 {
432     CreateOsAccountOptions *options = new (std::nothrow) CreateOsAccountOptions();
433     if ((options != nullptr) && (!options->ReadFromParcel(parcel))) {
434         ACCOUNT_LOGW("read from parcel failed");
435         delete options;
436         options = nullptr;
437     }
438     return options;
439 }
440 
ReadFromParcel(Parcel & parcel)441 bool CreateOsAccountOptions::ReadFromParcel(Parcel &parcel)
442 {
443     return parcel.ReadStringVector(&disallowedHapList);
444 }
445 }  // namespace AccountSA
446 }  // namespace OHOS