• 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 DOMAIN_ACCOUNT_STATUS = "domainAccountStatus";
44 }  // namespace
45 
OsAccountInfo()46 OsAccountInfo::OsAccountInfo()
47 {}
48 
OsAccountInfo(int localId,const std::string localName,OsAccountType type,int64_t serialNumber)49 OsAccountInfo::OsAccountInfo(int localId, const std::string localName, OsAccountType type, int64_t serialNumber)
50     : localId_(localId), localName_(localName), type_(type), serialNumber_(serialNumber)
51 {}
52 
OsAccountInfo(int localId,const std::string localName,const std::string shortName,OsAccountType type,int64_t serialNumber)53 OsAccountInfo::OsAccountInfo(int localId, const std::string localName, const std::string shortName, OsAccountType type,
54     int64_t serialNumber)
55     : localId_(localId), localName_(localName), shortName_(shortName), type_(type), serialNumber_(serialNumber)
56 {}
57 
GetLocalId() const58 int OsAccountInfo::GetLocalId() const
59 {
60     return localId_;
61 }
62 
SetLocalId(int localId)63 void OsAccountInfo::SetLocalId(int localId)
64 {
65     localId_ = localId;
66 }
67 
GetLocalName() const68 std::string OsAccountInfo::GetLocalName() const
69 {
70     return localName_;
71 }
72 
SetLocalName(const std::string localName)73 void OsAccountInfo::SetLocalName(const std::string localName)
74 {
75     localName_ = localName;
76 }
77 
GetShortName() const78 std::string OsAccountInfo::GetShortName() const
79 {
80     return shortName_;
81 }
82 
SetShortName(const std::string & shortName)83 void OsAccountInfo::SetShortName(const std::string &shortName)
84 {
85     shortName_ = shortName;
86 }
87 
GetType() const88 OsAccountType OsAccountInfo::GetType() const
89 {
90     return type_;
91 }
92 
SetType(OsAccountType type)93 void OsAccountInfo::SetType(OsAccountType type)
94 {
95     type_ = type;
96 }
97 
GetConstraints() const98 std::vector<std::string> OsAccountInfo::GetConstraints() const
99 {
100     return constraints_;
101 }
102 
SetConstraints(const std::vector<std::string> constraints)103 void OsAccountInfo::SetConstraints(const std::vector<std::string> constraints)
104 {
105     constraints_ = constraints;
106 }
107 
GetIsVerified() const108 bool OsAccountInfo::GetIsVerified() const
109 {
110     return isVerified_;
111 }
112 
SetIsVerified(bool isVerified)113 void OsAccountInfo::SetIsVerified(bool isVerified)
114 {
115     isVerified_ = isVerified;
116 }
117 
GetIsCreateCompleted() const118 bool OsAccountInfo::GetIsCreateCompleted() const
119 {
120     return isCreateCompleted_;
121 }
122 
SetIsCreateCompleted(const bool isCreateCompleted)123 void OsAccountInfo::SetIsCreateCompleted(const bool isCreateCompleted)
124 {
125     isCreateCompleted_ = isCreateCompleted;
126 }
127 
GetCredentialId() const128 uint64_t OsAccountInfo::GetCredentialId() const
129 {
130     return credentialId_;
131 }
132 
SetCredentialId(uint64_t credentialId)133 void OsAccountInfo::SetCredentialId(uint64_t credentialId)
134 {
135     credentialId_ = credentialId;
136 }
137 
SetDomainInfo(const DomainAccountInfo & domainInfo)138 bool OsAccountInfo::SetDomainInfo(const DomainAccountInfo &domainInfo)
139 {
140     if (domainInfo.accountName_.size() > Constants::DOMAIN_ACCOUNT_NAME_MAX_SIZE) {
141         ACCOUNT_LOGE("domain account name too long! %{public}zu.", domainInfo.accountName_.size());
142         return false;
143     }
144     if (domainInfo.domain_.size() > Constants::DOMAIN_NAME_MAX_SIZE) {
145         ACCOUNT_LOGE("domain name too long! %{public}zu.", domainInfo.domain_.size());
146         return false;
147     }
148     domainInfo_ = domainInfo;
149     return true;
150 }
151 
GetDomainInfo(DomainAccountInfo & domainInfo) const152 void OsAccountInfo::GetDomainInfo(DomainAccountInfo &domainInfo) const
153 {
154     domainInfo = domainInfo_;
155 }
156 
GetIsActived() const157 bool OsAccountInfo::GetIsActived() const
158 {
159     return isActivated_;
160 }
161 
SetIsActived(const bool isActivated)162 void OsAccountInfo::SetIsActived(const bool isActivated)
163 {
164     isActivated_ = isActivated;
165 }
166 
GetPhoto() const167 std::string OsAccountInfo::GetPhoto() const
168 {
169     return photo_;
170 }
171 
SetPhoto(const std::string photo)172 void OsAccountInfo::SetPhoto(const std::string photo)
173 {
174     photo_ = photo;
175 }
176 
GetCreateTime() const177 int64_t OsAccountInfo::GetCreateTime() const
178 {
179     return createTime_;
180 }
181 
SetCreateTime(const int64_t createTime)182 void OsAccountInfo::SetCreateTime(const int64_t createTime)
183 {
184     createTime_ = createTime;
185 }
186 
GetLastLoginTime() const187 int64_t OsAccountInfo::GetLastLoginTime() const
188 {
189     return lastLoginTime_;
190 }
191 
SetLastLoginTime(const int64_t lastLoginTime)192 void OsAccountInfo::SetLastLoginTime(const int64_t lastLoginTime)
193 {
194     lastLoginTime_ = lastLoginTime;
195 }
196 
ToJson() const197 Json OsAccountInfo::ToJson() const
198 {
199     Json jsonObject = Json {
200         {LOCAL_ID, localId_},
201         {LOCAL_NAME, localName_},
202         {SHORT_NAME, shortName_},
203         {TYPE, type_},
204         {CONSTRAINTS, constraints_},
205         {IS_OS_ACCOUNT_VERIFIED, isVerified_},
206         {PHOTO, photo_},
207         {CREATE_TIME, createTime_},
208         {LAST_LOGGED_IN_TIME, lastLoginTime_},
209         {SERIAL_NUMBER, serialNumber_},
210         {IS_ACTIVATED, isActivated_},
211         {IS_ACCOUNT_COMPLETED, isCreateCompleted_},
212         {TO_BE_REMOVED, toBeRemoved_},
213         {CREDENTIAL_ID, credentialId_},
214         {DOMAIN_INFO, {
215             {DOMAIN_NAME, domainInfo_.domain_},
216             {DOMAIN_ACCOUNT_NAME, domainInfo_.accountName_},
217             {DOMAIN_ACCOUNT_ID, domainInfo_.accountId_},
218             {DOMAIN_ACCOUNT_STATUS, domainInfo_.status_},
219         },
220         }
221     };
222     return jsonObject;
223 }
224 
Unmarshalling(Parcel & parcel)225 OsAccountInfo *OsAccountInfo::Unmarshalling(Parcel &parcel)
226 {
227     OsAccountInfo *osAccountInfo = new (std::nothrow) OsAccountInfo();
228 
229     if (osAccountInfo && !osAccountInfo->ReadFromParcel(parcel)) {
230         ACCOUNT_LOGE("failed to read from parcel");
231         delete osAccountInfo;
232         osAccountInfo = nullptr;
233     }
234 
235     return osAccountInfo;
236 }
237 
FromJson(const Json & jsonObject)238 void OsAccountInfo::FromJson(const Json &jsonObject)
239 {
240     const auto &jsonObjectEnd = jsonObject.end();
241     OHOS::AccountSA::GetDataByType<int>(
242         jsonObject, jsonObjectEnd, LOCAL_ID, localId_, OHOS::AccountSA::JsonType::NUMBER);
243     OHOS::AccountSA::GetDataByType<std::string>(
244         jsonObject, jsonObjectEnd, LOCAL_NAME, localName_, OHOS::AccountSA::JsonType::STRING);
245     OHOS::AccountSA::GetDataByType<std::string>(
246         jsonObject, jsonObjectEnd, SHORT_NAME, shortName_, OHOS::AccountSA::JsonType::STRING);
247     OHOS::AccountSA::GetDataByType<OsAccountType>(
248         jsonObject, jsonObjectEnd, TYPE, type_, OHOS::AccountSA::JsonType::NUMBER);
249     OHOS::AccountSA::GetDataByType<std::vector<std::string>>(
250         jsonObject, jsonObjectEnd, CONSTRAINTS, constraints_, OHOS::AccountSA::JsonType::ARRAY);
251     OHOS::AccountSA::GetDataByType<bool>(
252         jsonObject, jsonObjectEnd, IS_OS_ACCOUNT_VERIFIED, isVerified_, OHOS::AccountSA::JsonType::BOOLEAN);
253     OHOS::AccountSA::GetDataByType<std::string>(
254         jsonObject, jsonObjectEnd, PHOTO, photo_, OHOS::AccountSA::JsonType::STRING);
255     OHOS::AccountSA::GetDataByType<int64_t>(
256         jsonObject, jsonObjectEnd, CREATE_TIME, createTime_, OHOS::AccountSA::JsonType::NUMBER);
257     OHOS::AccountSA::GetDataByType<int64_t>(
258         jsonObject, jsonObjectEnd, LAST_LOGGED_IN_TIME, lastLoginTime_, OHOS::AccountSA::JsonType::NUMBER);
259     OHOS::AccountSA::GetDataByType<int64_t>(
260         jsonObject, jsonObjectEnd, SERIAL_NUMBER, serialNumber_, OHOS::AccountSA::JsonType::NUMBER);
261     OHOS::AccountSA::GetDataByType<bool>(
262         jsonObject, jsonObjectEnd, IS_ACTIVATED, isActivated_, OHOS::AccountSA::JsonType::BOOLEAN);
263     OHOS::AccountSA::GetDataByType<bool>(
264         jsonObject, jsonObjectEnd, IS_ACCOUNT_COMPLETED, isCreateCompleted_, OHOS::AccountSA::JsonType::BOOLEAN);
265     OHOS::AccountSA::GetDataByType<bool>(
266         jsonObject, jsonObjectEnd, TO_BE_REMOVED, toBeRemoved_, OHOS::AccountSA::JsonType::BOOLEAN);
267     OHOS::AccountSA::GetDataByType<uint64_t>(
268         jsonObject, jsonObjectEnd, CREDENTIAL_ID, credentialId_, OHOS::AccountSA::JsonType::NUMBER);
269 
270     Json typeJson;
271     OHOS::AccountSA::GetDataByType<Json>(
272         jsonObject, jsonObjectEnd, DOMAIN_INFO, typeJson, OHOS::AccountSA::JsonType::OBJECT);
273     OHOS::AccountSA::GetDataByType<std::string>(
274         typeJson, typeJson.end(), DOMAIN_NAME, domainInfo_.domain_, OHOS::AccountSA::JsonType::STRING);
275     OHOS::AccountSA::GetDataByType<std::string>(
276         typeJson, typeJson.end(), DOMAIN_ACCOUNT_NAME, domainInfo_.accountName_, OHOS::AccountSA::JsonType::STRING);
277     OHOS::AccountSA::GetDataByType<std::string>(
278         typeJson, typeJson.end(), DOMAIN_ACCOUNT_ID, domainInfo_.accountId_, OHOS::AccountSA::JsonType::STRING);
279     OHOS::AccountSA::GetDataByType<DomainAccountStatus>(
280         typeJson, typeJson.end(), DOMAIN_ACCOUNT_STATUS, domainInfo_.status_, OHOS::AccountSA::JsonType::NUMBER);
281 }
282 
Marshalling(Parcel & parcel) const283 bool OsAccountInfo::Marshalling(Parcel &parcel) const
284 {
285     parcel.WriteString(ToString());
286     return true;
287 }
288 
ReadFromParcel(Parcel & parcel)289 bool OsAccountInfo::ReadFromParcel(Parcel &parcel)
290 {
291     std::string jsonString = parcel.ReadString();
292     nlohmann::json jsonObject = nlohmann::json::parse(jsonString, nullptr, false);
293     FromJson(jsonObject);
294     return true;
295 }
296 
ToString() const297 std::string OsAccountInfo::ToString() const
298 {
299     auto jsonObject = ToJson();
300     std::string jsonString;
301     try {
302         jsonString = jsonObject.dump();
303     } catch (Json::type_error& err) {
304         ACCOUNT_LOGE("failed to dump json object, reason: %{public}s", err.what());
305     }
306     return jsonString;
307 }
308 
GetPrimeKey() const309 std::string OsAccountInfo::GetPrimeKey() const
310 {
311     return std::to_string(localId_);
312 }
313 
GetSerialNumber() const314 int64_t OsAccountInfo::GetSerialNumber() const
315 {
316     return serialNumber_;
317 }
318 
SetSerialNumber(const int64_t serialNumber)319 void OsAccountInfo::SetSerialNumber(const int64_t serialNumber)
320 {
321     serialNumber_ = serialNumber;
322 }
323 
GetToBeRemoved() const324 bool OsAccountInfo::GetToBeRemoved() const
325 {
326     return toBeRemoved_;
327 }
328 
SetToBeRemoved(bool toBeRemoved)329 void OsAccountInfo::SetToBeRemoved(bool toBeRemoved)
330 {
331     toBeRemoved_ = toBeRemoved;
332 }
333 
ParamCheck()334 ErrCode OsAccountInfo::ParamCheck()
335 {
336     if (localId_ < Constants::START_USER_ID) {
337         ACCOUNT_LOGE("os localId is invalid");
338         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
339     }
340 
341     if (localName_.size() > Constants::LOCAL_NAME_MAX_SIZE) {
342         ACCOUNT_LOGE("local name length %{public}zu is too long!", localName_.size());
343         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
344     }
345     if (localName_.empty() && localId_ != Constants::START_USER_ID) {
346         ACCOUNT_LOGE("local name is empty!");
347         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
348     }
349 
350     if ((type_ < OsAccountType::ADMIN) || (type_ >= OsAccountType::END) ||
351         (localId_ == Constants::START_USER_ID && type_ != OsAccountType::ADMIN)) {
352         ACCOUNT_LOGE("os account type is invalid");
353         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
354     }
355 
356     if ((createTime_ <= 0) && (localId_ != Constants::START_USER_ID)) {
357         ACCOUNT_LOGE("os create time is invalid");
358         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
359     }
360 
361     return ERR_OK;
362 }
363 
Marshalling(Parcel & parcel) const364 bool CreateOsAccountOptions::Marshalling(Parcel &parcel) const
365 {
366     return parcel.WriteStringVector(disallowedHapList);
367 }
368 
Unmarshalling(Parcel & parcel)369 CreateOsAccountOptions *CreateOsAccountOptions::Unmarshalling(Parcel &parcel)
370 {
371     CreateOsAccountOptions *options = new (std::nothrow) CreateOsAccountOptions();
372     if ((options != nullptr) && (!options->ReadFromParcel(parcel))) {
373         ACCOUNT_LOGW("read from parcel failed");
374         delete options;
375         options = nullptr;
376     }
377     return options;
378 }
379 
ReadFromParcel(Parcel & parcel)380 bool CreateOsAccountOptions::ReadFromParcel(Parcel &parcel)
381 {
382     return parcel.ReadStringVector(&disallowedHapList);
383 }
384 }  // namespace AccountSA
385 }  // namespace OHOS