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 #include "os_account_info_json_parser.h"
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
OsAccountInfo()25 OsAccountInfo::OsAccountInfo()
26 {}
27
OsAccountInfo(int localId,const std::string localName,OsAccountType type,int64_t serialNumber)28 OsAccountInfo::OsAccountInfo(int localId, const std::string localName, OsAccountType type, int64_t serialNumber)
29 : localId_(localId), localName_(localName), type_(type), serialNumber_(serialNumber)
30 {}
31
OsAccountInfo(int localId,const std::string localName,OsAccountType type)32 OsAccountInfo::OsAccountInfo(int localId, const std::string localName, OsAccountType type)
33 : localId_(localId), localName_(localName), type_(type)
34 {}
35
OsAccountInfo(int localId,const std::string localName,const std::string shortName,OsAccountType type,int64_t serialNumber)36 OsAccountInfo::OsAccountInfo(int localId, const std::string localName, const std::string shortName, OsAccountType type,
37 int64_t serialNumber)
38 : localId_(localId), localName_(localName), shortName_(shortName), type_(type), serialNumber_(serialNumber)
39 {}
40
GetLocalId() const41 int OsAccountInfo::GetLocalId() const
42 {
43 return localId_;
44 }
45
SetLocalId(int localId)46 void OsAccountInfo::SetLocalId(int localId)
47 {
48 localId_ = localId;
49 }
50
GetLocalName() const51 std::string OsAccountInfo::GetLocalName() const
52 {
53 return localName_;
54 }
55
SetLocalName(const std::string localName)56 void OsAccountInfo::SetLocalName(const std::string localName)
57 {
58 localName_ = localName;
59 }
60
GetShortName() const61 std::string OsAccountInfo::GetShortName() const
62 {
63 return shortName_;
64 }
65
SetShortName(const std::string & shortName)66 void OsAccountInfo::SetShortName(const std::string &shortName)
67 {
68 shortName_ = shortName;
69 }
70
GetType() const71 OsAccountType OsAccountInfo::GetType() const
72 {
73 return type_;
74 }
75
SetType(OsAccountType type)76 void OsAccountInfo::SetType(OsAccountType type)
77 {
78 type_ = type;
79 }
80
GetConstraints() const81 std::vector<std::string> OsAccountInfo::GetConstraints() const
82 {
83 return constraints_;
84 }
85
SetConstraints(const std::vector<std::string> constraints)86 void OsAccountInfo::SetConstraints(const std::vector<std::string> constraints)
87 {
88 constraints_ = constraints;
89 }
90
GetIsVerified() const91 bool OsAccountInfo::GetIsVerified() const
92 {
93 return isVerified_;
94 }
95
SetIsVerified(bool isVerified)96 void OsAccountInfo::SetIsVerified(bool isVerified)
97 {
98 isVerified_ = isVerified;
99 }
100
GetIsCreateCompleted() const101 bool OsAccountInfo::GetIsCreateCompleted() const
102 {
103 return isCreateCompleted_;
104 }
105
SetIsCreateCompleted(const bool isCreateCompleted)106 void OsAccountInfo::SetIsCreateCompleted(const bool isCreateCompleted)
107 {
108 isCreateCompleted_ = isCreateCompleted;
109 }
110
GetCredentialId() const111 uint64_t OsAccountInfo::GetCredentialId() const
112 {
113 return credentialId_;
114 }
115
SetCredentialId(uint64_t credentialId)116 void OsAccountInfo::SetCredentialId(uint64_t credentialId)
117 {
118 credentialId_ = credentialId;
119 }
120
GetDisplayId() const121 uint64_t OsAccountInfo::GetDisplayId() const
122 {
123 return displayId_;
124 }
125
SetDisplayId(const uint64_t displayId)126 void OsAccountInfo::SetDisplayId(const uint64_t displayId)
127 {
128 displayId_ = displayId;
129 }
130
GetIsForeground() const131 bool OsAccountInfo::GetIsForeground() const
132 {
133 return isForeground_;
134 }
135
SetIsForeground(bool isForeground)136 void OsAccountInfo::SetIsForeground(bool isForeground)
137 {
138 isForeground_ = isForeground;
139 }
140
GetIsLoggedIn() const141 bool OsAccountInfo::GetIsLoggedIn() const
142 {
143 return isLoggedIn_;
144 }
145
SetIsLoggedIn(bool isLoggedIn)146 void OsAccountInfo::SetIsLoggedIn(bool isLoggedIn)
147 {
148 isLoggedIn_ = isLoggedIn;
149 }
150
GetIsDataRemovable() const151 bool OsAccountInfo::GetIsDataRemovable() const
152 {
153 return isDataRemovable_;
154 }
155
SetIsDataRemovable(bool isDataRemovable)156 void OsAccountInfo::SetIsDataRemovable(bool isDataRemovable)
157 {
158 isDataRemovable_ = isDataRemovable;
159 }
160
GetCreatorType() const161 int32_t OsAccountInfo::GetCreatorType() const
162 {
163 return creatorType_;
164 }
165
SetCreatorType(int32_t creatorType)166 void OsAccountInfo::SetCreatorType(int32_t creatorType)
167 {
168 creatorType_ = creatorType;
169 }
170
SetDomainInfo(const DomainAccountInfo & domainInfo)171 bool OsAccountInfo::SetDomainInfo(const DomainAccountInfo &domainInfo)
172 {
173 if (domainInfo.accountName_.size() > Constants::LOCAL_NAME_MAX_SIZE) {
174 ACCOUNT_LOGE("domain account name too long! %{public}zu.", domainInfo.accountName_.size());
175 return false;
176 }
177 if (domainInfo.domain_.size() > Constants::DOMAIN_NAME_MAX_SIZE) {
178 ACCOUNT_LOGE("domain name too long! %{public}zu.", domainInfo.domain_.size());
179 return false;
180 }
181 domainInfo_ = domainInfo;
182 return true;
183 }
184
GetDomainInfo(DomainAccountInfo & domainInfo) const185 void OsAccountInfo::GetDomainInfo(DomainAccountInfo &domainInfo) const
186 {
187 domainInfo = domainInfo_;
188 }
189
GetIsActived() const190 bool OsAccountInfo::GetIsActived() const
191 {
192 return isActivated_;
193 }
194
SetIsActived(const bool isActivated)195 void OsAccountInfo::SetIsActived(const bool isActivated)
196 {
197 isActivated_ = isActivated;
198 }
199
GetPhoto() const200 std::string OsAccountInfo::GetPhoto() const
201 {
202 return photo_;
203 }
204
SetPhoto(const std::string photo)205 void OsAccountInfo::SetPhoto(const std::string photo)
206 {
207 photo_ = photo;
208 }
209
GetCreateTime() const210 int64_t OsAccountInfo::GetCreateTime() const
211 {
212 return createTime_;
213 }
214
SetCreateTime(const int64_t createTime)215 void OsAccountInfo::SetCreateTime(const int64_t createTime)
216 {
217 createTime_ = createTime;
218 }
219
GetLastLoginTime() const220 int64_t OsAccountInfo::GetLastLoginTime() const
221 {
222 return lastLoginTime_;
223 }
224
SetLastLoginTime(const int64_t lastLoginTime)225 void OsAccountInfo::SetLastLoginTime(const int64_t lastLoginTime)
226 {
227 lastLoginTime_ = lastLoginTime;
228 }
229
Unmarshalling(Parcel & parcel)230 OsAccountInfo *OsAccountInfo::Unmarshalling(Parcel &parcel)
231 {
232 OsAccountInfo *osAccountInfo = new (std::nothrow) OsAccountInfo();
233
234 if (osAccountInfo && !osAccountInfo->ReadFromParcel(parcel)) {
235 ACCOUNT_LOGE("failed to read from parcel");
236 delete osAccountInfo;
237 osAccountInfo = nullptr;
238 }
239
240 return osAccountInfo;
241 }
242
Marshalling(Parcel & parcel) const243 bool OsAccountInfo::Marshalling(Parcel &parcel) const
244 {
245 return parcel.WriteString(ToString());
246 }
247
ReadFromParcel(Parcel & parcel)248 bool OsAccountInfo::ReadFromParcel(Parcel &parcel)
249 {
250 std::string jsonString = parcel.ReadString();
251 auto jsonObject = CreateJsonFromString(jsonString);
252 if (jsonObject == nullptr) {
253 ACCOUNT_LOGE("jsonObject is_discarded");
254 }
255 FromJson(jsonObject.get(), *this);
256 return true;
257 }
258
ToString() const259 std::string OsAccountInfo::ToString() const
260 {
261 auto jsonObject = ToJson(*this);
262 std::string jsonString = PackJsonToString(jsonObject);
263 if (jsonString.empty()) {
264 ACCOUNT_LOGE("failed to dump json object");
265 }
266 return jsonString;
267 }
268
GetPrimeKey() const269 std::string OsAccountInfo::GetPrimeKey() const
270 {
271 return std::to_string(localId_);
272 }
273
GetSerialNumber() const274 int64_t OsAccountInfo::GetSerialNumber() const
275 {
276 return serialNumber_;
277 }
278
SetSerialNumber(const int64_t serialNumber)279 void OsAccountInfo::SetSerialNumber(const int64_t serialNumber)
280 {
281 serialNumber_ = serialNumber;
282 }
283
GetToBeRemoved() const284 bool OsAccountInfo::GetToBeRemoved() const
285 {
286 return toBeRemoved_;
287 }
288
SetToBeRemoved(bool toBeRemoved)289 void OsAccountInfo::SetToBeRemoved(bool toBeRemoved)
290 {
291 toBeRemoved_ = toBeRemoved;
292 }
293
IsTypeOutOfRange() const294 bool OsAccountInfo::IsTypeOutOfRange() const
295 {
296 return (type_ < OsAccountType::ADMIN) || ((type_ > OsAccountType::GUEST) && (type_ < OsAccountType::PRIVATE)) ||
297 (type_ >= OsAccountType::END);
298 }
299
ParamCheck()300 ErrCode OsAccountInfo::ParamCheck()
301 {
302 if (localId_ < Constants::START_USER_ID) {
303 ACCOUNT_LOGE("os localId is invalid");
304 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
305 }
306
307 if (localName_.size() > Constants::LOCAL_NAME_MAX_SIZE) {
308 ACCOUNT_LOGE("local name length %{public}zu is too long!", localName_.size());
309 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
310 }
311 if (localName_.empty() && localId_ != Constants::START_USER_ID) {
312 ACCOUNT_LOGE("local name is empty!");
313 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
314 }
315
316 if (IsTypeOutOfRange() || (localId_ == Constants::START_USER_ID && type_ != OsAccountType::ADMIN)) {
317 ACCOUNT_LOGE("os account type is invalid");
318 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
319 }
320
321 if ((createTime_ <= 0) && (localId_ != Constants::START_USER_ID)) {
322 ACCOUNT_LOGE("os create time is invalid");
323 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
324 }
325
326 return ERR_OK;
327 }
328
Marshalling(Parcel & parcel) const329 bool CreateOsAccountOptions::Marshalling(Parcel &parcel) const
330 {
331 if (allowedHapList.has_value()) {
332 if (!parcel.WriteBool(true)) {
333 ACCOUNT_LOGE("Write has value failed.");
334 return false;
335 }
336 std::vector<std::string> list = allowedHapList.value();
337 if (list.size() > ALLOWED_HAP_LIST_MAX_SIZE) {
338 ACCOUNT_LOGE("Abnormal allowedHapList data size, size %{public}zu", list.size());
339 return false;
340 }
341 if (!parcel.WriteStringVector(list)) {
342 ACCOUNT_LOGE("Write allowedHapList failed.");
343 return false;
344 }
345 } else {
346 if (!parcel.WriteBool(false)) {
347 ACCOUNT_LOGE("Write has not value failed.");
348 return false;
349 }
350 }
351 return parcel.WriteStringVector(disallowedHapList) && parcel.WriteBool(hasShortName);
352 }
353
Unmarshalling(Parcel & parcel)354 CreateOsAccountOptions *CreateOsAccountOptions::Unmarshalling(Parcel &parcel)
355 {
356 CreateOsAccountOptions *options = new (std::nothrow) CreateOsAccountOptions();
357 if ((options != nullptr) && (!options->ReadFromParcel(parcel))) {
358 ACCOUNT_LOGW("read from parcel failed");
359 delete options;
360 options = nullptr;
361 }
362 return options;
363 }
364
ReadFromParcel(Parcel & parcel)365 bool CreateOsAccountOptions::ReadFromParcel(Parcel &parcel)
366 {
367 bool hasValue = false;
368 if (!parcel.ReadBool(hasValue)) {
369 ACCOUNT_LOGE("Read has value failed.");
370 return false;
371 }
372 if (hasValue) {
373 std::vector<std::string> list = {};
374 if (!parcel.ReadStringVector(&list)) {
375 ACCOUNT_LOGE("Read allowedHapList failed.");
376 return false;
377 }
378 if (list.size() > ALLOWED_HAP_LIST_MAX_SIZE) {
379 ACCOUNT_LOGE("Abnormal allowedHapList data size reading form parcel, size %{public}zu", list.size());
380 return false;
381 }
382 allowedHapList = std::make_optional<std::vector<std::string>>(list);
383 }
384 return parcel.ReadStringVector(&disallowedHapList) && parcel.ReadBool(hasShortName);
385 }
386 } // namespace AccountSA
387 } // namespace OHOS