1c2b37d2cSopenharmony_ci/*
2c2b37d2cSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3c2b37d2cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c2b37d2cSopenharmony_ci * you may not use this file except in compliance with the License.
5c2b37d2cSopenharmony_ci * You may obtain a copy of the License at
6c2b37d2cSopenharmony_ci *
7c2b37d2cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c2b37d2cSopenharmony_ci *
9c2b37d2cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c2b37d2cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c2b37d2cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c2b37d2cSopenharmony_ci * See the License for the specific language governing permissions and
13c2b37d2cSopenharmony_ci * limitations under the License.
14c2b37d2cSopenharmony_ci */
15c2b37d2cSopenharmony_ci
16c2b37d2cSopenharmony_ci#ifndef OS_ACCOUNT_TOOLS_ACM_INCLUDE_ACCOUNT_COMMAND_H
17c2b37d2cSopenharmony_ci#define OS_ACCOUNT_TOOLS_ACM_INCLUDE_ACCOUNT_COMMAND_H
18c2b37d2cSopenharmony_ci
19c2b37d2cSopenharmony_ci#include "os_account.h"
20c2b37d2cSopenharmony_ci
21c2b37d2cSopenharmony_cinamespace OHOS {
22c2b37d2cSopenharmony_cinamespace AccountSA {
23c2b37d2cSopenharmony_ciconst std::string TOOL_NAME = "acm";
24c2b37d2cSopenharmony_ciconst std::string HELP_MSG_NO_OPTION = "error: you must specify an option at least.";
25c2b37d2cSopenharmony_ciconst std::string HELP_MSG = "usage: acm <command> [<options>]\n"
26c2b37d2cSopenharmony_ci                             "These are acm commands list:\n"
27c2b37d2cSopenharmony_ci                             "  help                list available commands\n"
28c2b37d2cSopenharmony_ci                             "  create              create a local account with options\n"
29c2b37d2cSopenharmony_ci                             "  delete              delete a local account with options\n"
30c2b37d2cSopenharmony_ci                             "  switch              switch to a local account with options\n"
31c2b37d2cSopenharmony_ci                             "  deactivate          deactivate to a local account with options\n"
32c2b37d2cSopenharmony_ci                             "  set                 set constraints of a local account\n"
33c2b37d2cSopenharmony_ci                             "  dump                dump the info of local accounts\n";
34c2b37d2cSopenharmony_ci
35c2b37d2cSopenharmony_ciconst std::string HELP_MSG_CREATE =
36c2b37d2cSopenharmony_ci    "usage: acm create <options>\n"
37c2b37d2cSopenharmony_ci    "options list:\n"
38c2b37d2cSopenharmony_ci    "  -h, --help                           list available commands\n"
39c2b37d2cSopenharmony_ci    "  -n <local-account-name> [-s] <shortName> -t <type> [-l] <disallowed-install-hap-list>\n"
40c2b37d2cSopenharmony_ci    "                                       create a local account with a name and a type\n"
41c2b37d2cSopenharmony_ci    "                                       <type>: admin, normal, guest, private\n"
42c2b37d2cSopenharmony_ci    "                                       <disallowed-install-hap-list>: can set disallowed pre-installed hap list\n";
43c2b37d2cSopenharmony_ci
44c2b37d2cSopenharmony_ciconst std::string HELP_MSG_DELETE =
45c2b37d2cSopenharmony_ci    "usage: acm delete <options>\n"
46c2b37d2cSopenharmony_ci    "options list:\n"
47c2b37d2cSopenharmony_ci    "  -h, --help                                       list available commands\n"
48c2b37d2cSopenharmony_ci    "  -i <local-account-id>                            delete a local account with an id\n";
49c2b37d2cSopenharmony_ci
50c2b37d2cSopenharmony_ciconst std::string HELP_MSG_DUMP =
51c2b37d2cSopenharmony_ci    "usage: acm dump <options>\n"
52c2b37d2cSopenharmony_ci    "options list:\n"
53c2b37d2cSopenharmony_ci    "  -h, --help                                       list available commands\n"
54c2b37d2cSopenharmony_ci    "  -a, --all                                        dump all local accounts\n"
55c2b37d2cSopenharmony_ci    "  -i <local-account-id>                            dump a local account with an id\n";
56c2b37d2cSopenharmony_ci
57c2b37d2cSopenharmony_ciconst std::string HELP_MSG_SET =
58c2b37d2cSopenharmony_ci    "usage: acm set <options>\n"
59c2b37d2cSopenharmony_ci    "options list:\n"
60c2b37d2cSopenharmony_ci    "  -h, --help                                       list available commands\n"
61c2b37d2cSopenharmony_ci    "  -i <local-account-id> -c <constraints> [-e]      set constraints for a local account\n";
62c2b37d2cSopenharmony_ci
63c2b37d2cSopenharmony_ciconst std::string HELP_MSG_SWITCH =
64c2b37d2cSopenharmony_ci    "usage: acm switch <options>\n"
65c2b37d2cSopenharmony_ci    "options list:\n"
66c2b37d2cSopenharmony_ci    "  -h, --help                                       list available commands\n"
67c2b37d2cSopenharmony_ci    "  -i <local-account-id>                            switch a local account with an id\n";
68c2b37d2cSopenharmony_ci
69c2b37d2cSopenharmony_ciconst std::string HELP_MSG_DEACTIVATE =
70c2b37d2cSopenharmony_ci    "usage: acm deactivate <options>\n"
71c2b37d2cSopenharmony_ci    "options list:\n"
72c2b37d2cSopenharmony_ci    "  -a, --all                                        deactivate all local account\n"
73c2b37d2cSopenharmony_ci    "  -h, --help                                       list available commands\n"
74c2b37d2cSopenharmony_ci    "  -i <local-account-id>                            deactivate a local account with an id\n";
75c2b37d2cSopenharmony_ci
76c2b37d2cSopenharmony_ciconst std::string HELP_MSG_STOP =
77c2b37d2cSopenharmony_ci    "usage: acm stop <options>\n"
78c2b37d2cSopenharmony_ci    "options list:\n"
79c2b37d2cSopenharmony_ci    "  -h, --help                                       list available commands\n"
80c2b37d2cSopenharmony_ci    "  -i <local-account-id>                            stop a local account with an id\n";
81c2b37d2cSopenharmony_ci
82c2b37d2cSopenharmony_ciconst std::string HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT = "error: option requires an argument.";
83c2b37d2cSopenharmony_ciconst std::string HELP_MSG_NO_NAME_OPTION = "error: -n <local-account-name> is expected";
84c2b37d2cSopenharmony_ciconst std::string HELP_MSG_NO_TYPE_OPTION = "error: -t <type> is expected";
85c2b37d2cSopenharmony_ciconst std::string HELP_MSG_NO_ID_OPTION = "error: -i <local-account-id> is expected";
86c2b37d2cSopenharmony_ciconst std::string HELP_MSG_NO_CONSTRAINTS_OPTION = "error: -c <constraints> is expected";
87c2b37d2cSopenharmony_ciconst std::string HELP_MSG_INVALID_TYPE_ARGUMENT = "error: invalid type argument";
88c2b37d2cSopenharmony_ciconst std::string HELP_MSG_INVALID_ID_ARGUMENT = "error: invalid id argument";
89c2b37d2cSopenharmony_ci
90c2b37d2cSopenharmony_ciconst std::string STRING_CREATE_OS_ACCOUNT_OK = "create the local account successfully.";
91c2b37d2cSopenharmony_ciconst std::string STRING_CREATE_OS_ACCOUNT_NG = "error: failed to create the local account.";
92c2b37d2cSopenharmony_ciconst std::string STRING_DELETE_OS_ACCOUNT_OK = "delete the local account successfully.";
93c2b37d2cSopenharmony_ciconst std::string STRING_DELETE_OS_ACCOUNT_NG = "error: failed to delete the local account.";
94c2b37d2cSopenharmony_ciconst std::string STRING_DUMP_OS_ACCOUNT_NG = "error: failed to dump state.";
95c2b37d2cSopenharmony_ciconst std::string STRING_SET_OS_ACCOUNT_CONSTRAINTS_OK = "set constraints for the local account successfully.";
96c2b37d2cSopenharmony_ciconst std::string STRING_SET_OS_ACCOUNT_CONSTRAINTS_NG = "error: failed to set constraints for the local account.";
97c2b37d2cSopenharmony_ciconst std::string STRING_SWITCH_OS_ACCOUNT_OK = "switch the local account successfully.";
98c2b37d2cSopenharmony_ciconst std::string STRING_SWITCH_OS_ACCOUNT_NG = "error: failed to switch the local account.";
99c2b37d2cSopenharmony_ciconst std::string STRING_STOP_OS_ACCOUNT_OK = "stop the local account successfully.";
100c2b37d2cSopenharmony_ciconst std::string STRING_STOP_OS_ACCOUNT_NG = "error: failed to stop the local account.";
101c2b37d2cSopenharmony_ciconst std::string STRING_DEACTIVATE_OS_ACCOUNT_OK = "deactivate the local account successfully.";
102c2b37d2cSopenharmony_ciconst std::string STRING_DEACTIVATE_OS_ACCOUNT_NG = "error: failed to deactivate the local account.";
103c2b37d2cSopenharmony_ciconst std::string STRING_DEACTIVATE_ALL_OS_ACCOUNTS_OK = "deactivate all local account successfully.";
104c2b37d2cSopenharmony_ciconst std::string STRING_DEACTIVATE_ALL_OS_ACCOUNTS_NG = "error: failed to deactivate all local account.";
105c2b37d2cSopenharmony_ci
106c2b37d2cSopenharmony_ciclass AccountCommand {
107c2b37d2cSopenharmony_cipublic:
108c2b37d2cSopenharmony_ci    AccountCommand(int argc, char *argv[]);
109c2b37d2cSopenharmony_ci    ~AccountCommand() = default;
110c2b37d2cSopenharmony_ci    std::string ExecCommand();
111c2b37d2cSopenharmony_ci
112c2b37d2cSopenharmony_ciprivate:
113c2b37d2cSopenharmony_ci    void CreateCommandMap();
114c2b37d2cSopenharmony_ci    void OnCommand();
115c2b37d2cSopenharmony_ci    std::string GetCommandErrorMsg() const;
116c2b37d2cSopenharmony_ci    std::string GetUnknownOptionMsg(std::string& unknownOption) const;
117c2b37d2cSopenharmony_ci
118c2b37d2cSopenharmony_ci    ErrCode RunAsHelpCommand(void);
119c2b37d2cSopenharmony_ci    ErrCode RunAsCreateCommand(void);
120c2b37d2cSopenharmony_ci    ErrCode RunAsDeleteCommand(void);
121c2b37d2cSopenharmony_ci    ErrCode RunAsSwitchCommand(void);
122c2b37d2cSopenharmony_ci    ErrCode RunAsDeactivateCommand(void);
123c2b37d2cSopenharmony_ci    ErrCode RunAsDumpCommand(void);
124c2b37d2cSopenharmony_ci    ErrCode RunAsSetCommand(void);
125c2b37d2cSopenharmony_ci
126c2b37d2cSopenharmony_ci    ErrCode RunAsCreateCommandError(void);
127c2b37d2cSopenharmony_ci    ErrCode RunAsCreateCommandMissingOptionArgument(void);
128c2b37d2cSopenharmony_ci    ErrCode RunAsCreateCommandExistentOptionArgument(const int &option, std::string &name,
129c2b37d2cSopenharmony_ci        std::string &shortName, OsAccountType &type, std::vector<std::string> &disallowedList);
130c2b37d2cSopenharmony_ci    ErrCode RunAsSetCommandError(void);
131c2b37d2cSopenharmony_ci    ErrCode RunAsSetCommandMissingOptionArgument(void);
132c2b37d2cSopenharmony_ci    ErrCode RunAsSetCommandExistentOptionArgument(
133c2b37d2cSopenharmony_ci        const int &option, int &id, std::vector<std::string> &constraints, bool &enable);
134c2b37d2cSopenharmony_ci    ErrCode RunAsCommonCommandExistentOptionArgument(const int &option, int &id);
135c2b37d2cSopenharmony_ci    ErrCode RunAsCommonCommandMissingOptionArgument(const std::string &command);
136c2b37d2cSopenharmony_ci    ErrCode RunCommandError(const std::string &command);
137c2b37d2cSopenharmony_ci
138c2b37d2cSopenharmony_ci    void ParseCommandOpt(const std::string &command, ErrCode &result, int &id);
139c2b37d2cSopenharmony_ci    void RunCommand(int &counter, ErrCode &result, bool &enable, int &id, std::vector<std::string> &constraints);
140c2b37d2cSopenharmony_ci    ErrCode ParseCreateCommandOpt(std::string &name,
141c2b37d2cSopenharmony_ci        std::string &shortName, OsAccountType &osAccountType, std::vector<std::string> &disallowedList);
142c2b37d2cSopenharmony_ci
143c2b37d2cSopenharmony_ci    ErrCode AnalyzeTypeArgument(OsAccountType &type);
144c2b37d2cSopenharmony_ci    ErrCode AnalyzeDisallowedListArgument(std::vector<std::string> &disallowedList);
145c2b37d2cSopenharmony_ci    ErrCode AnalyzeLocalIdArgument(int &id);
146c2b37d2cSopenharmony_ci    ErrCode AnalyzeConstraintArgument(std::vector<std::string> &constraints);
147c2b37d2cSopenharmony_ci
148c2b37d2cSopenharmony_ciprotected:
149c2b37d2cSopenharmony_ci    int argc_ = 0;
150c2b37d2cSopenharmony_ci    char** argv_ = nullptr;
151c2b37d2cSopenharmony_ci
152c2b37d2cSopenharmony_ci    std::string cmd_;
153c2b37d2cSopenharmony_ci    std::vector<std::string> argList_;
154c2b37d2cSopenharmony_ci
155c2b37d2cSopenharmony_ci    std::string name_;
156c2b37d2cSopenharmony_ci    std::map<std::string, std::function<int()>> commandMap_;
157c2b37d2cSopenharmony_ci
158c2b37d2cSopenharmony_ci    std::string resultReceiver_;
159c2b37d2cSopenharmony_ci};
160c2b37d2cSopenharmony_ci}  // namespace AccountSA
161c2b37d2cSopenharmony_ci}  // namespace OHOS
162c2b37d2cSopenharmony_ci
163c2b37d2cSopenharmony_ci#endif  // OS_ACCOUNT_TOOLS_ACM_INCLUDE_ACCOUNT_COMMAND_H
164