• 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 OS_ACCOUNT_TOOLS_ACM_INCLUDE_ACCOUNT_COMMAND_H
17 #define OS_ACCOUNT_TOOLS_ACM_INCLUDE_ACCOUNT_COMMAND_H
18 
19 #include "os_account.h"
20 #include "shell_command.h"
21 
22 namespace OHOS {
23 namespace AccountSA {
24 namespace {
25 const std::string TOOL_NAME = "acm";
26 
27 const std::string HELP_MSG = "usage: acm <command> [<options>]\n"
28                              "These are acm commands list:\n"
29                              "  help                list available commands\n"
30                              "  create              create a local account with options\n"
31                              "  delete              delete a local account with options\n"
32                              "  switch              switch to a local account with options\n"
33 #ifdef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
34                              "  stop                stop the local accounts\n"
35 #endif // ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
36                              "  set                 set constraints of a local account\n"
37                              "  dump                dump the info of local accounts\n";
38 
39 const std::string HELP_MSG_CREATE =
40     "usage: acm create <options>\n"
41     "options list:\n"
42     "  -h, --help                                       list available commands\n"
43     "  -n <local-account-name> -t <type>                create a local account with a name and a type\n"
44     "                                                   <type>: admin, normal, guest\n";
45 
46 const std::string HELP_MSG_DELETE =
47     "usage: acm delete <options>\n"
48     "options list:\n"
49     "  -h, --help                                       list available commands\n"
50     "  -i <local-account-id>                            delete a local account with an id\n";
51 
52 const std::string HELP_MSG_DUMP =
53     "usage: acm dump <options>\n"
54     "options list:\n"
55     "  -h, --help                                       list available commands\n"
56     "  -a, --all                                        dump all local accounts\n"
57     "  -i <local-account-id>                            dump a local account with an id\n";
58 
59 const std::string HELP_MSG_SET =
60     "usage: acm set <options>\n"
61     "options list:\n"
62     "  -h, --help                                       list available commands\n"
63     "  -i <local-account-id> -c <constraints> [-e]      set constraints for a local account\n";
64 
65 const std::string HELP_MSG_SWITCH =
66     "usage: acm switch <options>\n"
67     "options list:\n"
68     "  -h, --help                                       list available commands\n"
69     "  -i <local-account-id>                            switch a local account with an id\n";
70 
71 const std::string HELP_MSG_STOP =
72     "usage: acm stop <options>\n"
73     "options list:\n"
74     "  -h, --help                                       list available commands\n"
75     "  -i <local-account-id>                            stop a local account with an id\n";
76 
77 const std::string HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT = "error: option requires an argument.";
78 const std::string HELP_MSG_NO_NAME_OPTION = "error: -n <local-account-name> is expected";
79 const std::string HELP_MSG_NO_TYPE_OPTION = "error: -t <type> is expected";
80 const std::string HELP_MSG_NO_ID_OPTION = "error: -i <local-account-id> is expected";
81 const std::string HELP_MSG_NO_CONSTRAINTS_OPTION = "error: -c <constraints> is expected";
82 const std::string HELP_MSG_INVALID_TYPE_ARGUMENT = "error: invalid type argument";
83 const std::string HELP_MSG_INVALID_ID_ARGUMENT = "error: invalid id argument";
84 
85 const std::string STRING_CREATE_OS_ACCOUNT_OK = "create the local account successfully.";
86 const std::string STRING_CREATE_OS_ACCOUNT_NG = "error: failed to create the local account.";
87 const std::string STRING_DELETE_OS_ACCOUNT_OK = "delete the local account successfully.";
88 const std::string STRING_DELETE_OS_ACCOUNT_NG = "error: failed to delete the local account.";
89 const std::string STRING_DUMP_OS_ACCOUNT_NG = "error: failed to dump state.";
90 const std::string STRING_SET_OS_ACCOUNT_CONSTRAINTS_OK = "set constraints for the local account successfully.";
91 const std::string STRING_SET_OS_ACCOUNT_CONSTRAINTS_NG = "error: failed to set constraints for the local account.";
92 const std::string STRING_SWITCH_OS_ACCOUNT_OK = "switch the local account successfully.";
93 const std::string STRING_SWITCH_OS_ACCOUNT_NG = "error: failed to switch the local account.";
94 const std::string STRING_STOP_OS_ACCOUNT_OK = "stop the local account successfully.";
95 const std::string STRING_STOP_OS_ACCOUNT_NG = "error: failed to stop the local account.";
96 }  // namespace
97 
98 class AccountCommand : public OHOS::AAFwk::ShellCommand {
99 public:
100     AccountCommand(int argc, char *argv[]);
101     ~AccountCommand() = default;
102 
103 private:
104     ErrCode CreateCommandMap() override;
105     ErrCode CreateMessageMap() override;
106     ErrCode init() override;
107 
108     ErrCode RunAsHelpCommand(void);
109     ErrCode RunAsCreateCommand(void);
110     ErrCode RunAsDeleteCommand(void);
111     ErrCode RunAsSwitchCommand(void);
112     ErrCode RunAsStopCommand(void);
113     ErrCode RunAsDumpCommand(void);
114     ErrCode RunAsSetCommand(void);
115 
116     ErrCode RunAsCreateCommandError(void);
117     ErrCode RunAsCreateCommandMissingOptionArgument(void);
118     ErrCode RunAsCreateCommandExistentOptionArgument(const int &option, std::string &name, OsAccountType &type);
119     ErrCode RunAsDeleteCommandError(void);
120     ErrCode RunAsDeleteCommandMissingOptionArgument(void);
121     ErrCode RunAsDeleteCommandExistentOptionArgument(const int &option, int &id);
122     ErrCode RunAsDumpCommandError(void);
123     ErrCode RunAsDumpCommandMissingOptionArgument(void);
124     ErrCode RunAsDumpCommandExistentOptionArgument(const int &option, int &id);
125     ErrCode RunAsSetCommandError(void);
126     ErrCode RunAsSetCommandMissingOptionArgument(void);
127     ErrCode RunAsSetCommandExistentOptionArgument(
128         const int &option, int &id, std::vector<std::string> &constraints, bool &enable);
129     ErrCode RunAsSwitchCommandError(void);
130     ErrCode RunAsSwitchCommandMissingOptionArgument(void);
131     ErrCode RunAsSwitchCommandExistentOptionArgument(const int &option, int &id);
132     ErrCode RunAsStopCommandError(void);
133     ErrCode RunAsStopCommandMissingOptionArgument(void);
134     ErrCode RunAsStopCommandExistentOptionArgument(const int &option, int &id);
135 
136     ErrCode AnalyzeTypeArgument(OsAccountType &type);
137     ErrCode AnalyzeLocalIdArgument(int &id);
138     ErrCode AnalyzeConstraintArgument(std::vector<std::string> &constraints);
139 };
140 }  // namespace AccountSA
141 }  // namespace OHOS
142 
143 #endif  // OS_ACCOUNT_TOOLS_ACM_INCLUDE_ACCOUNT_COMMAND_H
144