• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef BASE_ACCOUNT_ACCOUNT_INFO_H
17 #define BASE_ACCOUNT_ACCOUNT_INFO_H
18 
19 #include <ctime>
20 #include <string>
21 #include "account_error_no.h"
22 #include "ohos_account_constants.h"
23 #include "string_ex.h"
24 #include "want.h"
25 
26 namespace OHOS {
27 namespace AccountSA {
28 
29 /**
30  * Account state type .
31  */
32 typedef enum : std::int32_t {
33     // invalid state
34     ACCOUNT_STATE_INVALID = -1,
35     // no account bound
36     ACCOUNT_STATE_UNBOUND = 0,
37     // account login
38     ACCOUNT_STATE_LOGIN,
39     // account logout but not unbound
40     ACCOUNT_STATE_NOTLOGIN,
41     // account logoff, all the data of this account will be deleted in the network
42     ACCOUNT_STATE_LOGOFF,
43 } OHOS_ACCOUNT_STATE;
44 
45 // event string
46 const std::string EVENT_PUBLISH                    = "event publish";
47 const std::string OHOS_ACCOUNT_EVENT_LOGIN         = "Ohos.account.event.LOGIN";
48 const std::string OHOS_ACCOUNT_EVENT_LOGOUT        = "Ohos.account.event.LOGOUT";
49 const std::string OHOS_ACCOUNT_EVENT_TOKEN_INVALID = "Ohos.account.event.TOKEN_INVALID";
50 const std::string OHOS_ACCOUNT_EVENT_LOGOFF        = "Ohos.account.event.LOGOFF";
51 const std::string OPERATION_INIT_OPEN_FILE_TO_READ = "InitOpenFileToRead";
52 const std::string OPERATION_REMOVE_FILE = "RemoveFile";
53 const std::string OPERATION_OPEN_FILE_TO_READ = "OpenFileToRead";
54 const std::string OPERATION_OPEN_FILE_TO_WRITE = "OpenFileToWrite";
55 const std::string OPERATION_CHANGE_MODE_FILE = "ChangeModeFile";
56 const std::string OPERATION_FORCE_CREATE_DIRECTORY = "ForceCreateDirectory";
57 const std::string OPERATION_CHANGE_MODE_DIRECTORY = "ChangeModeDirectory";
58 /**
59  * Account operation events
60  */
61 typedef enum : std::int32_t {
62     ACCOUNT_INVALID_EVT = -1, // invalid account event
63     ACCOUNT_BIND_SUCCESS_EVT = 0, // bind account successfully
64     ACCOUNT_BIND_FAILED_EVT, // bind account failed
65     ACCOUNT_AUTHENTICATE_SUCCESS_EVT, // authenticate account successfully
66     ACCOUNT_AUTHENTICATE_FAILED_EVT, // authenticate account failed
67     ACCOUNT_TOKEN_EXPIRED_EVT, // local token of account expired
68     ACCOUNT_PASSWORD_CHANGED_EVT, // account password changed in remount server
69     ACCOUNT_MANUAL_LOGOUT_EVT, // account logout manually
70     ACCOUNT_MANUAL_UNBOUND_EVT, // account unbound manually
71     ACCOUNT_MANUAL_LOGOFF_EVT, // account logoff manually
72 } ACCOUNT_INNER_EVENT_TYPE;
73 
74 const std::string DEFAULT_OHOS_ACCOUNT_NAME = "ohosAnonymousName"; // default name
75 const std::string DEFAULT_OHOS_ACCOUNT_UID = "ohosAnonymousUid"; // default UID
76 constexpr std::int32_t UID_TRANSFORM_DIVISOR = 200000; // local account id = uid / UID_TRANSFORM_DIVISOR
77 constexpr std::int32_t MAIN_OS_ACCOUNT_LOCAL_ID = 100; // main os account local id = 100
78 
79 class OhosAccountInfo {
80 public:
81     std::string name_;
82     std::string uid_;
83     std::int32_t status_;
84     std::string nickname_;
85     std::string avatar_;
86     AAFwk::Want scalableData_;
87 
OhosAccountInfo(const std::string & name,const std::string & id,std::int32_t status)88     OhosAccountInfo(const std::string &name, const std::string &id, std::int32_t status)
89         : name_(name), uid_(id), status_(status), rawUid_(id)
90     {
91         nickname_ = "";
92         avatar_ = "";
93         scalableData_ = {};
94     }
95 
OhosAccountInfo()96     OhosAccountInfo()
97     {
98         name_ = "";
99         uid_ = "";
100         nickname_ = "";
101         avatar_ = "";
102         scalableData_ = {};
103         status_ = ACCOUNT_STATE_UNBOUND;
104     }
105 
~OhosAccountInfo()106     ~OhosAccountInfo() {};
107 
108     // filtering the input scalableData only
GetScalableDataString(const AAFwk::Want & scalableData)109     static std::string GetScalableDataString(const AAFwk::Want &scalableData)
110     {
111         std::string result = "";
112         AAFwk::WantParams wantParams = scalableData.GetParams();
113         for (auto it : wantParams.GetParams()) {
114             if (it.first != "moduleName") {
115                 result += it.first;
116                 int typeId = AAFwk::WantParams::GetDataType(it.second);
117                 result += wantParams.GetStringByType(it.second, typeId);
118             }
119         }
120         return result;
121     }
122 
IsValid()123     bool IsValid() const
124     {
125         std::string str = GetScalableDataString(scalableData_);
126         return (nickname_.size() <= Constants::NICKNAME_MAX_SIZE) && (avatar_.size() <= Constants::AVATAR_MAX_SIZE) &&
127             (str.size() <= Constants::SCALABLEDATA_MAX_SIZE);
128     }
129 
GetRawUid()130     std::string GetRawUid() const
131     {
132         return rawUid_;
133     }
134 
SetRawUid(std::string rawUid)135     void SetRawUid(std::string rawUid)
136     {
137         rawUid_ = rawUid;
138     }
139 
140 private:
141     std::string rawUid_;
142 };
143 
144 class AccountInfo {
145 public:
146     OhosAccountInfo ohosAccountInfo_;
147     std::time_t bindTime_;
148     std::int32_t userId_;
149     std::string digest_;
AccountInfo()150     AccountInfo()
151     {
152         bindTime_ = 0;
153         userId_ = 0;
154         digest_ = "";
155     }
156 
AccountInfo(const OhosAccountInfo & ohosAccountInfo)157     AccountInfo(const OhosAccountInfo &ohosAccountInfo)
158     {
159         ohosAccountInfo_ = ohosAccountInfo;
160         bindTime_ = 0;
161         userId_ = 0;
162         digest_ = "";
163     }
164 
165     bool operator==(const AccountInfo &info)
166     {
167         return (ohosAccountInfo_.uid_ == info.ohosAccountInfo_.uid_);
168     }
169 
170     void clear(std::int32_t clrStatus = ACCOUNT_STATE_UNBOUND)
171     {
172         ohosAccountInfo_.name_ = DEFAULT_OHOS_ACCOUNT_NAME;
173         ohosAccountInfo_.uid_ = DEFAULT_OHOS_ACCOUNT_UID;
174         ohosAccountInfo_.status_ = clrStatus;
175         ohosAccountInfo_.nickname_ = "";
176         ohosAccountInfo_.avatar_ = "";
177         ohosAccountInfo_.scalableData_ = {};
178         ohosAccountInfo_.SetRawUid("");
179         digest_.clear();
180         bindTime_ = 0;
181     }
182 
~AccountInfo()183     ~AccountInfo() {}
184 };
185 } // namespace AccountSA
186 } // namespace OHOS
187 
188 #endif // BASE_ACCOUNT_ACCOUNT_INFO_H
189