• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 
16 #include "domain_account_policy.h"
17 
18 #include "cJSON.h"
19 #include "cjson_check.h"
20 #include "edm_log.h"
21 #include "parcel_macro.h"
22 
23 namespace OHOS {
24 namespace EDM {
DomainAccountPolicy()25 DomainAccountPolicy::DomainAccountPolicy()
26 {
27     EDMLOGD("admin account policy instance is created without parameters");
28 }
29 
DomainAccountPolicy(int32_t authenticationValidityPeriod,int32_t passwordValidityPeriod,int32_t passwordExpirationNotification)30 DomainAccountPolicy::DomainAccountPolicy(int32_t authenticationValidityPeriod, int32_t passwordValidityPeriod,
31     int32_t passwordExpirationNotification) : authenticationValidityPeriod(authenticationValidityPeriod),
32     passwordValidityPeriod(passwordValidityPeriod), passwordExpirationNotification(passwordExpirationNotification)
33 {
34     EDMLOGD("admin account policy instance is created with parameters");
35 }
36 
~DomainAccountPolicy()37 DomainAccountPolicy::~DomainAccountPolicy()
38 {
39     EDMLOGD("admin account policy instance is destroyed");
40 }
41 
Marshalling(MessageParcel & parcel) const42 bool DomainAccountPolicy::Marshalling(MessageParcel &parcel) const
43 {
44     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, authenticationValidityPeriod);
45     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, passwordValidityPeriod);
46     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, passwordExpirationNotification);
47     return true;
48 }
49 
Unmarshalling(MessageParcel & parcel,DomainAccountPolicy & domainAccountPolicy)50 bool DomainAccountPolicy::Unmarshalling(MessageParcel &parcel, DomainAccountPolicy &domainAccountPolicy)
51 {
52     return domainAccountPolicy.ReadFromParcel(parcel);
53 }
54 
ReadFromParcel(MessageParcel & parcel)55 bool DomainAccountPolicy::ReadFromParcel(MessageParcel &parcel)
56 {
57     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, authenticationValidityPeriod);
58     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, passwordValidityPeriod);
59     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, passwordExpirationNotification);
60     return true;
61 }
62 
ConvertDomainAccountPolicyToJsonStr(std::string & jsonStr)63 bool DomainAccountPolicy::ConvertDomainAccountPolicyToJsonStr(std::string &jsonStr)
64 {
65     cJSON *json = nullptr;
66     CJSON_CREATE_OBJECT_AND_CHECK(json, false);
67 
68     if (cJSON_AddNumberToObject(json, "authenticationValidityPeriod", authenticationValidityPeriod) == NULL) {
69         cJSON_Delete(json);
70         return false;
71     }
72     if (cJSON_AddNumberToObject(json, "passwordMaximumAge", passwordValidityPeriod) == NULL) {
73         cJSON_Delete(json);
74         return false;
75     }
76     if (cJSON_AddNumberToObject(json, "passwordExpirationNotification", passwordExpirationNotification) == NULL) {
77         cJSON_Delete(json);
78         return false;
79     }
80 
81     char *jsonStrTemp = cJSON_PrintUnformatted(json);
82     if (jsonStrTemp == nullptr) {
83         cJSON_Delete(json);
84         return false;
85     }
86     jsonStr = std::string(jsonStrTemp);
87     cJSON_Delete(json);
88     cJSON_free(jsonStrTemp);
89     return true;
90 }
91 
JsonStrToDomainAccountPolicy(const std::string & jsonStr,DomainAccountPolicy & domainAccountPolicy)92 bool DomainAccountPolicy::JsonStrToDomainAccountPolicy(const std::string &jsonStr,
93     DomainAccountPolicy &domainAccountPolicy)
94 {
95     cJSON *json = cJSON_Parse(jsonStr.c_str());
96     if (json == nullptr) {
97         return false;
98     }
99     cJSON *itemAuthenticationValidityPeriod = cJSON_GetObjectItem(json, "authenticationValidityPeriod");
100     cJSON *itemPasswordMaximumAge = cJSON_GetObjectItem(json, "passwordMaximumAge");
101     cJSON *itemPasswordExpirationNotification = cJSON_GetObjectItem(json, "passwordExpirationNotification");
102 
103     bool ret1 = cJSON_IsNumber(itemAuthenticationValidityPeriod);
104     bool ret2 = cJSON_IsNumber(itemPasswordMaximumAge);
105     bool ret3 = cJSON_IsNumber(itemPasswordExpirationNotification);
106 
107     if (ret1) {
108         domainAccountPolicy.authenticationValidityPeriod = itemAuthenticationValidityPeriod->valueint;
109     }
110     if (ret2) {
111         domainAccountPolicy.passwordValidityPeriod = itemPasswordMaximumAge->valueint;
112     }
113     if (ret3) {
114         domainAccountPolicy.passwordExpirationNotification = itemPasswordExpirationNotification->valueint;
115     }
116     cJSON_Delete(json);
117     return ret1 || ret2 || ret3;
118 }
119 
CheckParameterValidity()120 bool DomainAccountPolicy::CheckParameterValidity()
121 {
122     return (authenticationValidityPeriod >= -1) && (passwordValidityPeriod >= -1) &&
123         (passwordExpirationNotification >= 0);
124 }
125 } // namespace EDM
126 } // namespace OHOS